// 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 System.Collections.Generic;
using System.Reflection;
using System.Linq;

namespace Hiperscan.SGS.Benchtop.FinderButtonPolling
{
    public abstract class FinderButtonStateTemplate
    {
        internal event EventHandler AllConditionsStateChanged = null;
        public event EventHandler MeasuringConditionStateChanged = null;

        public FinderButtonStateTemplate()
        {
            this.InitAllConditions();
            this.AreMeasuringConditionsActive = this.GetMeasuringConditionState();
            this.ArePollingConditionsActive = this.AreMeasuringConditionsActive && this.HardwareSupport.IsActive;
        }

        public virtual void Reset()
        {
            this.DeviceExists.IsActive = false;
            this.HardwareSupport.IsActive = false;
        }

        private void InitAllConditions()
        {
            foreach (PropertyInfo property in  this.GetType().GetProperties())
            {
                if (property.PropertyType == typeof(FinderButtonStateProperty))
                {
                    FinderButtonStateProperty tmp = (FinderButtonStateProperty)property.GetValue(this);
                    tmp.StateChanged += this.OnStatePropertyChanged;
                    this.AllConditionProperties.Add(tmp);
                }
            }
        }

        private void OnStatePropertyChanged(object sender, EventArgs e)
        {
            /*
            Console.WriteLine("=====================================");
            foreach (PropertyInfo property in this.GetType().GetProperties())
            {
                if (property.PropertyType == typeof(FinderButtonStateProperty))
                {
                    FinderButtonStateProperty tmp = (FinderButtonStateProperty)property.GetValue(this);
                    Console.WriteLine("=============> {0}::{1}", property.Name, tmp.IsActive);
                }
            }
            Console.WriteLine("=====================================");
            */

            this.AreMeasuringConditionsActive = this.GetMeasuringConditionState();
            this.ArePollingConditionsActive = this.AreMeasuringConditionsActive && this.HardwareSupport.IsActive;
        }

        private bool GetMeasuringConditionState()
        {
            return this.AllConditionProperties.Where(x => x != this.HardwareSupport).
                       Any(x => x.IsActive == false) == false;
        }

        private bool are_measuring_conditions_active = false;
        public bool AreMeasuringConditionsActive
        {
            get
            {
                return this.are_measuring_conditions_active;
            }

            protected set
            {
                if (this.are_measuring_conditions_active == value)
                    return;

                this.are_measuring_conditions_active = value;
                this.MeasuringConditionStateChanged?.Invoke(this, EventArgs.Empty);
            }
        }

        private bool are_polling_conditions_active = false;
        public bool ArePollingConditionsActive
        {
            get
            {
                return this.are_polling_conditions_active;
            }

            protected set
            {
                if (this.are_polling_conditions_active == value)
                    return;

                this.are_polling_conditions_active = value;
                this.AllConditionsStateChanged?.Invoke(this, EventArgs.Empty);
            }
        }

        private List<FinderButtonStateProperty> AllConditionProperties { get; set; } = new List<FinderButtonStateProperty>();

        public  FinderButtonStateProperty DeviceExists { get; protected set; } = new FinderButtonStateProperty(false);
        public FinderButtonStateProperty HardwareSupport { get; protected set; } = new FinderButtonStateProperty(false);
        public FinderButtonStateProperty IsNoQuitRequested { get; protected set; } = new FinderButtonStateProperty(true);
        public FinderButtonStateProperty DeviceIsNotAcquiring { get; protected set; } = new FinderButtonStateProperty(true);
        public FinderButtonStateProperty IsMainWindowTopLevel { get; private set; } = new FinderButtonStateProperty(true);
    }
}
