using System;

namespace Aitex.Core.RT.IOCore.Interlock.Utils;

public class InterlockLimitRangeDouble : IAnalogInterlockLimitRange<double>
{
    #region Constructors

    public InterlockLimitRangeDouble(string value)
    {
        var strValues = value.Split(':');
        if (strValues.Length == 2
            && double.TryParse(strValues[0], out var min)
            && double.TryParse(strValues[1], out var max))
        {
            Min = min;
            Max = max;
        }
        else
            throw new InvalidCastException($"unable to convert {value} to double range.");
    }
    
    public InterlockLimitRangeDouble(double min, double max)
    {
        Min = min;
        Max = max;
    }

    #endregion

    #region Properties

    public double Min { get; }
    
    public double Max { get; }

    #endregion

    #region Methods

    public virtual bool CheckIsInRange(double currentValue)
    {
        if (double.IsNaN(currentValue))
            return false;

        return currentValue >= Min && currentValue <= Max;
    }

    public override string ToString()
    {
        return $"{Min:F2} - {Max:F2}";
    }

    #endregion
}