// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2023, Hiperscan GmbH Dresden, Germany
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;

using Glade;

using Gtk;

using Hiperscan.Unix;


namespace Hiperscan.GtkExtensions.Widgets
{
    public class WaitAndProgressWindow
    {
        [Widget] Gtk.Window wait_window = null;
        [Widget] Gtk.ProgressBar wait_progressbar = null;
        [Widget] Gtk.EventBox design_eventbox = null;
        [Widget] Gtk.Label wait_label = null;


        public WaitAndProgressWindow(Gtk.Window parent, string text)
        {
   
            this.Init(parent, text);
        }

        private void Init(Gtk.Window parent, string text)
        {
            Glade.XML gui = new Glade.XML(null, "wait.glade", "wait_window", "Hiperscan");
            gui.Autoconnect(this);

            this.wait_window.TransientFor = parent;

            this.wait_progressbar.Fraction = 0;
            this.wait_progressbar.PulseStep = 0.12;
            this.IsPulsing = true;

            this.wait_label.Text = text;
            this.wait_window.ShowAll();
        }

        public void Pulse()
        {
            System.Threading.Tasks.Task.Run(() =>
            {
                System.Threading.Thread.Sleep(this.SleepTime);

                Application.Invoke((sender, e) =>
                {
                    lock (this)
                    {
                        if (this.IsPulsing == true)
                        {
                            this.wait_progressbar.Pulse();
                            this.wait_window.ShowAll();
                        }
                    }
                });

                if (this.IsPulsing == true)
                    this.Pulse();
            });
        }

        public void SetProgress(double progress, double total, string text)
        {
            this.wait_label.Text = text;

            this.IsPulsing = false;
            this.wait_progressbar.Fraction = progress / total;

            this.wait_window.ShowAll();
        }

        public void Finish(string finish_text)    
        {
            this.wait_label.Text = finish_text;
            this.IsPulsing = false;
            this.wait_progressbar.Fraction = 1;
            this.wait_window.ShowAll();
        }

        public void Destroy()
        {
            lock (this)
            {
                this.IsPulsing = false;
                this.wait_progressbar.Fraction = 1;
                this.wait_window.Destroy();
            }
        }

        public Gdk.Color FontColor
        {
            set
            {
                WidgetColor.SetFontColor(this.wait_label, value);
                wait_window.QueueDraw();
            }
        }

        public Gdk.Color BackgroundColor
        {
            set
            {
                this.design_eventbox.ModifyBg(StateType.Normal, value);
                wait_window.QueueDraw();
            }
        }

        private bool IsPulsing { get; set; } = false;
        public int SleepTime { get; set; } = 150;
    }
}
