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

namespace Hiperscan.Extensions.ReferenceCheck.Control
{
    public partial class CoreApp
    {
        public event UpdateGuiHandler UpdateGuiNeeded = null;

        public bool CanNext()
        {
            bool can_next = false;

            switch (this.CurrentVisibleView)
            {
                case ViewTypes.Password:
                    can_next = this.HasPasswordEntered;
                    break;

                case ViewTypes.ExecutionMode:
                case ViewTypes.Session:
                    can_next = true;
                    break;

                case ViewTypes.InitDevice:
                    can_next = this.CurrentProgrammState == ProgramState.Waiting;
                    break;

                case ViewTypes.Acquisition:
                    can_next = this.CurrentAcquisitionSet.AcquisitionIsFinished;
                    break;

                case ViewTypes.Restore:
                    can_next = this.RestoreSuccess;
                    break;

                case ViewTypes.Protocol:
                    can_next = this.CanLeaveApplication;
                    break;

                default:
                    break;
            }

            return can_next;
        }

        public bool CanExit()
        {
            if (this.QuitRequested)
                return false;

            bool exit = true;

            switch(this.CurrentVisibleView)
            {
                case ViewTypes.InitDevice:
                    exit = this.CurrentProgrammState != ProgramState.InitDevice;
                    break;

                case ViewTypes.Acquisition:
                    exit = this.CurrentAcquisitionSet.IsAcquiring == false;
                    break;

                case ViewTypes.Restore:
                    exit = this.CurrentProgrammState != ProgramState.Restore;
                    break;

                case ViewTypes.Protocol:
                    exit = this.ProtocolIsBusy == false;
                    break;

                default:
                    break;
            }

            return exit;
        }

        public void SetNextView()
        {
            if (this.CanNext() == false)
                return;

            switch (this.CurrentVisibleView)
            {
                case ViewTypes.Password:
                    this.CheckPassword();
                    break;

                case ViewTypes.ExecutionMode:
                    this.GoToSelectedExecutionMode();
                    break;

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

                case ViewTypes.InitDevice:
                    this.GoToAcqquisitionSection();
                    break;

                case ViewTypes.Acquisition:
                    this.GoToNextAcquisition();
                    break;

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

                case ViewTypes.Protocol:
                    this.TryToExit();
                    break;
  
                default:
                    break;
            }
        }

        private ViewTypes current_visible_view = ViewTypes.None;
        public ViewTypes CurrentVisibleView
        {
            get
            {
                return this.current_visible_view;
            }
            set
            {
                this.current_visible_view = value;

                UpdateArgs arg = new UpdateArgs();
                arg.ViewType = value;
                arg.UpdateCatecory = UpdateCategory.All;
                arg.ViewChanged = true;
                this.UpdateGuiNeeded?.Invoke(arg);
            }
        }

        public ProgramState CurrentProgrammState { get; private set; } = ProgramState.Waiting;

    }
}
