// 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.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 big_measuring_label = null;
        [Widget] Gtk.Button measuring_start_button = null;
        [Widget] Gtk.Label measuring_advice_label = null;
        [Widget] Gtk.ToggleButton measuring_insert_togglebutton = null;

        [Widget] Gtk.Button measuring_abort_button = null;
        [Widget] Gtk.Label measuring_time_label = null;
        [Widget] Gtk.Label measuring_time_output_label = null;

        private void UpdateMeasuringSection(string left_time = "")
        {
            bool active = this.Control.QuitRequested == false &&
                this.Control.ConnectionSuccess &&
                 this.SubstanceManagement.CurrentSubstance != null &&
                 this.Control.IsTopLevelWindow == true &&
                              this.Control.Busy == false;

            this.big_measuring_label.Sensitive = active;
            this.measuring_time_label.Sensitive = active;
            this.measuring_time_output_label.Sensitive = active;
            this.MeasuringButton.Sensitive = active && this.Control.Busy == false;

            this.SetMeasuringButton();
            this.SetMeasuringInsertCheckButton(active);
            this.SetMeasuringTimeLabel(active);
            this.SetMeasuringTimeOutputLabel(active, left_time);
        }

        private void SetMeasuringButton()
        {
            this.MeasuringButton.CurrentState = this.Finder?.MeasuringState ?? MeasuringState.None;
        }

        private void SetMeasuringInsertCheckButton(bool active)
        {
            this.measuring_insert_togglebutton.Visible = false;

            if (this.Control.MeasuringProgressState.IsReferencing)
                return;

            if (active == true)
            {
                bool is_es_20 = this.Finder.ProductId == DeviceList.PRODUCT_ID_APOIDENT_ES20 &&
                                    this.SubstanceManagement.CurrentModelClass.SubstanceType == SubstanceType.Solid;

                bool is_can_insert = this.SubstanceManagement.CurrentSubstance.CanInsert;

                bool visible = (is_es_20 || is_can_insert) && this.SubstanceManagement.CurrentModelClass.SubstanceType != SubstanceType.SampleInsertOnly;

                this.measuring_insert_togglebutton.Visible = visible;
                this.measuring_insert_togglebutton.Active = this.Finder.IsInsertActive;
            }

            this.measuring_insert_togglebutton.Sensitive = active && this.Control.Busy == false;
        }

        private void SetMeasuringTimeLabel(bool active)
        {
            if (this.Finder == null || this.Finder.FinderExists == false)
            {
                this.measuring_time_label.Visible = false;
                return;
            }


            this.measuring_time_label.Visible = this.Finder.MeasuringState == MeasuringState.MeasuringFluid;
        }

        private void SetMeasuringTimeOutputLabel(bool active, string time_left)
        {
            if (this.Finder == null || this.Finder.FinderExists == false)
            {
                this.measuring_time_label.Visible = false;
                return;
            }


            this.measuring_time_output_label.Visible = this.Finder.MeasuringState == MeasuringState.MeasuringFluid;

            this.measuring_time_output_label.Text = time_left;
        }

        private MeasuringButton MeasuringButton { get; set; }
    }
}
