// Created with MonoDevelop
//
//    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 Hiperscan.ApoIdentCore.Enums;
using Hiperscan.ApoIdentCore.GuiControl;
using Hiperscan.ApoIdentCore.Misc;
using Hiperscan.ApoIdentCore.SectionManager.Substance.SubstanceSelection;

namespace Hiperscan.ApoIdentCore
{
    public partial class AppControl
    {
        private void InitGlobalStates()
        {
            if (this.GlobalStates == null)
            {
                this.GlobalStates = new GlobalStates();
                this.GlobalStates.BusyStateChanged += this.OnBusyStateChanged;
                this.GlobalStates.TopLevelWindowStateChanged += this.OnTopLevelWindowStateChanged;
                this.GlobalStates.QuitRequestedStateChanged += this.OnQuitRequestedStateChanged;

                this.GlobalStates.QuitRequested = false;
                this.GlobalStates.Busy = false;
                this.GlobalStates.IsTopLevelWindow = false;
            }
        }

        private void OnBusyStateChanged(object sender, EventArgs e)
        {
            bool is_not_busy = (sender as GlobalStates).Busy == false;

            if (this.MeasuringManager != null)
                this.MeasuringManager.Finder.FinderButton.FinderButtonState.IsMainWindowNotBusy.IsActive = is_not_busy;
        }

        private void OnQuitRequestedStateChanged(object sender, EventArgs e)
        {
            bool is_no_quit_requested = (sender as GlobalStates).QuitRequested == false;

            if (this.MeasuringManager != null)
                this.MeasuringManager.Finder.FinderButton.FinderButtonState.IsNoQuitRequested.IsActive = is_no_quit_requested;
        }

        private void OnTopLevelWindowStateChanged(object sender, EventArgs e)
        {
            bool is_top_level = (sender as GlobalStates).IsTopLevelWindow;

            if (this.MeasuringManager != null)
                this.MeasuringManager.Finder.FinderButton.FinderButtonState.IsMainWindowTopLevel.IsActive = is_top_level;
        }

        private void OnNirMeasurableStateChanged(object sender, EventArgs e)
        {
            bool is_nir_measurable = (sender as SubstancesCatalog).IsSelectedSubstanceNirMeasurable;

            if (this.MeasuringManager != null)
                this.MeasuringManager.Finder.FinderButton.FinderButtonState.IsSubstanceNirMeasurable.IsActive = is_nir_measurable;
        }


        private void OnSubstanceSelectionStateChanged(object sender, EventArgs e)
        {
            bool is_substance_selected = (sender as SubstancesCatalog).IsSubstanceSelected;
            this.MeasuringManager.Finder.FinderButton.FinderButtonState.IsSubstanceSelected.IsActive = is_substance_selected;
        }

        private void OnDataExchangeSuccessStateChanged(object sender, EventArgs e)
        {
            bool is_data_exchange_not_finished = this.ProtocolManager.DataExchangeSuccess == false;
            this.MeasuringManager.Finder.FinderButton.FinderButtonState.IsDataExchangeNotFinished.IsActive = is_data_exchange_not_finished;
        }

        private bool IsReleaseMode { get; set; } = false;
        public GlobalStates GlobalStates { get; private set; }
    }
}
