// 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 Hiperscan.GtkExtensions.GuiSync;
using Hiperscan.Extensions.ReferenceCheck.Control.Enums;
using Hiperscan.Extensions.ReferenceCheck.Control.GuiUpdate;

namespace Hiperscan.Extensions.ReferenceCheck.View
{
    public partial class GUI : GuiSynchronizerTemplate<SyncControl, UpdateArgs, DialogArgs>
    {
        protected override void InitGuiSyncControl(Gtk.Window main_window, SyncControl sync_control)
        {
            this.MainWindow = main_window;
            this.SyncControl = sync_control;
        }

        protected override void OtherUpdates(UpdateArgs arg)
        {
            if (arg.ViewType == ViewTypes.ExitStart)
            {
                this.main_window.Sensitive = false;
                return;
            }

            if (arg.ViewType == ViewTypes.ExitEnd)
            {
                this.Exit();
                return;
            }

            if (arg.ViewChanged)
                this.SetVisibleView(arg.ViewType);

            if (arg.UpdateCatecory.HasFlag(UpdateCategory.CanNext))
                this.UpdateViewControlSection();

            if (arg.ViewType != ViewTypes.None)
            {
                switch (arg.ViewType)
                {
                    case ViewTypes.Password:
                        this.UpdatePasswordSection();
                        break;

                    case ViewTypes.Session:
                        this.UpdateSessionSection();
                        break;

                    case ViewTypes.InitDevice:
                        this.UpdateInitDeviceSection(arg.UpdateCatecory);
                        break;

                    case ViewTypes.Acquisition:
                        this.UpdateAcquisitionSection(arg.UpdateCatecory);
                        break;

                    case ViewTypes.Protocol:
                        this.UpdateProtocolSection(arg.UpdateCatecory, arg.PharmacyProfiles);
                        break;

                    case ViewTypes.Restore:
                        this.UpdateRestoreSection();
                        break;

                    default:
                        break;
                }
            }

            if (arg.DialogArgs.ZipDialogType != ZipDialogType.None)
                this.ShowZipDialog(arg);
        }

        private void SetVisibleView(ViewTypes category)
        {
            foreach (ViewTypes item in this.Views.Keys)
                this.Views[item].Visible = false;

            this.Views[category].Visible = true;
        }

        private void ShowZipDialog(UpdateArgs arg)
        {
            switch(arg.DialogArgs.ZipDialogType)
            {
                case ZipDialogType.Open:
                    ZipFileOpenDialog.Run(this.MainWindow, arg);
                    break;

                case ZipDialogType.Save:
                    ZipFileSaveDialog.Run(this.MainWindow, arg);
                    break;

                default:
                    break;
            }
        }
    }
}
