// 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 Hiperscan.AnalysisModule;
using Hiperscan.ApoIdentCore.Enums;

namespace Hiperscan.ApoIdentCore.Misc
{
    public class GlobalStates
    {
        public event EventHandler TopLevelWindowStateChanged = null;
        public event EventHandler BusyStateChanged = null;
        public event EventHandler QuitRequestedStateChanged = null;

        public GlobalStates()
        {
        }

        public void AddInstallation(bool ident_server)
        {
            if (ident_server)
            {
                if (this.ModuleAvailableState == ModuleAvailableState.None)
                    this.ModuleAvailableState = ModuleAvailableState.OnlyIdentModule;

                if (this.ModuleAvailableState == ModuleAvailableState.OnlyQuantifierModule)
                    this.ModuleAvailableState = ModuleAvailableState.BothModules;
            }
            else
            {
                if (this.ModuleAvailableState == ModuleAvailableState.None)
                    this.ModuleAvailableState = ModuleAvailableState.OnlyQuantifierModule;

                if (this.ModuleAvailableState == ModuleAvailableState.OnlyIdentModule)
                    this.ModuleAvailableState = ModuleAvailableState.BothModules;
            }
        }

        public void RemoveInstallation(AnalysisType analysis_type)
        {
            if (analysis_type == AnalysisType.Identification)
            {
                if (this.ModuleAvailableState == ModuleAvailableState.BothModules)
                    this.ModuleAvailableState = ModuleAvailableState.OnlyQuantifierModule;

                if (this.ModuleAvailableState == ModuleAvailableState.OnlyIdentModule)
                    this.ModuleAvailableState = ModuleAvailableState.None;
            }

            if (analysis_type == AnalysisType.Quantification)
            {
                if (this.ModuleAvailableState == ModuleAvailableState.BothModules)
                    this.ModuleAvailableState = ModuleAvailableState.OnlyIdentModule;

                if (this.ModuleAvailableState == ModuleAvailableState.OnlyQuantifierModule)
                    this.ModuleAvailableState = ModuleAvailableState.None;
            }
        }


        public void ResetAvailableModules()
        {
            this.ModuleAvailableState = ModuleAvailableState.None;
        }

        private bool is_top_level_window = true;
        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.TopLevelWindowStateChanged?.Invoke(this, EventArgs.Empty);
            }
        }

        private bool quit_requested = true;
        public bool QuitRequested
        {
            get
            {
                return this.quit_requested;
            }
            set
            {
                if (this.quit_requested == value)
                    return;

                this.quit_requested = value;
                this.QuitRequestedStateChanged?.Invoke(this, EventArgs.Empty);
            }
        }

        private bool busy = true;
        public bool Busy
        {
            get
            {
                return this.busy;
            }
            set
            {
                if (this.busy == value)
                    return;

                this.busy = value;
                this.BusyStateChanged?.Invoke(this, EventArgs.Empty);
            }
        }

        public bool IsIdentModuleAvailable
        {
            get
            {
                return this.ModuleAvailableState == ModuleAvailableState.BothModules ||
                           this.ModuleAvailableState == ModuleAvailableState.OnlyIdentModule;
            }
        }

        public bool IsOnlyIdentModuleAvailable
        {
            get
            {
                return this.ModuleAvailableState == ModuleAvailableState.OnlyIdentModule;
            }
        }

        public bool IsQuantifierModuleAvailable
        {
            get
            {
                return this.ModuleAvailableState == ModuleAvailableState.BothModules ||
                           this.ModuleAvailableState == ModuleAvailableState.OnlyQuantifierModule;
            }
        }

        public bool IsOnlyQuantifierModuleAvailable
        {
            get
            {
                return this.ModuleAvailableState == ModuleAvailableState.OnlyQuantifierModule;
            }
        }

        private ModuleAvailableState ModuleAvailableState { get; set; }

        public bool ConnectionSuccess { get; set; }
        public bool ProgramExitCalled { get; set; } = false;
        public bool SilentExit { get; set; } = false;
        public bool ExitContiueAction { get; set; } = false;
        public bool HasNotMeasurableData { get; set; } = false;

    }
}
