using System.Collections.Generic;

namespace MECF.Framework.UI.Client.RecipeEditorLib.RecipeModel.Params
{
    public abstract class NumParamBase<TValue> : ParamBaseWithGenericValue<TValue>
    {
        #region Variables

        protected TValue _minimun;
        protected TValue _maximun;

        #endregion

        #region Constructors

        protected NumParamBase()
        {
        }

        protected NumParamBase(TValue initValue) : base(initValue)
        {
        }

        protected NumParamBase(TValue initValue, TValue minimum, TValue maximum) : base(initValue)
        {
            if (minimum.GetType() != typeof(TValue))
                minimum = default;
            
            if (maximum.GetType() != typeof(TValue))
                maximum = default;
            
            _minimun = minimum;
            _maximun = maximum;
        }

        #endregion

        public TValue Minimun
        {
            get => _minimun;
            set => Set(ref _minimun, value);
        }

        
        public TValue Maximun
        {
            get => _maximun;
            set => Set(ref _maximun, value);
        }


         #region Methods

        public override void Validate()
        {
            var ret = Comparer<TValue>.Default.Compare(_minimun, _maximun);

            if (ret > 0)
            {
                IsValid = false;
                ValidationError = "Config error: minimum value is greater than maximum value";
                return;
            }

            if (double.TryParse(Value.ToString(), out var dblValue))
            {
                var retV2Min = Comparer<TValue>.Default.Compare(Value, _minimun);
                var retV2Max = Comparer<TValue>.Default.Compare(Value, _maximun);

                if (retV2Min < 0 || retV2Max > 0)
                {
                    IsValid = false;
                    ValidationError = $"actual value ({_value}) is outside the range of {_minimun} to {_maximun}";
                }
                else
                {
                    IsValid = true;
                    ValidationError = null;
                }
            }
            else
            {
                IsValid = false;
                ValidationError = "Format error: not a number";
            }

        }

        #endregion
    }
}
