using System;
using System.ComponentModel;
using System.Globalization;
using System.Text;
using System.Xml.Serialization;
using QC.GlobalVariableManager;
using QC.QsprTestObjects.Loops;
using QC.QsprTestObjects.Parameters;
using QC.QsprTestObjects.Tests.Base;
using QC.QTMInterface;

namespace QC.QsprTestObjects.TestConditions;

[Serializable]
[XmlInclude(typeof(TestPreCondition))]
[XmlInclude(typeof(TestPostCondition))]
[XmlInclude(typeof(ScenarioCondition))]
public class BaseCondition
{
	public const int ANY_PARAM_INDEX = -2;

	public const int ALL_PARAM_INDEX = -1;

	public const int NO_PARAM_INDEX = -3;

	protected ConditionType testConditionType = ConditionType.None;

	protected string baseConditionName = "";

	private ConditionSign globalVariableSign;

	protected ConditionSign parameterSign;

	protected string parameterValue = "";

	protected TestResult result;

	protected int parameterIndex = -3;

	protected CompareType compareToType;

	[NonSerialized]
	[XmlIgnore]
	public RootTest root;

	private string globalVariableName = "";

	private string globalVariableValue = "";

	protected string parameterName = "";

	protected string gvToCompareName = "";

	public string Name
	{
		get
		{
			return baseConditionName;
		}
		set
		{
			baseConditionName = value;
		}
	}

	[DefaultValue("")]
	public string GlobalVariableName
	{
		get
		{
			return globalVariableName;
		}
		set
		{
			globalVariableName = value;
		}
	}

	[DefaultValue("")]
	public string GlobalVariableValue
	{
		get
		{
			return globalVariableValue.Trim();
		}
		set
		{
			globalVariableValue = value;
		}
	}

	public ConditionSign GlobalVariableSign
	{
		get
		{
			return globalVariableSign;
		}
		set
		{
			globalVariableSign = value;
		}
	}

	public ConditionType TestConditionType
	{
		get
		{
			return testConditionType;
		}
		set
		{
			testConditionType = value;
		}
	}

	[DefaultValue("")]
	public string ParameterName
	{
		get
		{
			return parameterName;
		}
		set
		{
			parameterName = value;
		}
	}

	public ConditionSign ParameterSign
	{
		get
		{
			return parameterSign;
		}
		set
		{
			parameterSign = value;
		}
	}

	public string ParameterValue
	{
		get
		{
			return parameterValue.Trim();
		}
		set
		{
			parameterValue = value;
		}
	}

	[DefaultValue("")]
	public string GVToCompareName
	{
		get
		{
			return gvToCompareName.Trim();
		}
		set
		{
			gvToCompareName = value;
		}
	}

	public CompareType CompareToType
	{
		get
		{
			return compareToType;
		}
		set
		{
			compareToType = value;
		}
	}

	public virtual string ConditionString
	{
		get
		{
			new StringBuilder();
			return testConditionType switch
			{
				ConditionType.GlobalVariables => "Global Variable Condition", 
				ConditionType.TestParameters => "Test Parameters Condition", 
				ConditionType.TestResult => "Test Result Condition", 
				_ => "", 
			};
		}
	}

	public TestResult Result
	{
		get
		{
			return result;
		}
		set
		{
			result = value;
		}
	}

	public virtual bool IsValid
	{
		get
		{
			bool flag = false;
			try
			{
				switch (testConditionType)
				{
				case ConditionType.TestParameters:
					flag = parameterName.Trim().Length != 0 && ((CompareToType != CompareType.GlobalVariable || !(GVToCompareName == "")) ? true : false);
					break;
				case ConditionType.TestResult:
					flag = true;
					break;
				case ConditionType.GlobalVariables:
					flag = globalVariableName.Trim().Length != 0 && (CompareToType == CompareType.Constant || CompareToType == CompareType.GlobalVariable) && ((CompareToType != CompareType.GlobalVariable || !(GVToCompareName == "")) ? true : false);
					break;
				}
			}
			catch
			{
			}
			return flag;
		}
	}

	public int ParameterIndex
	{
		get
		{
			return parameterIndex;
		}
		set
		{
			parameterIndex = value;
		}
	}

