using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace QC.QTMXmlSchema;

[Serializable]
public class Test
{
	public const double ERROR_VALUE = -9999.0;

	public const long INVALID_TESTCASE_ID = -1L;

	public ParamResult currParam;

	public string TestName;

	public string TestResult;

	public string NodeIndex;

	public string ErrorCode;

	public bool IsLinkedTree;

	public List<string> LinkedFiles;

	public string TestStartTime;

	public string TestStopTime;

	public TimeSpan TestDuration;

	public long TestCaseRID = -1L;

	public string TestCaseName = string.Empty;

	public string BlockedCRNumber;

	public string Comments = string.Empty;

	public string LogFilePath = string.Empty;

	public string LogFileRemotePath = string.Empty;

	public long TestCaseRefRID = -1L;

	public string JIRAID = string.Empty;

	public string CRID = string.Empty;

	public string Warning = string.Empty;

	public string ErrorDesc = string.Empty;

	public string TestPathInTree;

	public string TestStepLogPath;

	public string IRID = string.Empty;

	public string SRID = string.Empty;

	public string JIRACategories = string.Empty;

	public List<Test> ChildTests = new List<Test>();

	public string RealName;

	public bool? LimitsEnforced = null;

	private int _index;

	private static int _nextIndex;

	private LoopValueStruct loop;

	public ArrayList LoopConditions;

	private int? currentLoopIteration;

	private int? maxLoopIteration;

	private ArrayList paramColl;

	public bool LogFilesCopied => !string.IsNullOrEmpty(LogFileRemotePath);

	public string ShortTestPath
	{
		get
		{
			if (TestPathInTree == null)
			{
				return TestPathInTree;
			}
			char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
			string text = TestPathInTree;
			char[] array = invalidFileNameChars;
			foreach (char oldChar in array)
			{
				try
				{
					text = text.Replace(oldChar, '_');
				}
				catch
				{
				}
			}
			if (text.Length <= 20)
			{
				return text;
			}
			return text.Substring(0, 10) + text.Substring(text.Length - 10, 10);
		}
	}

	public int Index
	{
		get
		{
			if (_index == 0)
			{
				_index = ++_nextIndex;
			}
			return _index;
		}
	}

	public LoopValueStruct LoopValues
	{
		get
		{
			return loop;
		}
		set
		{
			loop = value;
		}
	}

	public int? CurrentLoopIteration
	{
		get
		{
			return currentLoopIteration;
		}
		set
		{
			currentLoopIteration = value;
		}
	}

	public int? MaxLoopIteration
	{
		get
		{
			return maxLoopIteration;
		}
		set
		{
			maxLoopIteration = value;
		}
	}

	[XmlElement(Type = typeof(ParamResult))]
	public ArrayList ParamResults
	{
		get
		{
			return paramColl;
		}
		set
		{
			paramColl = value;
		}
	}

	public static void ResetIndex()
	{
		_nextIndex = 0;
	}

	public ParamResult AddParameter(string paramName, string unitOfMeasure, double? paramValue, string upperLimit, string lowerLimit, bool? reportingEnabled, string dataType = null)
	{
		try
		{
			return AddParameterData(paramName, unitOfMeasure, paramValue, upperLimit, lowerLimit, "", "", reportingEnabled, dataType);
		}
		catch (Exception ex)
		{
			throw new Exception("AddParameter failed for ParamName: " + paramName + "; Error: " + ex.Message);
		}
	}

	public ParamResult AddParameter(string paramName, string unitOfMeasure, string charValue, string extHyperLink, bool? reportingEnabled, string dataType = null)
	{
		try
		{
			return AddParameterData(paramName, unitOfMeasure, null, "", "", charValue, extHyperLink, reportingEnabled, dataType);
		}
		catch (Exception ex)
		{
			throw new Exception("AddParameter failed for ParamName: " + paramName + " Error: " + ex.Message);
		}
	}

	public ParamResult AddParameter(string paramName, string unitOfMeasure, string charValue, bool? reportingEnabled, string dataType = null)
	{
		try
		{
			return AddParameterData(paramName, unitOfMeasure, null, "", "", charValue, "", reportingEnabled, dataType);
		}
		catch (Exception ex)
		{
			throw new Exception("AddParameter failed for ParamName: " + paramName + " Error: " + ex.Message);
		}
	}

	public ParamResult AddParameter(string paramName, string unitOfMeasure, string charValue, bool? reportingEnabled, string upperLimit, string lowerLimit, string dataType = null)
	{
		try
		{
			return AddParameterData(paramName, unitOfMeasure, null, upperLimit, lowerLimit, charValue, "", reportingEnabled, dataType);
		}
		catch (Exception ex)
		{
			throw new Exception("AddParameter failed for ParamName: " + paramName + " Error: " + ex.Message);
		}
	}

	private ParamResult AddParameterData(string paramName, string unitOfMeasure, double? paramValue, string upperLimit, string lowerLimit, string charValue, string extHyperLink, bool? reportingEnabled, string dataType = null)
	{
		return AddParameterData(paramName, unitOfMeasure, paramValue, upperLimit, lowerLimit, charValue, extHyperLink, null, reportingEnabled, dataType);
	}

	private ParamResult AddParameterData(string paramName, string unitOfMeasure, double? paramValue, string upperLimit, string lowerLimit, string charValue, string extHyperLink, bool? paramResult, bool? reportingEnabled, string dataType = null)
	{
		try
		{
			if (ParamResults == null)
			{
				ParamResults = new ArrayList();
			}
			ParamResult paramResult2 = new ParamResult();
			paramResult2.Name = paramName;
			paramResult2.UnitOfMeasure = unitOfMeasure;
			paramResult2.CHAR_Value = charValue;
			paramResult2.ExtHyperLink = extHyperLink;
			paramResult2.Value = paramValue;
			paramResult2.UpperLimit = upperLimit;
			paramResult2.LowerLimit = lowerLimit;
			paramResult2.Result = paramResult;
			paramResult2.ReportingEnabled = reportingEnabled;
			paramResult2.DataType = dataType;
			ParamResults.Add(paramResult2);
			currParam = paramResult2;
			return currParam;
		}
		catch (Exception ex)
		{
			throw new Exception(ex.Message);
		}
	}

	public void AddLoopCondition(string loopName, string loopValue)
	{
		AddLoopCondition(loopName, loopValue, isInputParam: false);
	}

	public void AddLoopCondition(string loopName, string loopValue, bool isInputParam)
	{
		try
		{
			if (LoopConditions == null)
			{
				LoopConditions = new ArrayList();
			}
			LoopCondition loopCondition = new LoopCondition();
			loopCondition.Key = loopName;
			loopCondition.Value = loopValue;
			loopCondition.IsInputParam = isInputParam;
			LoopConditions.Add(loopCondition);
		}
		catch (Exception ex)
		{
			throw new Exception("AddLoopConditions failed for LoopName: " + loopName + "; Error: " + ex.Message);
		}
	}
}
