//
//    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 System.Threading;
using Gtk;
using Hiperscan.ApoIdentCore.ActionControl;
using Hiperscan.GtkExtensions.Widgets;
using Hiperscan.Unix;
namespace Hiperscan.Extensions.ApoIdent
{
    public partial class ApoIdentApplication
    {
        public void ProcessAction(ApoActionArg arg)
        {
            this.Control.GuiActionSync.Enter(arg);

            if (this.Control.MainThreadId == Thread.CurrentThread.ManagedThreadId)
                this.__ActionThreadSafe(arg, EventArgs.Empty);
            else
                Application.Invoke(arg, EventArgs.Empty, this.__ActionThreadSafe);

            this.Control.GuiActionSync.Wait(arg);
        }
            
        private void __ActionThreadSafe(object sender, EventArgs e)
        {
            ApoActionArg arg = sender as ApoActionArg;

            switch (arg.Action)
            {
                case ApoActionType.BeginExit:
                    this.BeginExit();
                    break;

                case ApoActionType.ContinueExit:
                    this.ContinueExit();
                    break;

                case ApoActionType.EndExit: 
                    this.EndExit();
                    break;

                case ApoActionType.CloseWindows:
                    this.CloseWindows();
                    break;

                case ApoActionType.UpdateProperties:
                    this.UpdateProperties();
                    break;

                case ApoActionType.CloseHost:
                    this.Host.Quit();
                    break;

                default:
                    break;
            }

            this.Control.GuiActionSync.Leave(arg);
        }

        private void BeginExit()
        {
            this.MainWindow.Visible = this.Control.GlobalStates.SilentExit == false;

            if (Plugin.replace_gui && this.Control.GlobalStates.SilentExit == false)
            {
                if (this.Host.CheckForUnsavedSpectra() == false)
                    this.Control.GlobalStates.ExitContiueAction = false;
            }
        }

        private void ContinueExit()
        {
            this.WaitWindow = null;

            if (this.Control.GlobalStates.SilentExit == false)
            {
                this.WaitWindow = new WaitWindow(this.MainWindow,
                                                 Catalog.GetString("Closing application ..."),
                                                 500);
            }

            if (this.Control.GuiSettings.WindowMaximized == false)
            {
                this.MainWindow.GetSize(out int width, out int height);
                this.Control.GuiSettings.WindowWidth = width;
                this.Control.GuiSettings.WindowHeight = height;
            }
        }

        private void EndExit()
        {
            if (this.WaitWindow != null)
                this.WaitWindow.Destroy();

            this.WaitWindow = null;

            this.MainWindow.Destroy();

            if (Plugin.replace_gui)
            {
                this.Host.MainWindow.Destroy();
                this.Host.Quit(true);
            }
            else
            {
                ApoIdentApplication.CurrentInstance = null;
            }
        }

        private void CloseWindows()
        {
            ExtToolTip.CloseToolTips();
            this.SpecimenDeliveryDateEntry.CloseCalenderWindow();
            this.SpecimenExpiryDateEntry.CloseCalenderWindow();

            if (this.HelpMenu != null)
                this.HelpMenu.Destroy();

            if (this.ArchiveMenu != null)
                this.ArchiveMenu.Destroy();

            if (this.CannabisMenu != null)
                this.CannabisMenu.Destroy();

            if (ValidationMenu != null)
                this.ValidationMenu.Destroy();
        }

        private void UpdateProperties()
        {
            this.Host.UpdateProperties();
        }

        private WaitWindow WaitWindow { get; set; }
    }
}