using System.Linq;
using UIShare.UIViewModel;

namespace UIShare.GlobalVariable
{
    /// <summary>
    /// 值限制报警检查辅助类：供各广播器在采样到信号值时统一判断是否超限。
    /// </summary>
    public static class ValueLimitAlarmHelper
    {
        /// <summary>
        /// 根据指定作用域的 ValueLimitList 检查当前值是否超限，并更新报警状态。
        /// </summary>
        /// <param name="scope">作用域名称</param>
        /// <param name="fingerprint">硬件指纹</param>
        /// <param name="methodName">方法名/信号标识</param>
        /// <param name="value">当前采样值</param>
        /// <param name="globalInfo">全局信息</param>
        public static string CheckAlarm(string scope, string fingerprint, string methodName, double value, GlobalInfo globalInfo)
        {
            if (globalInfo?.ConfigDic == null) return "";
            if (!globalInfo.ConfigDic.TryGetValue(scope, out var systemConfig)) return "";
            if (systemConfig.ValueLimitList == null) return "";

            var limit = systemConfig.ValueLimitList.FirstOrDefault(x =>
                x.Fingerprint == fingerprint && x.MethodName == methodName);
            if (limit == null) return "";
             if (value > limit.UpperExtreme)
            {
                limit.IsAlarm = true;
                limit.AlarmSatus = AlarmStatus.超上极限;
            }
            else if (value < limit.LowerExtreme)
            {
                limit.IsAlarm = true;
                limit.AlarmSatus = AlarmStatus.超下极限;
            }
            else if (value > limit.Upper)
            {
                limit.IsAlarm = true;
                limit.AlarmSatus = AlarmStatus.超上限;
            }
            else if (value < limit.Lower)
            {
                limit.IsAlarm = true;
                limit.AlarmSatus = AlarmStatus.超下限;
            }
            else
            {
                limit.IsAlarm = false;
                limit.AlarmSatus = AlarmStatus.未报警;
            }
            return limit.AlarmSatus.ToString();
        }
    }
}
