using System;
using System.Text;
using QC.QsprTestObjects.Parameters;
using QC.QsprTestObjects.Tests.Base;
using QC.QTMInterface;

namespace QC.QsprTestObjects.TestConditions;

[Serializable]
public class TestPreCondition : BaseCondition
{
	private string testIndex = "";

	public string TestIndex
	{
		get
		{
			return testIndex;
		}
		set
		{
			testIndex = value;
		}
	}

	public override string ConditionString
	{
		get
		{
			StringBuilder stringBuilder = new StringBuilder();
			switch (testConditionType)
			{
			case ConditionType.GlobalVariables:
				stringBuilder.Append(base.GlobalVariableName);
				stringBuilder.Append(' ');
				BaseCondition.AppendSign(stringBuilder, base.GlobalVariableSign);
				stringBuilder.Append(' ');
				if (base.CompareToType == CompareType.Constant)
				{
					stringBuilder.Append(base.GlobalVariableValue);
				}
				else if (base.CompareToType == CompareType.GlobalVariable)
				{
					stringBuilder.Append(base.GVToCompareName);
				}
				break;
			case ConditionType.TestParameters:
			{
				BaseTest baseTest = root.FindTest(testIndex);
				stringBuilder.Append(baseTest.Properties.TestName);
				stringBuilder.Append(':');
				stringBuilder.Append(base.ParameterName);
				if (parameterIndex != -3)
				{
					stringBuilder.Append('[');
					switch (parameterIndex)
					{
					case -2:
						stringBuilder.Append("any");
						break;
					case -1:
						stringBuilder.Append("all");
						break;
					default:
						stringBuilder.Append(parameterIndex);
						break;
					}
					stringBuilder.Append(']');
				}
				stringBuilder.Append(' ');
				BaseCondition.AppendSign(stringBuilder, base.ParameterSign);
				stringBuilder.Append(' ');
				switch (base.CompareToType)
				{
				case CompareType.Constant:
					stringBuilder.Append(base.ParameterValue);
					break;
				case CompareType.GlobalVariable:
					stringBuilder.Append(base.GVToCompareName);
					break;
				case CompareType.LowerLimit:
					stringBuilder.Append("Param LL");
					break;
				case CompareType.UpperLimit:
					stringBuilder.Append("Param UL");
					break;
				}
				break;
			}
			case ConditionType.TestResult:
			{
				BaseTest baseTest = root.FindTest(testIndex);
				stringBuilder.Append("Result of ");
				stringBuilder.Append(baseTest.Properties.TestName);
				stringBuilder.Append(" is ");
				stringBuilder.Append(base.Result.ToString());
				break;
			}
			}
			return stringBuilder.ToString();
		}
	}

	public override bool IsValid
	{
		get
		{
			if (base.IsValid)
			{
				switch (base.TestConditionType)
				{
				case ConditionType.TestParameters:
				case ConditionType.TestResult:
					if (!string.IsNullOrEmpty(testIndex))
					{
						return true;
					}
					break;
				case ConditionType.GlobalVariables:
					return true;
				}
				return false;
			}
			return false;
		}
	}

	public BaseCondition Base => base.Copy(new BaseCondition());

	public override BaseCondition Clone()
	{
		TestPreCondition testPreCondition = new TestPreCondition();
		base.Copy(testPreCondition);
		testPreCondition.testIndex = testIndex;
		testPreCondition.root = root;
		return testPreCondition;
	}

	public override BaseCondition Copy(BaseCondition bCondition)
	{
		if (bCondition is TestPreCondition testPreCondition)
		{
			testPreCondition.TestIndex = TestIndex;
			return base.Copy(testPreCondition);
		}
		return base.Copy(bCondition);
	}

	public override bool Equals(object obj)
	{
		if (!(obj is TestPreCondition))
		{
			return false;
		}
		TestPreCondition testPreCondition = (TestPreCondition)obj;
		if ((testPreCondition.TestConditionType == ConditionType.TestParameters || testPreCondition.TestConditionType == ConditionType.TestResult) && !testPreCondition.TestIndex.Equals(TestIndex))
		{
			return false;
		}
		return base.Equals(obj);
	}

	public override bool IsConditionMet(ParameterCollection parameters, TestResult tResult)
	{
		bool flag = false;
		try
		{
			if (IsValid)
			{
				switch (base.TestConditionType)
				{
				case ConditionType.TestParameters:
				case ConditionType.TestResult:
				{
					BaseTest baseTest = root.FindTest(testIndex);
					TestResult tResult2 = TestResult.Passed;
					if (baseTest.RuntimeAttributes.Exception || baseTest.RuntimeAttributes.Failed || !baseTest.RuntimeAttributes.TestRan)
					{
						tResult2 = TestResult.Failed;
					}
					flag = base.IsConditionMet(baseTest.Properties.Parameters, tResult2);
					break;
				}
				case ConditionType.GlobalVariables:
					flag = base.IsConditionMet(new ParameterCollection(), TestResult.Passed);
					break;
				}
			}
		}
		catch
		{
		}
		return flag;
	}
}