	public virtual bool IsConditionMet(ParameterCollection parameters, TestResult tResult)
	{
		bool flag = false;
		try
		{
			if (IsValid)
			{
				switch (TestConditionType)
				{
				case ConditionType.TestParameters:
					foreach (ParameterCls parameter in parameters)
					{
						if (!(parameter.Name.Trim() == parameterName.Trim()))
						{
							continue;
						}
						if (parameter.NoOfElements == 1)
						{
							flag = ((CompareToType != CompareType.GlobalVariable) ? CompareParamToValue(parameter, parameter.Value, 0) : CompareParamToGV(parameter.DataType, parameter.Value));
							continue;
						}
						string[] array = parameter.Value.Split(',');
						switch (ParameterIndex)
						{
						case -2:
						{
							bool flag2 = false;
							for (int j = 0; j < array.Length; j++)
							{
								if ((CompareToType != CompareType.GlobalVariable) ? CompareParamToValue(parameter, array[j], j) : CompareParamToGV(parameter.DataType, array[j]))
								{
									flag = true;
									break;
								}
							}
							break;
						}
						case -1:
						{
							for (int i = 0; i < array.Length; i++)
							{
								flag = ((CompareToType != CompareType.GlobalVariable) ? CompareParamToValue(parameter, array[i], i) : CompareParamToGV(parameter.DataType, array[i]));
								if (!flag)
								{
									break;
								}
							}
							break;
						}
						default:
							flag = ((CompareToType != CompareType.GlobalVariable) ? CompareParamToValue(parameter, array[ParameterIndex], ParameterIndex) : CompareParamToGV(parameter.DataType, array[ParameterIndex]));
							break;
						}
					}
					break;
				case ConditionType.TestResult:
					if (tResult == Result)
					{
						flag = true;
					}
					break;
				case ConditionType.GlobalVariables:
					flag = CompareToType switch
					{
						CompareType.GlobalVariable => CompareGVToGV(), 
						CompareType.Constant => CompareGVToValue(), 
						_ => throw new Exception("Invalid CompareType for ConditionType.GlobalVariables in BaseCondition.IsConditionMet."), 
					};
					break;
				}
			}
		}
		catch
		{
		}
		return flag;
	}

	public virtual BaseCondition Copy(BaseCondition bCondition)
	{
		bCondition.Name = Name;
		bCondition.TestConditionType = TestConditionType;
		bCondition.ParameterName = ParameterName;
		bCondition.ParameterValue = ParameterValue;
		bCondition.ParameterIndex = ParameterIndex;
		bCondition.ParameterSign = ParameterSign;
		bCondition.Result = Result;
		bCondition.globalVariableName = globalVariableName;
		bCondition.globalVariableSign = globalVariableSign;
		bCondition.globalVariableValue = globalVariableValue;
		bCondition.gvToCompareName = gvToCompareName;
		bCondition.compareToType = compareToType;
		return bCondition;
	}

	public virtual BaseCondition Clone()
	{
		BaseCondition baseCondition = new BaseCondition();
		baseCondition.root = root;
		baseCondition.Name = Name;
		baseCondition.TestConditionType = TestConditionType;
		baseCondition.ParameterName = ParameterName;
		baseCondition.ParameterValue = ParameterValue;
		baseCondition.ParameterIndex = ParameterIndex;
		baseCondition.ParameterSign = ParameterSign;
		baseCondition.Result = Result;
		baseCondition.globalVariableName = globalVariableName;
		baseCondition.globalVariableSign = globalVariableSign;
		baseCondition.globalVariableValue = globalVariableValue;
		baseCondition.gvToCompareName = gvToCompareName;
		baseCondition.compareToType = compareToType;
		return baseCondition;
	}

	public override string ToString()
	{
		return baseConditionName;
	}

