// 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.Reflection;

using Hiperscan.Unix;
using Hiperscan.Common.I18n;
using Hiperscan.GuiExtensions;
using Hiperscan.GuiExtensions.Dialog;
using Hiperscan.Extensions.ReferenceCheck.Control.Enums;
using Hiperscan.Extensions.ReferenceCheck.Control.GuiUpdate;

namespace Hiperscan.Extensions.ReferenceCheck.Control
{
    public partial class CoreApp
    {
        public CoreApp(IHost host)
        {
            this.Host = host;
            this.Host.DontShowInfoMessage = true;
            this.FinderButton.Reset();
        }

        public void TryToExit()
        {
            System.Threading.Tasks.Task.Run(() => this.DoTryToExit());
        }

        private void DoTryToExit()
        {
            if (this.CanExit() == false)
                return;

            if (this.CurrentProgrammState == ProgramState.Warmingup)
            {
                this.InterruptWarming();
                return;
            }

            if (this.CurrentVisibleView == ViewTypes.Protocol)
            {
                if (this.SessionManager.IsRestored)
                {
                    this.Exit();
                    return;
                }

                if (this.ProtocolSaveSuccess == true)
                {
                    if (this.SessionManager.AreChangesNotSaved == false)
                    {
                        this.Exit();
                        return;
                    }

                    UpdateArgs args = new UpdateArgs();
                    args.DialogArgs.StandardDialogType = StandardDialogType.RemindSaving;
                    args.DialogArgs.Message = Catalog.GetString("Not all changes have been saved. Do you want to save them now?");
                    this.UpdateGuiNeeded?.Invoke(args);

                    switch (args.DialogArgs.Response)
                    {
                        case DialogRespnseType.Cancel:
                            return;

                        case DialogRespnseType.No:
                            this.Exit();
                            return;

                        default:
                            //TODO CREATE AND SAVE
                            if (this.ProtocolSaveSuccess)
                                this.Exit();

                            return;
                    }
                }
            }

            UpdateArgs arg = new UpdateArgs();
            arg.DialogArgs.StandardDialogType = StandardDialogType.Question;
            arg.DialogArgs.Message = Catalog.GetString("Are you sure that you want to close the application?");
            this.UpdateGuiNeeded?.Invoke(arg);

            if (arg.DialogArgs.Response == DialogRespnseType.Yes)
                this.Exit();
        }

        private void Exit()
        {
            if (this.QuitRequested == true)
                return;

            this.QuitRequested = true;

            this.CurrentProgrammState = ProgramState.Exit;
            UpdateArgs arg = new UpdateArgs();
            arg.ViewType = ViewTypes.ExitStart;
            arg.CursorState = CursorState.Busy;
            this.UpdateGuiNeeded?.Invoke(arg);

            this.ResetDevice();
            this.CalibrationSet?.CurrentSession?.StopTimer();
            System.Threading.Thread.Sleep(2000);

            arg.CursorState = CursorState.None;
            arg.ViewType = ViewTypes.ExitEnd;
            this.UpdateGuiNeeded?.Invoke(arg);

            this.Host.DontShowInfoMessage = false;
        }

        public static string ApplicationName
        {
            get
            {
                return "References Check";
            }
        }

        public static string Version
        {
            get
            {
                Version version = Assembly.GetExecutingAssembly().GetName().Version;
                version = new Version(version.Major, version.Minor, version.Build);
                return version.ToString();
            }
        }

        public static string Creator
        {
            get
            {
                return  CoreApp.ApplicationName + " " + CoreApp.Version;
            }
        }

        private bool quit_requested = false;
        public bool QuitRequested
        {
            get
            {
                return this.quit_requested;
            }

            set
            {
                if (this.quit_requested == value)
                    return;

                this.quit_requested = value;
                this.FinderButton.FinderButtonState.IsNoQuitRequested.IsActive = this.quit_requested == false;
            }
        }

        private bool is_top_level_window = false;
        public bool IsTopLevelWindow
        {
            get
            {
                return this.is_top_level_window;
            }

            set
            {
                if (this.is_top_level_window == value)
                    return;

                this.is_top_level_window = value;
                this.FinderButton.FinderButtonState.IsMainWindowTopLevel.IsActive = this.is_top_level_window;
            }
        }
        

        public SyncControl GuiSyncControl { get; private set; } = new SyncControl();


        private IHost Host { get; set; }
        private CatalogLocale.LocaleType Locale { get; set; } = CatalogLocale.GetLocaleType();
        private Protocol.ReferenceCheckProtocol Protocol { get; set; }
    }
}