// 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.Extensions.ApoIdent.Widgets;
using Hiperscan.Spectroscopy.IdentModule;
using Hiperscan.Spectroscopy.Method;

namespace Hiperscan.Extensions.ApoIdent
{
    public partial class CombinedFinder
    {
        public class AbortToken
        {
            public bool IsAborted { get; set; } = false;
        }

        public MeasuringState GetMeasuringState(SubstanceManagement substance_db)
        {
            if (this.FinderExists == false)
                return MeasuringState.None;

            if (substance_db == null)
                return MeasuringState.None;

            if (substance_db.CurrentModelClass == null)
                return MeasuringState.None;

            if (substance_db.CurrentSubstanceCategory == SubstanceCategory.NotMeasurable)
                return MeasuringState.NotMeasurable;

            if (substance_db.CurrentSubstanceCategory == SubstanceCategory.UserDefined)
                return MeasuringState.NotMeasurable;

            if (substance_db.CurrentModelClass.SubstanceType == SubstanceType.Fluid)
            {
                if (this.StandardFinder.RecalibrationIsPending)
                {
                    if (this.IsFinderSD)
                        return MeasuringState.TransflectanceAu;
                    else
                        return MeasuringState.Transflectance;
                }

                else
                {
                    return MeasuringState.MeasuringFluid;
                }
            }
            else
            {
                if (substance_db.CurrentModelClass.SubstanceType == SubstanceType.SampleInsertOnly || this.IsInsertActive)
                {
                    return MeasuringState.MeasuringInsert;
                }
                else
                {
                    return MeasuringState.MeasuringSolid;
                }
            }
        }

        private void AbortInvokeCalibrationEvent()
        {
            this.AbortCalibrationToken.IsAborted = true;
            this.AbortCalibrationToken = new AbortToken();
        }


        public AbortToken AbortCalibrationToken { get; private set; } = new AbortToken();
        public bool IsInsertActive { get; set; } = false;
        public MeasuringState MeasuringState { get; set; } = MeasuringState.None;
        public bool Wait { get; set; } = false;
    }
}
