using System;
using System.Collections.Generic;
using Aitex.Core.RT.IOCore.Interlock.DataProvider;
namespace Aitex.Core.RT.IOCore.Interlock.Limits;

public class BoolDataPollLimit : InterlockLimitBase<BoolDataPollProvider, bool>
{
    #region Constructors

    public BoolDataPollLimit(IInterlockLimitDataProvider dataProvider, string value, string tip,
        Dictionary<string, string> cultureTip,bool ignoreReverse) : base(dataProvider, value, tip, cultureTip, ignoreReverse)
    {
        if (bool.TryParse(value, out var limitValue))
            LimitValue = limitValue;
        else
            throw new InvalidCastException($"unable to convert {value} to boolean.");
    }

    #endregion

    #region Properties

    /// <summary>
    /// <inheritdoc cref="InterlockLimitBase{TAccessor,TValue}.CurrentValue"/>
    /// </summary>
    public override bool CurrentValue
    {
        get
        {
            if (DataProvider.GetValue() is bool value)
                return value;

            // 如果Poll不到值，始终返回LimitValue相反的值，避免当前Limit被命中
            return !LimitValue;
        }
    }

    /// <summary>
    /// <inheritdoc cref="InterlockLimitBase{TAccessor,TValue}.LimitReason"/>
    /// </summary>
    public override string LimitReason =>
        $"{DataProvider.Name} = [{(CurrentValue ? "ON" : "OFF")}]{(string.IsNullOrEmpty(Tip) ? "" : $",{Tip}")}";

    /// <summary>
    /// <inheritdoc cref="InterlockLimitBase{TAccessor,TValue}.DaemonReason"/>
    /// </summary>
    public override string DaemonReason =>
        $"{DataProvider.Name} = [{(LimitValue ? "ON" : "OFF")}]{(string.IsNullOrEmpty(Tip) ? "" : $",{Tip}")}";

    #endregion


    /// <summary>
    /// <inheritdoc cref="InterlockLimitBase{TAccessor,TValue}.CheckInRange"/>
    /// </summary>
    /// <returns></returns>
    protected override bool CheckInRange()
    {
        return CurrentValue == LimitValue;
    }
}