// 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 System.Threading;
using System.Threading.Tasks;

namespace Hiperscan.SGS.Benchtop.FinderButtonPolling
{
    public partial class FinderButton<T>
    {
        public event EventHandler MeasurementStarted = null;
        public event EventHandler MeasuringFinished = null;

        public void StartMeasuringFromExtern()
        {
            this.EnterMeasureCycle(false);
        }

        private void EnterMeasureCycle(bool start_by_finder_button)
        {
            bool continue_action = true;
            lock (this.MeasurementSync)
            {
                if (this.MeasurentCycleIsRunning == false)
                {
                    if (start_by_finder_button)
                        continue_action = this.IsButtonPressed();
                }
                else
                {
                    continue_action = false;
                }

                if (continue_action)
                {
                    if (start_by_finder_button)
                        Console.WriteLine("Start measuring by device.");
                    else
                        Console.WriteLine("Start measuring by software.");

                    this.FinderButtonState.DeviceIsNotAcquiring.IsActive = false;
                    this.WaitForPollingEnd();
                    this.MeasurentCycleIsRunning = true;
                }
            }

            if (continue_action)
            {
                Task.Run(() =>
                {
                    Console.WriteLine("FinderButton start measure cycle");
                    this.MeasurementStarted?.Invoke(this, EventArgs.Empty);
                    this.LeaveMeasureCycle();
                });
            }
        }

        public void LeaveMeasureCycle()
        {
            lock (this.MeasurementSync)
            {
                this.AllowRestartPolling();
                this.FinderButtonState.DeviceIsNotAcquiring.IsActive = true;
                this.MeasurentCycleIsRunning = false;
                Console.WriteLine("FinderButton stop measure cycle");
            }

            this.MeasuringFinished?.Invoke(this, EventArgs.Empty);
        }

        private object MeasurementSync { get; set; } = new object();
        private bool MeasurentCycleIsRunning { get; set; } = false;
    }
}
