// Created with MonoDevelop
//
//    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.Spectroscopy;
using Hiperscan.Unix;

namespace Hiperscan.SGS.Benchtop
{
    public partial class Finder
    {
        private void SetProbeType(IDevice probe_type_device)
        {
            this.ProbeType = probe_type_device.GetFinderProbeType();
            this.InitAfterSettingProbeType();
        }

        public void SetProbeType(ProbeType probe_type)
        {
            this.ProbeType = probe_type;
            this.InitAfterSettingProbeType();
        }

        private void InitAfterSettingProbeType()
        {
            switch (this.ProbeType)
            {
                case ProbeType.Unknown:
                    this.StandardRecalibrationSet = null;
                    this.InsertRecalibrationSet = null;
                    this.RecalibrationSet = null;
                    this.StandardReferencingSet = null;
                    this.InsertReferencingSet = null;
                    this.ReferencingSet = null;
                    this.SampleSet = null;
                    this.StandardSampleSet = null;
                    this.InsertSampleSet = null;
                    break;

                case ProbeType.FinderInsert:
                case ProbeType.FinderStandard:
                    this.StandardRecalibrationSet = new RecalibrationSet(ProbeType.FinderStandard);
                    this.InsertRecalibrationSet = new RecalibrationSet(ProbeType.FinderInsert);
                    this.StandardReferencingSet = new ReferencingSet(ProbeType.FinderStandard);
                    this.InsertReferencingSet = new ReferencingSet(ProbeType.FinderInsert);
                    this.StandardSampleSet = new SampleSet(ProbeType.FinderStandard);
                    this.InsertSampleSet = new SampleSet(ProbeType.FinderInsert);
                    if (this.ProbeType == ProbeType.FinderStandard)
                    {
                        this.RecalibrationSet = this.StandardRecalibrationSet;
                        this.ReferencingSet = this.StandardReferencingSet;
                        this.SampleSet = StandardSampleSet;
                    }
                    else
                    {
                        this.RecalibrationSet = this.InsertRecalibrationSet;
                        this.ReferencingSet = this.InsertReferencingSet;
                        this.SampleSet = InsertSampleSet;
                    }
                    break;



                case ProbeType.FinderSDInsert:
                case ProbeType.FinderSDStandard:
                    this.StandardRecalibrationSet = new RecalibrationSet(ProbeType.FinderSDStandard);
                    this.InsertRecalibrationSet = new RecalibrationSet(ProbeType.FinderSDInsert);
                    this.StandardReferencingSet = new ReferencingSet(ProbeType.FinderSDStandard);
                    this.InsertReferencingSet = new ReferencingSet(ProbeType.FinderSDInsert);
                    this.StandardSampleSet = new SampleSet(ProbeType.FinderSDStandard);
                    this.InsertSampleSet = new SampleSet(ProbeType.FinderSDInsert);
                    if (this.ProbeType == ProbeType.FinderSDInsert)
                    {
                        this.RecalibrationSet = this.StandardRecalibrationSet;
                        this.ReferencingSet = this.StandardReferencingSet;
                        this.SampleSet = StandardSampleSet;
                    }
                    else
                    {
                        this.RecalibrationSet = this.InsertRecalibrationSet;
                        this.ReferencingSet = this.InsertReferencingSet;
                        this.SampleSet = InsertSampleSet;
                    }
                    break;



                case ProbeType.FinderSDSampleSpinner:
                    this.StandardRecalibrationSet = new RecalibrationSet(ProbeType.FinderSDSampleSpinner);
                    this.RecalibrationSet = this.StandardRecalibrationSet;
                    this.StandardReferencingSet = new ReferencingSet(ProbeType.FinderSDSampleSpinner);
                    this.ReferencingSet = this.StandardReferencingSet;
                    this.StandardSampleSet = new SampleSet(ProbeType.FinderSDSampleSpinner);
                    this.SampleSet = this.StandardSampleSet;
                    break;



                default:
                    throw new Exception(String.Format(Catalog.GetString("Unknown probe type {0}"), this.ProbeType));
            }

            Console.WriteLine("Set Finder probe type to {0}", this.ProbeType);
        }

        public void ChangeProbeType(bool insert)
        {
            if (this.CanInsertMode == false)
                throw new Exception(String.Format(Catalog.GetString("Cannot change probe type {0} to 'Insert'"), this.ProbeType));

            if (this.IsInsertMode == insert)
                return;

            if (insert)
            {
                this.RecalibrationSet = this.InsertRecalibrationSet;
                this.ReferencingSet = this.InsertReferencingSet;
                this.SampleSet = this.InsertSampleSet;
            }
            else
            {
                this.RecalibrationSet = this.StandardRecalibrationSet;
                this.ReferencingSet = this.StandardReferencingSet;
                this.SampleSet = this.StandardSampleSet;
            }

            if (this.SampleSet != null)
                Console.WriteLine("Change Finder probe type to {0}", this.SampleSet.ProbeType);
        }

        public bool IsInsertMode
        {
            get
            {
                if (this.RecalibrationSet == null)
                    return false;

                if (this.RecalibrationSet == this.StandardRecalibrationSet)
                    return false;

                return this.RecalibrationSet == this.InsertRecalibrationSet;
            }
        }

        public bool CanInsertMode
        {
            get
            {
                return this.RecalibrationSet != null;
            }
        }

        //TODO change setter to protected
        public ProbeType ProbeType { get; set; } = ProbeType.Unknown;
    }
}