	public override bool Equals(object obj)
	{
		if (!(obj is BaseCondition))
		{
			return false;
		}
		BaseCondition baseCondition = (BaseCondition)obj;
		if (!baseCondition.Name.Equals(Name))
		{
			return false;
		}
		if (baseCondition.TestConditionType != TestConditionType)
		{
			return false;
		}
		switch (TestConditionType)
		{
		case ConditionType.GlobalVariables:
			if (baseCondition.GlobalVariableName != GlobalVariableName || baseCondition.GlobalVariableSign != GlobalVariableSign || baseCondition.CompareToType != CompareToType)
			{
				return false;
			}
			if (CompareToType == CompareType.Constant && baseCondition.GlobalVariableValue != GlobalVariableValue)
			{
				return false;
			}
			if (CompareToType == CompareType.GlobalVariable && baseCondition.GVToCompareName != GVToCompareName)
			{
				return false;
			}
			break;
		case ConditionType.TestParameters:
			if (baseCondition.ParameterIndex != ParameterIndex || !baseCondition.ParameterName.Equals(ParameterName) || baseCondition.ParameterSign != ParameterSign || baseCondition.CompareToType != CompareToType)
			{
				return false;
			}
			if (CompareToType == CompareType.Constant && baseCondition.ParameterValue != ParameterValue)
			{
				return false;
			}
			if (CompareToType == CompareType.GlobalVariable && baseCondition.GVToCompareName != GVToCompareName)
			{
				return false;
			}
			break;
		case ConditionType.TestResult:
			if (baseCondition.Result != Result)
			{
				return false;
			}
			break;
		}
		return true;
	}

	protected static void AppendSign(StringBuilder sb, ConditionSign sign)
	{
		switch (sign)
		{
		case ConditionSign.Equals:
			sb.Append('=');
			break;
		case ConditionSign.GreaterThan:
			sb.Append('>');
			break;
		case ConditionSign.GreaterThanEqual:
			sb.Append(">=");
			break;
		case ConditionSign.LessThan:
			sb.Append('<');
			break;
		case ConditionSign.LessThanEqual:
			sb.Append("<=");
			break;
		case ConditionSign.NotEqual:
			sb.Append("!=");
			break;
		}
	}

