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

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

/// <summary>
/// 基于AO判断的互锁限制条件。
/// </summary>
internal class DoLimit : InterlockLimitBase<DoValueProvider, bool>
{
	public override bool CurrentValue => (bool)DataProvider.GetValue();

	public override string LimitReason =>
		$"{DataProvider.Name} = [{(CurrentValue ? "ON" : "OFF")}]{(string.IsNullOrEmpty(Tip) ? "" : $",{Tip}")}";

	public override string DaemonReason =>
		$"{DataProvider.Name} = [{(LimitValue ? "ON" : "OFF")}]{(string.IsNullOrEmpty(Tip) ? "" : $",{Tip}")}";

	public DoLimit(DoValueProvider 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.");
	}

	protected override bool CheckInRange()
	{
		return CurrentValue == LimitValue;
	}
}


