// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2022, 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.Tasks;
using Hiperscan.ApoIdentCore.GuiControl;
using Hiperscan.GuiExtensions.Dialog;
using Hiperscan.Unix;

namespace Hiperscan.ApoIdentCore
{
    public partial class AppControl
    {
        public bool OpenQuickStepWindow()
        {
            this.ProgramSync.EnterCriticialZone();

            bool continue_action = this.GuiSettings.DontShowQuickStepMessageAgain;

            UpdateArgs arg = new UpdateArgs();
            arg.DialogArgs.StandardDialogType = StandardDialogType.DontShowAgainQuestion;
            arg.DialogArgs.DontShowAgain = this.GuiSettings.DontShowQuickStepMessageAgain;
            arg.DialogArgs.Message = Catalog.GetString("In QuickStep mode (for advanced users) parameters of the spectrometer can be modified. Wrong parameters may produce incorrect measurements or identifications. Are you sure you want to continue?");

            this.UpdateGuiNeeded?.Invoke(arg);

            if (arg.DialogArgs.Response == DialogRespnseType.Yes)
                this.GuiSettings.DontShowQuickStepMessageAgain = arg.DialogArgs.DontShowAgain;

            this.ProgramSync.LeaveCriticalZone();

            return arg.DialogArgs.Response == DialogRespnseType.Yes || continue_action;
        }

        public void ExitApplicationByUser()
        {
            Task.Run(() =>
            {
                this.ProgramSync.EnterCriticialZone();
                this.Exit(false);
                this.ProgramSync.LeaveCriticalZone();
            });
        }

        public void ShowQueryDialog()
        {
            this.ProgramSync.EnterCriticialZone();

            UpdateArgs arg = UpdateArgs.GetResetArg();
            arg.DialogArgs.ApoDialogType = ApoDialogType.Query;
            this.UpdateGuiNeeded?.Invoke(arg);

            this.ProgramSync.LeaveCriticalZone();
        }

        public void ResetReferences()
        {
            this.ProgramSync.EnterCriticialZone();

            try
            { 
                this.MeasuringManager.Finder.ResetAllReferences();
                this.MeasuringManager.DeactivateRecalibrationTimer();

                UpdateArgs arg = UpdateArgs.GetResetArg();
                if (this.ResultManager.AnalysisResultExists == false)
                    arg.AnimationType = this.MeasuringManager.GetMeasuringState();
                this.UpdateGuiNeeded?.Invoke(arg);
            }
            catch (Exception ex)
            {
                UpdateArgs arg = new UpdateArgs();
                arg.DialogArgs.StandardDialogType = StandardDialogType.ErrorWithServiceMessage;
                arg.DialogArgs.Exception = ex;
                arg.DialogArgs.Message = String.Format(Catalog.GetString("Cannot reset references: {0}"), ex.Message);
                this.UpdateGuiNeeded?.Invoke(arg);
            }

            this.ProgramSync.LeaveCriticalZone();
        }

        public void ShowPreferencesDialog()
        {
            this.ProgramSync.EnterCriticialZone();
            bool saved = this.ProtocolManager.CheckProtocolSaveState(this);

            if (saved)
            {
                this.ResetSubstanceSelection();

                UpdateArgs arg = new UpdateArgs();
                arg.DialogArgs.ApoDialogType = ApoDialogType.Preferences;

                this.UpdateGuiNeeded?.Invoke(arg);

                this.SetReadyForSubstanceInput();
            }

            this.ProgramSync.LeaveCriticalZone();
        }

        public void Reconnect()
        {
            System.Threading.Tasks.Task.Run(() =>
            {
                this.ProgramSync.EnterCriticialZone();
                this.Connect();
                this.ProgramSync.LeaveCriticalZone();
            });
        }

        public void ShowSubstanceExtensionDialog()
        {
            this.ProgramSync.EnterCriticialZone();

            if (this.ProtocolManager.CheckProtocolSaveState(this))
            {
                this.ResetSubstanceSelection();

                UpdateArgs arg = new UpdateArgs();
                arg.DialogArgs.ApoDialogType = ApoDialogType.SubstanceExtension;
                this.UpdateGuiNeeded?.Invoke(arg);
               
                this.SubstanceManager.SubstancesCatalog.RefreshUserDefinedSubstances(this.ProfileManager.CurrentProfile.ModelExtension.ModelClass);

                arg = new UpdateArgs();
                arg.Substance = UpdateCategory.ReloadValues | UpdateCategory.Sensitives;
                arg.Pharmacy = UpdateCategory.Sensitives;
                this.UpdateGuiNeeded?.Invoke(arg);
                this.SetReadyForSubstanceInput();
            }

            this.ProgramSync.LeaveCriticalZone();
        }

        public void RequestQuit()
        {
            if (this.MeasuringManager.Finder.FinderExists)
                this.MeasuringManager.Finder.QuitRequested = true;

            this.GlobalStates.QuitRequested = true;
        }

        public void ShowAboutDialog()
        {
            this.ProgramSync.EnterCriticialZone();

            UpdateArgs arg = new UpdateArgs();
            arg.DialogArgs.ApoDialogType = ApoDialogType.AboutDialog;
            this.UpdateGuiNeeded?.Invoke(arg);

            this.ProgramSync.LeaveCriticalZone();
        }
    }
}