	private bool CompareParamToValue(ParameterCls param, string paramValue, int index)
	{
		bool flag = false;
		bool flag2 = false;
		DataType dataType = param.DataType;
		string text = "";
		try
		{
			if (CompareToType == CompareType.LowerLimit)
			{
				text = ((param.NoOfElements <= 1) ? param.LowerLimit : param.LowerLimit.Split(',')[index]);
			}
			if (CompareToType == CompareType.UpperLimit)
			{
				text = ((param.NoOfElements <= 1) ? param.UpperLimit : param.UpperLimit.Split(',')[index]);
			}
			switch (dataType)
			{
			case DataType.Int:
			case DataType.Hex:
			{
				int num3 = 0;
				int num4 = 0;
				num3 = int.Parse(paramValue);
				switch (CompareToType)
				{
				case CompareType.Constant:
					num4 = ((dataType != DataType.Hex) ? int.Parse(ParameterValue) : int.Parse(ParameterValue, NumberStyles.HexNumber));
					break;
				case CompareType.LowerLimit:
				case CompareType.UpperLimit:
					flag2 = ((dataType != DataType.Hex) ? (!int.TryParse(text, out num4)) : (!int.TryParse(text, NumberStyles.HexNumber, null, out num4)));
					break;
				default:
					throw new Exception("Invalid CompareType passed to BaseCondition.CompareParamToValue");
				}
				if (flag2)
				{
					break;
				}
				switch (ParameterSign)
				{
				case ConditionSign.Equals:
					if (num4 == num3)
					{
						flag = true;
					}
					break;
				case ConditionSign.NotEqual:
					if (num4 != num3)
					{
						flag = true;
					}
					break;
				case ConditionSign.LessThan:
					if (num4 > num3)
					{
						flag = true;
					}
					break;
				case ConditionSign.GreaterThan:
					if (num4 < num3)
					{
						flag = true;
					}
					break;
				case ConditionSign.GreaterThanEqual:
					if (num4 <= num3)
					{
						flag = true;
					}
					break;
				case ConditionSign.LessThanEqual:
					if (num4 >= num3)
					{
						flag = true;
					}
					break;
				}
				break;
			}
			case DataType.Double:
			{
				double num = double.Parse(paramValue, CultureInfo.InvariantCulture);
				double num2 = 0.0;
				switch (CompareToType)
				{
				case CompareType.Constant:
					num2 = double.Parse(ParameterValue, CultureInfo.InvariantCulture);
					break;
				case CompareType.LowerLimit:
				case CompareType.UpperLimit:
					flag2 = !double.TryParse(text, out num2);
					break;
				default:
					throw new Exception("Invalid CompareType passed to BaseCondition.CompareParamToValue");
				}
				if (flag2)
				{
					break;
				}
				switch (ParameterSign)
				{
				case ConditionSign.Equals:
					if (num2 == num)
					{
						flag = true;
					}
					break;
				case ConditionSign.NotEqual:
					if (num2 != num)
					{
						flag = true;
					}
					break;
				case ConditionSign.LessThan:
					if (num2 > num)
					{
						flag = true;
					}
					break;
				case ConditionSign.GreaterThan:
					if (num2 < num)
					{
						flag = true;
					}
					break;
				case ConditionSign.GreaterThanEqual:
					if (num2 <= num)
					{
						flag = true;
					}
					break;
				case ConditionSign.LessThanEqual:
					if (num2 >= num)
					{
						flag = true;
					}
					break;
				}
				break;
			}
			case DataType.Byte:
			{
				byte b = byte.Parse(paramValue);
				byte b2 = 0;
				switch (CompareToType)
				{
				case CompareType.Constant:
					b2 = byte.Parse(ParameterValue);
					break;
				case CompareType.LowerLimit:
				case CompareType.UpperLimit:
					flag2 = !byte.TryParse(text, out b2);
					break;
				default:
					throw new Exception("Invalid CompareType passed to BaseCondition.CompareParamToValue");
				}
				if (flag2)
				{
					break;
				}
				switch (ParameterSign)
				{
				case ConditionSign.Equals:
					if (b2 == b)
					{
						flag = true;
					}
					break;
				case ConditionSign.NotEqual:
					if (b2 != b)
					{
						flag = true;
					}
					break;
				case ConditionSign.LessThan:
					if (b2 > b)
					{
						flag = true;
					}
					break;
				case ConditionSign.GreaterThan:
					if (b2 < b)
					{
						flag = true;
					}
					break;
				case ConditionSign.GreaterThanEqual:
					if (b2 <= b)
					{
						flag = true;
					}
					break;
				case ConditionSign.LessThanEqual:
					if (b2 >= b)
					{
						flag = true;
					}
					break;
				}
				break;
			}
			default:
				flag2 = true;
				break;
			}
			if (flag2)
			{
				string text2 = paramValue.Trim().ToUpper();
				string text3 = "";
				switch (CompareToType)
				{
				case CompareType.Constant:
					text3 = ParameterValue.Trim().ToUpper();
					break;
				case CompareType.LowerLimit:
				case CompareType.UpperLimit:
					text3 = text.Trim().ToUpper();
					break;
				default:
					return false;
				}
				switch (ParameterSign)
				{
				case ConditionSign.Equals:
					if (text3 == text2)
					{
						flag = true;
					}
					break;
				case ConditionSign.NotEqual:
					if (text3 != text2)
					{
						flag = true;
					}
					break;
				case ConditionSign.LessThan:
					if (text3.CompareTo(text2) > 0)
					{
						flag = true;
					}
					break;
				case ConditionSign.GreaterThan:
					if (text3.CompareTo(text2) < 0)
					{
						flag = true;
					}
					break;
				case ConditionSign.GreaterThanEqual:
					if (text3.CompareTo(text2) <= 0)
					{
						flag = true;
					}
					break;
				case ConditionSign.LessThanEqual:
					if (text3.CompareTo(text2) >= 0)
					{
						flag = true;
					}
					break;
				}
			}
		}
		catch
		{
		}
		return flag;
	}

