// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2018 Thomas Klose & Torsten Schache, 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.Common.IO;
using Hiperscan.Common.LogWriter;
using Hiperscan.GtkExtensions.Widgets;
using Hiperscan.Unix;


namespace Hiperscan.Extensions.ApoIdent
{
    public class DataBackupWaitDialog
    {
        [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 DataBackupWaitDialog(Gtk.Window parent, DataBackupDialog.ExportMode mode)
        {
            this.Mode = mode;
            this.Init(parent);
        }

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

            ApoIdent.ApoIdentApplication.SetWmcClassName(this.wait_window);

            this.wait_window.TransientFor = parent;

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

            this.design_eventbox.ModifyBg(StateType.Normal, new Gdk.Color(52, 53, 63));

    
            WidgetColor.SetFontColor(this.wait_label, new Gdk.Color(255, 255, 255));

            if (this.Mode == DataBackupDialog.ExportMode.OnlyLogFiles)
                this.wait_label.Text = Catalog.GetString("Compressing files...");
            else
                this.wait_label.Text = Catalog.GetString("Searching files...");

            GuiDesigner.SetSchema(this.wait_window);
            this.wait_window.ShowAll();
        }

        public void Pulse()
        {
            if (this.IsPulsing == true)
                this.wait_progressbar.Pulse();

            this.wait_window.ShowAll();
        }

        public void SetProgress(double progress, double total)
        {
            if (this.Mode != DataBackupDialog.ExportMode.OnlyLogFiles)
                this.wait_label.Text  = Catalog.GetString("Compressing files...");

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

            this.wait_window.ShowAll();
        }

        public void Finish()    
        {
            this.wait_label.Text = Catalog.GetString("Finished");
            this.IsPulsing = false;
            this.wait_progressbar.Fraction = 1;
            this.wait_window.ShowAll();
        }

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

        private DataBackupDialog.ExportMode Mode { get; set; }
        private bool IsPulsing { get; set; } = false;
    }
}
