// 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 Glade;
using Hiperscan.ApoIdentCore.GuiControl;
using Hiperscan.Extensions.ApoIdent.Widgets;
using Hiperscan.SGS;
using Hiperscan.Spectroscopy;
using Hiperscan.Spectroscopy.Method;
using Hiperscan.Unix;


namespace Hiperscan.Extensions.ApoIdent
{
    public partial class ApoIdentApplication
    {
        [Widget] Gtk.Label measuring_section_label = null;

        [Widget] Gtk.Button measuring_start_button = null;
        [Widget] Gtk.Label measuring_advice_label = null;
        [Widget] Gtk.Button measuring_abort_button = null;
        [Widget] Gtk.Label measuring_time_label = null;
        [Widget] Gtk.Label measuring_time_output_label = null;
        [Widget] Gtk.ToggleButton measuring_insert_togglebutton = null;

        private void UpdateMeasuringSection(UpdateArgs update_arg)
        {
            if (update_arg.Measuring.HasFlag(UpdateCategory.Sensitives))
                this.SetMeasuringSensitives();

            if (update_arg.Measuring.HasFlag(UpdateCategory.SetValues))
            {
                this.SetMeasuringButton();
                this.SetMeasuringTimeLabel();
                this.SetMeasuringTimeOutputLabel();
                this.SetMeasuringInsertToggleutton();
            }

            if (update_arg.Measuring.HasFlag(UpdateCategory.SpecialCases))
                this.SetMeasuringTimeOutputLabel(update_arg);
        }

        private void SetMeasuringSensitives()
        {
            bool sensitive = this.Control.GlobalStates.QuitRequested == false &&
                                this.Control.GlobalStates.IsTopLevelWindow == true &&
                                this.Control.GlobalStates.Busy == false &&
                                this.Control.SubstanceManager.IsComplete &&
                                this.Control.ProtocolManager.DataExchangeSuccess == false;

            this.measuring_section_label.Sensitive = sensitive;
            this.measuring_time_label.Sensitive = sensitive;
            this.measuring_time_output_label.Sensitive = sensitive;
            this.MeasuringStartButton.Sensitive = sensitive;
            this.MeasuringStartButton.Sensitive = sensitive;
            this.measuring_insert_togglebutton.Sensitive = sensitive;
        }

        private void SetMeasuringButton()
        {
            this.MeasuringStartButton.CurrentState = this.Control.MeasuringManager.GetMeasuringState();
        }

        private void SetMeasuringTimeLabel()
        {
            this.measuring_time_label.Visible = this.Control.MeasuringManager.IsRecalibrtionTimerActive;
        }

        private void SetMeasuringTimeOutputLabel()
        {
            this.measuring_time_output_label.Visible = this.Control.MeasuringManager.IsRecalibrtionTimerActive;
        }

        private void SetMeasuringTimeOutputLabel(UpdateArgs arg)
        {
            this.measuring_time_output_label.Text = arg.RecalibrationTimeLeft;
        }

        private void SetMeasuringInsertToggleutton()
        {
            if (this.Control.SubstanceManager.IsComplete)
            {
                this.measuring_insert_togglebutton.Visible = this.Control.MeasuringManager.CanMeasureInsert();
                this.measuring_insert_togglebutton.Active = this.Control.MeasuringManager.Finder.IsInsertActive;
            }
            else
            {
                this.measuring_insert_togglebutton.Visible = false;
                this.measuring_insert_togglebutton.Active = false;
            }
        }

        private MeasuringButton MeasuringStartButton { get; set; }
    }
}
