using System;
using System.Collections.Generic;
using System.Linq;
using QC.CTDS.DM.DataStructures.LimitDescriptorDataStructure;

namespace QC.CTDS.DM.DataStructures.InputDataStructure;

public class OutputParamInfo : ParamInfo
{
	private double minLimit = double.NaN;

	private double maxLimit = double.NaN;

	private Limits customLimits;

	private bool paramLimitViolated;

	public double MinLimit
	{
		get
		{
			return minLimit;
		}
		set
		{
			minLimit = value;
		}
	}

	public double MaxLimit
	{
		get
		{
			return maxLimit;
		}
		set
		{
			maxLimit = value;
		}
	}

	public Limits CustomLimits
	{
		get
		{
			return customLimits;
		}
		set
		{
			customLimits = value;
		}
	}

	public OutputParamInfo()
	{
	}

	public OutputParamInfo(ParamInfo paramInfo)
	{
		base.Val = paramInfo.Val;
		base.StrVal = paramInfo.StrVal;
		base.ParamUnit = paramInfo.ParamUnit;
	}

	public void SaveParamFailedState(List<LimitGroupType> customLimitsType = null)
	{
		if (customLimitsType != null && customLimitsType.Count > 0)
		{
			if (CustomLimits != null)
			{
				List<Tuple<string, string, string>> list = new List<Tuple<string, string, string>>();
				List<string> uniqueLims = new List<string>();
				if (CustomLimits.HighLimit != null && CustomLimits.HighLimit.Count > 0)
				{
					uniqueLims.AddRange(CustomLimits.HighLimit.Select((Limit h) => h.Index));
				}
				if (CustomLimits.LowLimit != null && CustomLimits.LowLimit.Count > 0)
				{
					uniqueLims.AddRange(CustomLimits.LowLimit.Select((Limit l) => l.Index).Except(uniqueLims));
				}
				if (uniqueLims != null && uniqueLims.Count > 0)
				{
					int k;
					for (k = 0; k < uniqueLims.Count; k++)
					{
						string item = string.Empty;
						string item2 = string.Empty;
						if (CustomLimits.HighLimit != null && CustomLimits.HighLimit.Count > 0)
						{
							IEnumerable<string> enumerable = from lim1 in CustomLimits.HighLimit
								where lim1.Index == uniqueLims[k]
								select lim1 into lim
								select lim.Val;
							if (enumerable != null && enumerable.Count() > 0)
							{
								item2 = enumerable.First();
							}
						}
						if (CustomLimits.LowLimit != null && CustomLimits.LowLimit.Count > 0)
						{
							IEnumerable<string> enumerable2 = from lim1 in CustomLimits.LowLimit
								where lim1.Index == uniqueLims[k]
								select lim1 into lim
								select lim.Val;
							if (enumerable2 != null && enumerable2.Count() > 0)
							{
								item = enumerable2.First();
							}
						}
						list.Add(new Tuple<string, string, string>(uniqueLims[k], item, item2));
					}
				}
				if (list == null || list.Count <= 0)
				{
					return;
				}
				for (int i = 0; i < list.Count(); i++)
				{
					double result = double.NaN;
					double result2 = double.NaN;
					if (!string.IsNullOrEmpty(list[i].Item2))
					{
						double.TryParse(list[i].Item2, out result);
					}
					if (!string.IsNullOrEmpty(list[i].Item3))
					{
						double.TryParse(list[i].Item3, out result2);
					}
					VerifyLimitViolation(result, result2, base.Val);
				}
			}
			else
			{
				VerifyLimitViolation(MinLimit, MaxLimit, base.Val);
			}
		}
		else
		{
			VerifyLimitViolation(MinLimit, MaxLimit, base.Val);
		}
	}

	private void VerifyLimitViolation(double minVal, double maxVal, double actualVal)
	{
		if (double.IsNaN(actualVal))
		{
			paramLimitViolated = false;
		}
		else if (!double.IsNaN(minVal) && !double.IsNaN(maxVal))
		{
			if (actualVal >= minVal && actualVal <= maxVal)
			{
				paramLimitViolated = false;
			}
			else
			{
				paramLimitViolated = true;
			}
		}
		else if (!double.IsNaN(minVal))
		{
			if (actualVal >= minVal)
			{
				paramLimitViolated = false;
			}
			else
			{
				paramLimitViolated = true;
			}
		}
		else if (!double.IsNaN(maxVal))
		{
			if (actualVal <= maxVal)
			{
				paramLimitViolated = false;
			}
			else
			{
				paramLimitViolated = true;
			}
		}
		else
		{
			paramLimitViolated = false;
		}
	}

	public override bool HasParamFailed()
	{
		return paramLimitViolated;
	}
}