	private bool CompareParamToGV(DataType dType, string paramValue)
	{
		bool flag = false;
		bool flag2 = true;
		try
		{
			if (QTMGlobalVariable.GetGlobalVariable(GVToCompareName, out var gvValue, root.Scheduler.Env.SchedulerID))
			{
				if (double.TryParse(gvValue, out var num))
				{
					switch (dType)
					{
					case DataType.Int:
					case DataType.Hex:
					{
						int num3 = int.Parse(paramValue);
						switch (ParameterSign)
						{
						case ConditionSign.Equals:
							if (num == (double)num3)
							{
								flag = true;
							}
							break;
						case ConditionSign.NotEqual:
							if (num != (double)num3)
							{
								flag = true;
							}
							break;
						case ConditionSign.LessThan:
							if (num > (double)num3)
							{
								flag = true;
							}
							break;
						case ConditionSign.GreaterThan:
							if (num < (double)num3)
							{
								flag = true;
							}
							break;
						case ConditionSign.GreaterThanEqual:
							if (num <= (double)num3)
							{
								flag = true;
							}
							break;
						case ConditionSign.LessThanEqual:
							if (num >= (double)num3)
							{
								flag = true;
							}
							break;
						}
						break;
					}
					case DataType.Double:
					{
						double num2 = double.Parse(paramValue, CultureInfo.InvariantCulture);
						switch (ParameterSign)
						{
						case ConditionSign.Equals:
							if (num == num2)
							{
								flag = true;
							}
							break;
						case ConditionSign.NotEqual:
							if (num != num2)
							{
								flag = true;
							}
							break;
						case ConditionSign.LessThan:
							if (num > num2)
							{
								flag = true;
							}
							break;
						case ConditionSign.GreaterThan:
							if (num < num2)
							{
								flag = true;
							}
							break;
						case ConditionSign.GreaterThanEqual:
							if (num <= num2)
							{
								flag = true;
							}
							break;
						case ConditionSign.LessThanEqual:
							if (num >= num2)
							{
								flag = true;
							}
							break;
						}
						break;
					}
					case DataType.Byte:
					{
						byte b = byte.Parse(paramValue);
						switch (ParameterSign)
						{
						case ConditionSign.Equals:
							if (num == (double)(int)b)
							{
								flag = true;
							}
							break;
						case ConditionSign.NotEqual:
							if (num != (double)(int)b)
							{
								flag = true;
							}
							break;
						case ConditionSign.LessThan:
							if (num > (double)(int)b)
							{
								flag = true;
							}
							break;
						case ConditionSign.GreaterThan:
							if (num < (double)(int)b)
							{
								flag = true;
							}
							break;
						case ConditionSign.GreaterThanEqual:
							if (num <= (double)(int)b)
							{
								flag = true;
							}
							break;
						case ConditionSign.LessThanEqual:
							if (num >= (double)(int)b)
							{
								flag = true;
							}
							break;
						}
						break;
					}
					default:
						flag2 = false;
						break;
					}
				}
				else
				{
					flag2 = false;
				}
				if (!flag2)
				{
					string text = paramValue.Trim().ToUpper();
					switch (ParameterSign)
					{
					case ConditionSign.Equals:
						if (gvValue.Trim().ToUpper() == text)
						{
							flag = true;
						}
						break;
					case ConditionSign.NotEqual:
						if (gvValue.Trim().ToUpper() != text)
						{
							flag = true;
						}
						break;
					case ConditionSign.LessThan:
						if (gvValue.Trim().ToUpper().CompareTo(text) > 0)
						{
							flag = true;
						}
						break;
					case ConditionSign.GreaterThan:
						if (gvValue.Trim().ToUpper().CompareTo(text) < 0)
						{
							flag = true;
						}
						break;
					case ConditionSign.GreaterThanEqual:
						if (gvValue.Trim().ToUpper().CompareTo(text) <= 0)
						{
							flag = true;
						}
						break;
					case ConditionSign.LessThanEqual:
						if (gvValue.Trim().ToUpper().CompareTo(text) >= 0)
						{
							flag = true;
						}
						break;
					}
				}
			}
		}
		catch
		{
		}
		return flag;
	}

