// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2021 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 Hiperscan.Unix;
namespace Hiperscan.Extensions.ApoIdent
{
    public partial class ApoIdentApplication
    {
        private void InitDataBackup()
        {
            DataBackupDialog.StartZipping += this.OnStartZipping;
            DataBackupDialog.ZippingFinished += this.OnZippingFinished;
            DataBackupDialog.ZippingFailed += this.OnZippingFailed;
            DataBackupDialog.ZippingProgress += this.OnZippingProgressed;     
        }

        private void OnStartZipping(object sender, EventArgs e)
        {
            DataBackupDialog dialog = sender as DataBackupDialog;
            this.InitZippingProgressDialog(dialog.Mode);
        }

        private void OnZippingFinished(object sender, EventArgs e)
        {
            DataBackupDialog dialog = sender as DataBackupDialog;

            this.IsPulseZipping = false;
            this.DestrpyZippingProgessDialog();
        }

        private void OnZippingProgressed(object sender, EventArgs e)
        {
            DataBackupDialog dialog = sender as DataBackupDialog;
            this.IsPulseZipping = false;

            Gtk.Application.Invoke((sender1, e2) =>
            {
                try
                {
                    this.DataBackupProgressDialog.SetProgress(dialog.FileProgressCount, dialog.FileCount);
                }
                catch(Exception)
                {

                }
            });
        }

        private void OnZippingFailed(object sender, EventArgs e)
        {
            DataBackupDialog dialog = sender as DataBackupDialog;

            this.IsPulseZipping = false;
            this.DestrpyZippingProgessDialog();

            string message = String.Format(Catalog.GetString("Cannot export data: {0}"), 
                                           dialog.Exception.Message);

            this.Update(ApoIdentApplicationState.ShowErrorDialogWithServiceDialog, message, dialog.Exception);
        }




        private void InitZippingProgressDialog(DataBackupDialog.ExportMode mode)
        {
            Gtk.Application.Invoke((sender, e) =>
            {
                this.DataBackupProgressDialog = new DataBackupWaitDialog(this.MainWindow, mode);
                this.IsPulseZipping = true;
                this.PulseZippingFileSearch();
            });

            System.Threading.Thread.Sleep(1500);
        }

        private void PulseZippingFileSearch()
        {
            System.Threading.Tasks.Task.Run(() =>
            {
                for (; ; )
                {
                    System.Threading.Thread.Sleep(150);

                    if (this.IsPulseZipping)
                    {
                        Gtk.Application.Invoke((sender, e) =>
                        {
                            try
                            {
                                if (this.DataBackupProgressDialog != null)
                                    this.DataBackupProgressDialog.Pulse();
                            }
                            catch(Exception)
                            {

                            }
                        });

                    }
                    else
                    {
                        break;
                    }
                }
            });
        }

        private void DestrpyZippingProgessDialog()
        {
            Gtk.Application.Invoke((sender, e) =>
            {
                this.DataBackupProgressDialog.Finish();
            });

            System.Threading.Thread.Sleep(2500);

            Gtk.Application.Invoke((sender, e) =>
            {
                this.DataBackupProgressDialog.Destroy();
                this.DataBackupProgressDialog = null;
            
            });
        }

        private DataBackupWaitDialog DataBackupProgressDialog { get; set; }
        private bool IsPulseZipping { get; set; } = false;
    }
}
