// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2022, 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;


namespace Hiperscan.ApoIdentCore.SectionManager.FieldDefinitions
{
    public abstract class InputField<T> : IInputField
    {
        public event EventHandler ValidStateChanged = null;
        public event EventHandler ValueChanged = null;

        protected abstract bool CheckEntry();
        public abstract void SetDefaultValue();
        public abstract string ErrorMessage { get; }
        public abstract int FieldLength { get; }
        protected abstract bool Compare(T value1, T value2);
        public abstract T GetEntryValue();

        protected void Init()
        {
            this.SetDefaultValue();
            this.IsEntryValid = this.CheckEntry();
        }

        protected void CallValidStateChangedEvent()
        {
            if (this.FireEvent)
                this.ValidStateChanged?.Invoke(this, EventArgs.Empty);
        }

        public bool IsEntryValid { get; protected set; }
        public bool FireEvent { private get; set; } = true;

        protected T entry;
        public T Entry
        {
            set
            {
                if (this.Compare(this.entry, value) == true)
                    return;

                this.entry = value;
                bool tmp = this.CheckEntry();
                bool fire_state_event = (tmp != this.IsEntryValid);
                this.IsEntryValid = tmp;

                if (this.FireEvent)
                    this.ValueChanged?.Invoke(this, EventArgs.Empty);

                if (fire_state_event)
                    this.CallValidStateChangedEvent();
            }
        }

        private bool is_visible = true;
        public bool IsVisible
        {
            get
            {
                return this.is_visible;
            }
            set
            {
                if (this.is_visible == value)
                    return;

                this.is_visible = value;

                bool tmp = this.CheckEntry();
                bool fire_state_event = (tmp != this.IsEntryValid);
                this.IsEntryValid = tmp;

                if (this.FireEvent)
                    this.ValueChanged?.Invoke(this, EventArgs.Empty);

                if (fire_state_event)
                    this.CallValidStateChangedEvent();
            }
        }
    }
}