	private bool CompareGVToValue()
	{
		bool flag = false;
		if (QTMGlobalVariable.GetGlobalVariable(GlobalVariableName, out var gvValue, root.Scheduler.Env.SchedulerID))
		{
			double num = -1.0;
			double num2 = -1.0;
			bool flag2 = true;
			try
			{
				num = double.Parse(gvValue, CultureInfo.InvariantCulture);
			}
			catch
			{
				flag2 = false;
			}
			if (flag2)
			{
				try
				{
					num2 = double.Parse(GlobalVariableValue, CultureInfo.InvariantCulture);
				}
				catch
				{
					flag2 = false;
				}
			}
			if (flag2)
			{
				switch (GlobalVariableSign)
				{
				case ConditionSign.Equals:
					if (num2 == num)
					{
						flag = true;
					}
					break;
				case ConditionSign.NotEqual:
					if (num2 != num)
					{
						flag = true;
					}
					break;
				case ConditionSign.LessThan:
					if (num2 > num)
					{
						flag = true;
					}
					break;
				case ConditionSign.GreaterThan:
					if (num2 < num)
					{
						flag = true;
					}
					break;
				case ConditionSign.GreaterThanEqual:
					if (num2 <= num)
					{
						flag = true;
					}
					break;
				case ConditionSign.LessThanEqual:
					if (num2 >= num)
					{
						flag = true;
					}
					break;
				}
			}
			else
			{
				gvValue = gvValue.ToUpper().Trim();
				switch (GlobalVariableSign)
				{
				case ConditionSign.Equals:
					if (gvValue.CompareTo(GlobalVariableValue.ToUpper().Trim()) == 0)
					{
						flag = true;
					}
					break;
				case ConditionSign.NotEqual:
					if (gvValue.CompareTo(GlobalVariableValue.ToUpper().Trim()) != 0)
					{
						flag = true;
					}
					break;
				case ConditionSign.LessThan:
					if (gvValue.CompareTo(GlobalVariableValue.ToUpper().Trim()) < 0)
					{
						flag = true;
					}
					break;
				case ConditionSign.GreaterThan:
					if (gvValue.CompareTo(GlobalVariableValue.ToUpper().Trim()) > 0)
					{
						flag = true;
					}
					break;
				case ConditionSign.GreaterThanEqual:
					if (gvValue.CompareTo(GlobalVariableValue.ToUpper().Trim()) >= 0)
					{
						flag = true;
					}
					break;
				case ConditionSign.LessThanEqual:
					if (gvValue.CompareTo(GlobalVariableValue.ToUpper().Trim()) <= 0)
					{
						flag = true;
					}
					break;
				}
			}
		}
		return flag;
	}

	private bool CompareGVToGV()
	{
		bool flag = false;
		if (QTMGlobalVariable.GetGlobalVariable(GlobalVariableName, out var gvValue, root.Scheduler.Env.SchedulerID) && QTMGlobalVariable.GetGlobalVariable(GVToCompareName, out var gvValue2, root.Scheduler.Env.SchedulerID))
		{
			double num = -1.0;
			double num2 = -1.0;
			bool flag2 = true;
			if (!double.TryParse(gvValue, out num))
			{
				flag2 = false;
			}
			if (!double.TryParse(gvValue2, out num2))
			{
				flag2 = false;
			}
			if (flag2)
			{
				switch (GlobalVariableSign)
				{
				case ConditionSign.Equals:
					if (num2 == num)
					{
						flag = true;
					}
					break;
				case ConditionSign.NotEqual:
					if (num2 != num)
					{
						flag = true;
					}
					break;
				case ConditionSign.LessThan:
					if (num2 > num)
					{
						flag = true;
					}
					break;
				case ConditionSign.GreaterThan:
					if (num2 < num)
					{
						flag = true;
					}
					break;
				case ConditionSign.GreaterThanEqual:
					if (num2 <= num)
					{
						flag = true;
					}
					break;
				case ConditionSign.LessThanEqual:
					if (num2 >= num)
					{
						flag = true;
					}
					break;
				}
			}
			else
			{
				gvValue = gvValue.ToUpper().Trim();
				gvValue2 = gvValue2.ToUpper().Trim();
				switch (GlobalVariableSign)
				{
				case ConditionSign.Equals:
					if (gvValue.CompareTo(gvValue2) == 0)
					{
						flag = true;
					}
					break;
				case ConditionSign.NotEqual:
					if (gvValue.CompareTo(gvValue2) != 0)
					{
						flag = true;
					}
					break;
				case ConditionSign.LessThan:
					if (gvValue.CompareTo(gvValue2) < 0)
					{
						flag = true;
					}
					break;
				case ConditionSign.GreaterThan:
					if (gvValue.CompareTo(gvValue2) > 0)
					{
						flag = true;
					}
					break;
				case ConditionSign.GreaterThanEqual:
					if (gvValue.CompareTo(gvValue2) >= 0)
					{
						flag = true;
					}
					break;
				case ConditionSign.LessThanEqual:
					if (gvValue.CompareTo(gvValue2) <= 0)
					{
						flag = true;
					}
					break;
				}
			}
		}
		return flag;
	}
}
