using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using QC.QsprTestObjects;
using QC.QsprTestObjects.Parameters;
using QC.QsprTestObjects.Registry;
using QC.QsprTestObjects.Tests.Base;
using QC.QsprTestObjects.Tests.DotNet;
using QC.QsprTestObjects.Tests.Hive;
using QC.QsprTestObjects.Tests.LabVIEW;
using QC.QsprTestObjects.Tests.Script;
using QC.QsprTestObjects.Tests.TestCase;
using QC.QTMInterface;

namespace QC.QSPRSchedulerWrapper;

[ClassInterface(ClassInterfaceType.None)]
[ProgId("QSPR.TestNode")]
[ComVisible(true)]
public class QSPRTestNode : ITestNode
{
	private const string DEFAULT_FOLDERNAME = "Folder";

	private string testIndex;

	private QSPRTestTree testTree;

	private string lastCallInfo = "";

	private string lastError = "No Error";

	private bool errorsExist;

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

	internal RootTest Root => testTree.root;

	internal Scheduler Scheduler => testTree.scheduler;

	internal QSPRTestTree TestTree
	{
		get
		{
			return testTree;
		}
		set
		{
			testTree = value;
		}
	}

	public string LastError => lastError;

	public int ErrorsExist
	{
		get
		{
			if (errorsExist)
			{
				return 1;
			}
			return 0;
		}
	}

	public int Checked
	{
		get
		{
			try
			{
				lastCallInfo = "Checked";
				if (testIndex == null)
				{
					lastCallInfo = "Checked called for invalid TestNode object.";
					lastError = "Checked failed, the testNode object is not valid";
					errorsExist = true;
					return 0;
				}
				BaseTest baseTest = Root.FindTest(testIndex);
				if (baseTest != null)
				{
					CheckState checkStateTest = baseTest.Properties.CheckStateTest;
					if (checkStateTest == CheckState.Checked || checkStateTest == CheckState.Indeterminate)
					{
						return 1;
					}
					return 0;
				}
				lastError = "Getting checked state failed: The testnode does not exist in the tree anymore.";
				errorsExist = true;
				return 0;
			}
			catch (Exception ex)
			{
				lastError = "Checked failed: " + ex.ToString();
				errorsExist = true;
				return 0;
			}
		}
		set
		{
			lastCallInfo = "Checked = " + value;
			if (testIndex == null)
			{
				lastCallInfo = "Checked called for invalid TestNode object.";
				lastError = "Checked failed, the testNode object is not valid";
				errorsExist = true;
				return;
			}
			try
			{
				BaseTest baseTest = Root.FindTest(testIndex);
				if (baseTest != null)
				{
					if (value == 1)
					{
						baseTest.Properties.CheckStateTest = CheckState.Checked;
					}
					else
					{
						baseTest.Properties.CheckStateTest = CheckState.Unchecked;
					}
					Scheduler.GlobalStatus.RegistrationStatus.TreeSelectionChanged = true;
				}
			}
			catch (Exception ex)
			{
				lastError = "SetTestChecked failed, " + ex.ToString();
				errorsExist = true;
			}
		}
	}

	public QSPRTestNode AddTest(string realName, string newTestName)
	{
		if (testIndex == null)
		{
			lastCallInfo = "TestNode object is not valid. The index of the node is null.";
			lastError = "TestNode object is not valid. The index of the node is null.";
			errorsExist = true;
			return null;
		}
		if (string.IsNullOrEmpty(realName))
		{
			lastCallInfo = "AddTest called with invalid first argument. RealName can't be null or empty";
			lastError = "AddTest called with invalid first argument. RealName can't be null or empty";
			errorsExist = true;
			return null;
		}
		if (newTestName == null)
		{
			lastCallInfo = "AddTest called with invalid third argument. NewTestName can't be null";
			lastError = "AddTest called with invalid third argument. NewTestName can't be null";
			errorsExist = true;
			return null;
		}
		lastCallInfo = "AddTest(" + realName + ", " + newTestName + ")";
		string text = AddTest(realName, newTestName, testIndex, Position.Last);
		if (text != null)
		{
			QSPRTestNode qSPRTestNode = new QSPRTestNode();
			qSPRTestNode.testTree = testTree;
			qSPRTestNode.TestIndex = text;
			return qSPRTestNode;
		}
		lastError = "AddTest failed";
		errorsExist = true;
		return null;
	}

	public QSPRTestNode CreateFolder(string subFolderName)
	{
		if (string.IsNullOrEmpty(subFolderName))
		{
			lastCallInfo = "CreateFolder was called with a null foldername";
			lastError = "CreateFolder failed, foldername was null";
			errorsExist = true;
			return null;
		}
		lastCallInfo = "CreateFolder(" + subFolderName + ")";
		string text = CreateFolder(subFolderName, testIndex);
		if (text != null)
		{
			QSPRTestNode qSPRTestNode = new QSPRTestNode();
			qSPRTestNode.TestTree = TestTree;
			qSPRTestNode.TestIndex = text;
			return qSPRTestNode;
		}
		lastError = "Failed to create folder.";
		errorsExist = true;
		return null;
	}

	public object GetTestPropertyValue(string propertyName, int convertToString)
	{
		if (propertyName == null)
		{
			lastCallInfo = "GetTestPropertyValue called with one or more null argument";
			lastError = "GetTestPropertyValue failed, one or more arguments were null";
			errorsExist = true;
			return null;
		}
		lastCallInfo = "GetTestPropertyValue(" + propertyName + " ," + convertToString + ")";
		object testPropertyValue = GetTestPropertyValue(propertyName, testIndex, convertToString);
		if (testPropertyValue == null)
		{
			lastError = "GetTestPropertyValue failed";
			errorsExist = true;
		}
		return testPropertyValue;
	}

	public int SetTestProperty(string propertyName, object value)
	{
		try
		{
			if (string.IsNullOrEmpty(testIndex))
			{
				lastCallInfo = "TestNode object is invalid. The test index cannot be null or empty.";
				lastError = "TestNode object is invalid. The test index cannot be null or empty.";
				errorsExist = true;
				return 0;
			}
			if (string.IsNullOrEmpty(propertyName))
			{
				lastCallInfo = "SetTestProperty called with invalid propertyName. This argument cannot be null or empty.";
				lastError = "SetTestProperty called with invalid propertyName. This argument cannot be null or empty.";
				errorsExist = true;
				return 0;
			}
			if (value == null)
			{
				lastCallInfo = "SetTestProperty called with invalid value. This argument cannot be null.";
				lastError = "SetTestProperty called with invalid value. This argument cannot be null.";
				errorsExist = true;
				return 0;
			}
			lastCallInfo = "SetTestProperty(" + propertyName + ", " + value.ToString() + ")";
			if (SetTestProperty(testIndex, propertyName, value))
			{
				return 1;
			}
			lastError = "SetTestProperty failed";
			errorsExist = true;
		}
		catch (Exception ex)
		{
			lastError = "SetTestProperty failed: " + ex.Message + ex.InnerException;
			errorsExist = true;
		}
		return 0;
	}

	public int SetTestParameter(string ParamName, object newVal, string lowerLimit, string upperLimit)
	{
		if (ParamName == null || newVal == null)
		{
			lastCallInfo = "SetTestParameter called with one or more null arguments";
			lastError = "SetTestParameter failed, one or more arguments were null";
			errorsExist = true;
			return 0;
		}
		if (lowerLimit == null)
		{
			lowerLimit = "";
		}
		if (upperLimit == null)
		{
			upperLimit = "";
		}
		lastCallInfo = "SetTestParameter(" + ParamName + ", " + newVal.ToString() + ", " + lowerLimit + ", " + upperLimit + ")";
		string text = newVal.GetType().Name.ToString();
		string text2 = "";
		if (text.CompareTo("String") == 0)
		{
			text2 = (string)newVal;
		}
		else if (text.CompareTo("Int32") == 0)
		{
			text2 = ((int)newVal).ToString();
		}
		else if (text.CompareTo("Int16") == 0)
		{
			text2 = ((short)newVal).ToString();
		}
		else if (text.CompareTo("Float") == 0)
		{
			text2 = ((float)newVal).ToString();
		}
		else
		{
			if (text.CompareTo("Double") != 0)
			{
				lastError = "SetTestParameter failed, invalid value";
				errorsExist = true;
				return 0;
			}
			text2 = ((double)newVal).ToString();
		}
		if (!SetTestParameter(testIndex, ParamName, text2, lowerLimit, upperLimit))
		{
			lastError = "SetTestParameter failed";
			errorsExist = true;
			return 0;
		}
		return 1;
	}

	public string GetTestParamLimit(string ParamName, int LimitType)
	{
		if (ParamName == null)
		{
			lastCallInfo = "GetTestParamLimit called with null param name";
			lastError = "GetTestParamLimit failed, the ParamName is null";
			errorsExist = true;
			return "";
		}
		lastCallInfo = "GetTestParamLimit(" + ParamName + " ," + LimitType + ")";
		ParameterCls parameter = null;
		if (GetTestParameterValue(ParamName, out parameter))
		{
			if (LimitType == 0)
			{
				return parameter.LowerLimit;
			}
			return parameter.UpperLimit;
		}
		lastError = "GetTestParamLimit failed, the ParamName was not found";
		errorsExist = true;
		return null;
	}

	private string AddTest(string realName, string newTestName, string destinationIndex, Position position)
	{
		if (!string.IsNullOrEmpty(realName) && newTestName != null && destinationIndex != null)
		{
			BaseTest baseTest = Root.FindTest(destinationIndex);
			if (baseTest != null && !(baseTest is RootTest))
			{
				if (baseTest is FolderTest)
				{
					if (position == Position.NextSibling || position == Position.PreviousSibling)
					{
						baseTest.Properties.Expanded = false;
					}
					else
					{
						baseTest.Properties.Expanded = true;
					}
				}
				else if (position != Position.PreviousSibling)
				{
					position = Position.NextSibling;
				}
				QTMTest testNode = null;
				BaseTest newTest = null;
				QTMTestContainer containerNode = null;
				if (GetRegistryTestNode(realName, matchCase: true, partialMatch: false, ref testNode, ref containerNode))
				{
					if (!containerNode.Loaded)
					{
						testTree.LoadContainer(containerNode, newContainer: false);
					}
					if (AddNewNode(baseTest, testNode, out newTest, position))
					{
						if (newTestName != "")
						{
							newTest.Properties.TestName = newTestName;
						}
						return newTest.Index;
					}
				}
				else
				{
					lastError = realName + " node can't be found in the test registry tree.";
					errorsExist = true;
				}
			}
			else
			{
				lastError = "A valid test node was not selected. Valid nodes are tests or folders, excluding the root node.";
				errorsExist = true;
			}
		}
		else
		{
			lastError = "RealName, TestName, and/or DestinationIndex object(s) are not valid.";
			errorsExist = true;
		}
		return null;
	}

	private bool AddNewNode(BaseTest parentNode, QTMTest registryTest, out BaseTest newTest, Position insertPosition, bool isTestStep = false)
	{
		newTest = null;
		try
		{
			bool flag = false;
			bool flag2 = false;
			int num = 0;
			QTMTestContainer tContainer = new QTMTestContainer();
			((QTMRegistry)testTree.qtmData.theRegistry).GetContainerForTest(registryTest.Name, registryTest.TargetName, out tContainer);
			switch (tContainer.ContainerType)
			{
			case QC.QsprTestObjects.Tests.Base.QTMTestTypes.HiveTest:
			{
				string[] array = tContainer.Name.Split('\\');
				HiveTest hiveTest = new HiveTest(registryTest.Name, tContainer.TargetName, Root);
				hiveTest.HiveProperties.DLLName = array[array.Length - 1];
				ApplyParameterLogging(hiveTest);
				newTest = hiveTest;
				break;
			}
			case QC.QsprTestObjects.Tests.Base.QTMTestTypes.ScriptTest:
			{
				ScriptTest scriptTest = new ScriptTest(registryTest.Name, tContainer.TargetName, Root);
				ApplyParameterLogging(scriptTest);
				newTest = scriptTest;
				break;
			}
			case QC.QsprTestObjects.Tests.Base.QTMTestTypes.LabVIEWTest:
			{
				LabVIEWTest labVIEWTest = new LabVIEWTest(registryTest.Name, tContainer.TargetName, Root);
				ApplyParameterLogging(labVIEWTest);
				newTest = labVIEWTest;
				break;
			}
			case QC.QsprTestObjects.Tests.Base.QTMTestTypes.DotNetTest:
			{
				DotNetTest dotNetTest = new DotNetTest(registryTest.Name, tContainer.TargetName, Root);
				FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(tContainer.Name);
				dotNetTest.DotNetProperties.MinDllVersion = versionInfo.FileVersion;
				dotNetTest.DotNetProperties.DllName = Path.GetFileName(versionInfo.FileName);
				ApplyParameterLogging(dotNetTest);
				newTest = dotNetTest;
				break;
			}
			case QC.QsprTestObjects.Tests.Base.QTMTestTypes.DotNetTestEx:
			{
				DotNetTestEx dotNetTestEx = new DotNetTestEx(registryTest.Name, tContainer.TargetName, Root);
				ApplyParameterLogging(dotNetTestEx);
				newTest = dotNetTestEx;
				break;
			}
			}
			if (newTest != null)
			{
				newTest.Properties.CheckStateTest = CheckState.Checked;
			}
			if (parentNode == testTree.root)
			{
				flag = true;
				num = ((!parentNode.Properties.Expanded) ? ((FolderTest)parentNode).TestList.Count : ((insertPosition == Position.Last) ? ((FolderTest)parentNode).TestList.Count : 0));
			}
			else if ((parentNode is FolderTest && !isTestStep) || (parentNode is TestCaseTest && (isTestStep || (parentNode as TestCaseTest).TestList.Count == 0)))
			{
				if (parentNode.Properties.Expanded || ((FolderTest)parentNode).TestList.Count == 0)
				{
					switch (insertPosition)
					{
					case Position.Last:
						num = ((FolderTest)parentNode).TestList.Count;
						flag = true;
						break;
					case Position.First:
						num = 0;
						flag = true;
						break;
					default:
						if (parentNode.Parent == testTree.root && !(newTest is FolderTest))
						{
							return false;
						}
						switch (insertPosition)
						{
						case Position.NextSibling:
							num = parentNode.Parent.TestList.IndexOf((FolderTest)parentNode) + 1;
							flag2 = true;
							break;
						case Position.PreviousSibling:
							num = parentNode.Parent.TestList.IndexOf((FolderTest)parentNode);
							flag2 = true;
							break;
						}
						break;
					}
				}
				else if (parentNode.Parent is RootTest && !(newTest is FolderTest))
				{
					if (insertPosition == Position.NextSibling || insertPosition == Position.PreviousSibling)
					{
						return false;
					}
					num = 0;
					flag = true;
				}
				else
				{
					switch (insertPosition)
					{
					case Position.NextSibling:
						num = parentNode.Parent.TestList.IndexOf((FolderTest)parentNode) + 1;
						flag2 = true;
						break;
					case Position.PreviousSibling:
						num = parentNode.Parent.TestList.IndexOf((FolderTest)parentNode);
						flag2 = true;
						break;
					default:
						num = parentNode.Parent.TestList.IndexOf((FolderTest)parentNode) + 1;
						parentNode = parentNode.Parent;
						flag = true;
						break;
					}
				}
			}
			else
			{
				switch (insertPosition)
				{
				case Position.NextSibling:
					num = parentNode.Parent.TestList.IndexOf(parentNode) + 1;
					flag2 = true;
					break;
				case Position.PreviousSibling:
					num = parentNode.Parent.TestList.IndexOf(parentNode);
					flag2 = true;
					break;
				default:
					num = parentNode.Parent.TestList.IndexOf(parentNode) + 1;
					parentNode = parentNode.Parent;
					flag = true;
					break;
				}
			}
			if (flag)
			{
				if (num > 0)
				{
					BaseTest baseTest = ((FolderTest)parentNode).TestList[num - 1];
					if (num > 0 && baseTest is FolderTest && ((FolderTest)baseTest).FolderProperties.IsCleanupFolder)
					{
						num--;
					}
				}
				((FolderTest)parentNode).TestList.Insert(num, newTest);
				newTest.Parent = (FolderTest)parentNode;
			}
			else if (flag2)
			{
				parentNode.Parent.TestList.Insert(num, newTest);
				newTest.Parent = parentNode.Parent;
			}
			else
			{
				((FolderTest)parentNode).TestList.Add(newTest);
				newTest.Parent = (FolderTest)parentNode;
			}
			testTree.scheduler.GlobalStatus.RegistrationStatus.TreeChanged = true;
			return true;
		}
		catch (Exception ex)
		{
			lastError = "AddNewNode " + ex.ToString();
			errorsExist = true;
		}
		return false;
	}

	private void ApplyParameterLogging(BaseTest tTest)
	{
		foreach (string globalInclude in testTree.TreeInfo.GlobalIncludeList)
		{
			tTest.Properties.Parameters.DataLogging(globalInclude, bEnableLogging: true);
		}
		foreach (string globalExclude in testTree.TreeInfo.GlobalExcludeList)
		{
			tTest.Properties.Parameters.DataLogging(globalExclude, bEnableLogging: false);
		}
	}

	private string CreateFolder(string newFolderName, string destinationIndex)
	{
		try
		{
			BaseTest baseTest = Root.FindTest(destinationIndex);
			Position insertPosition = testTree.FolderInsertPosition;
			if (baseTest is FolderTest)
			{
				baseTest.Properties.Expanded = true;
			}
			else
			{
				insertPosition = Position.NextSibling;
			}
			FolderTest FolderNode = null;
			if (AddFolder(insertPosition, out FolderNode, baseTest) && FolderNode != null)
			{
				FolderNode.Properties.TestName = newFolderName;
				return FolderNode.Index;
			}
			lastError = "FolderName or folderInfo object is not valid.";
			errorsExist = true;
			return null;
		}
		catch (Exception ex)
		{
			lastError = "CreateFolder failed with exception: " + ex.ToString();
			errorsExist = true;
			return null;
		}
	}

	private bool AddFolder(Position insertPosition, out FolderTest FolderNode, BaseTest dropNode, bool isTestCaseFolder = false)
	{
		FolderNode = null;
		try
		{
			if (dropNode.Properties.LinkNode && dropNode.Properties.IsLinkActive == true)
			{
				lastError = "Linked nodes cannot be modified.";
				errorsExist = true;
				return false;
			}
			if (dropNode is FolderTest && !(dropNode is TestCaseTest) && ((FolderTest)dropNode).FolderProperties.LinkRoot)
			{
				bool? isLinkActive = ((FolderTest)dropNode).FolderProperties.IsLinkActive;
				if (isLinkActive.GetValueOrDefault() && isLinkActive.HasValue && dropNode.Properties.Expanded)
				{
					lastError = "Linked nodes cannot be modified.";
					errorsExist = true;
					return false;
				}
			}
			if (isTestCaseFolder)
			{
				FolderNode = new TestCaseTest();
			}
			else
			{
				FolderNode = new FolderTest();
			}
			FolderNode.Properties.TestName = "Folder";
			FolderNode.Properties.CheckStateTest = CheckState.Checked;
			FolderNode.Properties.Expanded = true;
			if (!AddNewNode(dropNode, FolderNode, insertPosition))
			{
				lastError = "ERROR: Cannot Add a new node.";
				errorsExist = true;
				return false;
			}
			if (dropNode is FolderTest)
			{
				((FolderTest)dropNode).Properties.Expanded = true;
			}
			return true;
		}
		catch (Exception ex)
		{
			lastError = "CreateFolder failed with exception: " + ex.ToString();
			errorsExist = true;
			return false;
		}
	}

	private bool AddNewNode(BaseTest ParentNode, BaseTest NewNode, Position insertPosition, bool isTestStep = false)
	{
		bool flag = false;
		bool flag2 = false;
		int num = 0;
		if (ParentNode is RootTest)
		{
			if (ParentNode.Properties.Expanded)
			{
				if (insertPosition == Position.Last)
				{
					num = ((RootTest)ParentNode).TestList.Count;
					flag = true;
				}
				else
				{
					num = 0;
					flag = true;
				}
			}
			else
			{
				num = ((RootTest)ParentNode).TestList.Count;
				flag = true;
			}
		}
		else if (ParentNode is FolderTest)
		{
			if (((FolderTest)ParentNode).Properties.Expanded || ((FolderTest)ParentNode).TestList.Count == 0)
			{
				switch (insertPosition)
				{
				case Position.Last:
					num = ((FolderTest)ParentNode).TestList.Count;
					flag = true;
					break;
				case Position.First:
					num = 0;
					flag = true;
					break;
				default:
					if (ParentNode.Parent == Root && !(NewNode is FolderTest))
					{
						return false;
					}
					switch (insertPosition)
					{
					case Position.NextSibling:
						num = ParentNode.Parent.TestList.IndexOf(ParentNode) + 1;
						flag2 = true;
						break;
					case Position.PreviousSibling:
						num = ParentNode.Parent.TestList.IndexOf(ParentNode);
						flag2 = true;
						break;
					}
					break;
				}
			}
			else if (ParentNode.Parent is RootTest && !(NewNode is FolderTest))
			{
				if (insertPosition == Position.NextSibling || insertPosition == Position.PreviousSibling)
				{
					return false;
				}
				num = 0;
				flag = true;
			}
			else
			{
				switch (insertPosition)
				{
				case Position.NextSibling:
					num = ParentNode.Parent.TestList.IndexOf(ParentNode) + 1;
					flag2 = true;
					break;
				case Position.PreviousSibling:
					num = ParentNode.Parent.TestList.IndexOf(ParentNode);
					flag2 = true;
					break;
				default:
					num = ParentNode.Parent.TestList.IndexOf(ParentNode) + 1;
					ParentNode = ParentNode.Parent;
					flag = true;
					break;
				}
			}
		}
		else
		{
			switch (insertPosition)
			{
			case Position.NextSibling:
				num = ParentNode.Parent.TestList.IndexOf(ParentNode) + 1;
				flag2 = true;
				break;
			case Position.PreviousSibling:
				num = ParentNode.Parent.TestList.IndexOf(ParentNode);
				flag2 = true;
				break;
			default:
				num = ParentNode.Parent.TestList.IndexOf(ParentNode) + 1;
				ParentNode = ParentNode.Parent;
				flag = true;
				break;
			}
		}
		if (flag)
		{
			if (num > 0)
			{
				BaseTest baseTest = ((FolderTest)ParentNode).TestList[num - 1];
				if (num > 0 && baseTest is FolderTest && ((FolderTest)baseTest).FolderProperties.IsCleanupFolder)
				{
					num--;
				}
			}
			((FolderTest)ParentNode).TestList.Insert(num, NewNode);
			NewNode.Parent = (FolderTest)ParentNode;
		}
		else if (flag2)
		{
			ParentNode.Parent.TestList.Insert(num, NewNode);
			if (ParentNode is FolderTest)
			{
				NewNode.Parent = (FolderTest)ParentNode;
			}
			else
			{
				NewNode.Parent = ParentNode.Parent;
			}
		}
		else
		{
			((FolderTest)ParentNode).TestList.Add(NewNode);
			NewNode.Parent = (FolderTest)ParentNode;
		}
		NewNode.Parent.Properties.Expanded = true;
		Scheduler.GlobalStatus.RegistrationStatus.TreeChanged = true;
		return true;
	}

	private bool GetRegistryTestNode(string realName, bool matchCase, bool partialMatch, ref QTMTest testNode, ref QTMTestContainer containerNode)
	{
		if (((QTMRegistry)Scheduler.QTMData.theRegistry).Targets.Count > 0)
		{
			QTMTarget qTMTarget = ((QTMRegistry)Scheduler.QTMData.theRegistry).Target[0];
			bool flag = false;
			int num = 0;
			if (containerNode != null)
			{
				num = qTMTarget.TestContainers.IndexOf(containerNode);
			}
			for (int i = num; i < qTMTarget.TestContainers.Count; i++)
			{
				QTMTestContainer qTMTestContainer = qTMTarget.TestContainers[i];
				int num2 = 0;
				if (testNode != null)
				{
					num2 = qTMTestContainer.Tests.IndexOf(testNode);
					flag = true;
				}
				for (int j = num2; j < qTMTestContainer.Tests.Count; j++)
				{
					if (flag && j == num2)
					{
						continue;
					}
					QTMTest qTMTest = qTMTestContainer.Tests[j] as QTMTest;
					string text = qTMTest.Name;
					if ((qTMTestContainer.ContainerType == QC.QsprTestObjects.Tests.Base.QTMTestTypes.DotNetTest || qTMTestContainer.ContainerType == QC.QsprTestObjects.Tests.Base.QTMTestTypes.DotNetTestEx) && text.Contains('.'))
					{
						string[] array = null;
						array = text.Split('.');
						text = array[array.Length - 1];
					}
					if (partialMatch)
					{
						if (matchCase)
						{
							if (text.Contains(realName))
							{
								containerNode = qTMTestContainer;
								testNode = qTMTest;
								return true;
							}
						}
						else if (text.ToLower().Contains(realName.ToLower()))
						{
							containerNode = qTMTestContainer;
							testNode = qTMTest;
							return true;
						}
					}
					else if (string.Compare(text, realName, !matchCase) == 0)
					{
						containerNode = qTMTestContainer;
						testNode = qTMTest;
						return true;
					}
				}
			}
		}
		return false;
	}

	private object GetTestPropertyValue(string propertyName, string testIndex, int convertToString)
	{
		object obj = null;
		try
		{
			if (!string.IsNullOrEmpty(testIndex))
			{
				BaseTest baseTest = null;
				baseTest = ((!(Root.Index == testIndex)) ? Root.FindTest(testIndex) : Root);
				if (baseTest != null)
				{
					PropertyGrid propertyGrid = new PropertyGrid();
					propertyGrid.SelectedObject = baseTest.Properties;
					Attribute[] array = new Attribute[propertyGrid.BrowsableAttributes.Count];
					propertyGrid.BrowsableAttributes.CopyTo(array, 0);
					PropertyDescriptor propertyDescriptor = propertyGrid.SelectedTab.GetProperties(propertyGrid.SelectedObject, array)[propertyName];
					obj = propertyDescriptor.GetValue(propertyGrid.SelectedObject);
					if (convertToString == 1)
					{
						obj = obj.ToString();
					}
				}
			}
		}
		catch (Exception ex)
		{
			lastError = "GetTestPropertyValue failed with exception: " + ex.ToString();
			errorsExist = true;
			return null;
		}
		return obj;
	}

	private bool SetTestProperty(string testIndex, string propertyName, object value)
	{
		bool result = false;
		try
		{
			if (!string.IsNullOrEmpty(testIndex))
			{
				BaseTest baseTest = Root.FindTest(testIndex);
				if (baseTest != null)
				{
					PropertyGrid propertyGrid = new PropertyGrid();
					propertyGrid.SelectedObject = baseTest.Properties;
					Attribute[] array = new Attribute[propertyGrid.BrowsableAttributes.Count];
					propertyGrid.BrowsableAttributes.CopyTo(array, 0);
					PropertyDescriptor propertyDescriptor = propertyGrid.SelectedTab.GetProperties(propertyGrid.SelectedObject, array)[propertyName];
					propertyDescriptor.GetValue(propertyGrid.SelectedObject);
					string text = value.ToString();
					TypeConverter converter = TypeDescriptor.GetConverter(propertyDescriptor.PropertyType);
					if (converter is BooleanConverter)
					{
						if (text == "0")
						{
							text = "False";
						}
						else if (text == "1")
						{
							text = "True";
						}
					}
					object value2 = converter.ConvertFromString(text);
					propertyDescriptor.SetValue(propertyGrid.SelectedObject, value2);
					try
					{
						GridItem gridItem = propertyGrid.SelectedGridItem;
						while (gridItem.Parent != null)
						{
							gridItem = gridItem.Parent;
						}
						GridItem gridItem2 = gridItem.GridItems[propertyDescriptor.Category].GridItems[propertyName];
						if (gridItem2 != null)
						{
							gridItem2.Select();
						}
						else
						{
							lastError = "TestProperty not found: Category=" + propertyDescriptor.Category + " PropertyName=" + propertyName;
							errorsExist = true;
						}
					}
					catch (Exception ex)
					{
						lastError = "SetTestProperty exception: " + ex.ToString();
						errorsExist = true;
					}
					result = true;
				}
			}
		}
		catch (Exception ex2)
		{
			lastError = "SetTestProperty failed with exception: " + ex2.ToString();
			errorsExist = true;
		}
		return result;
	}

	private bool SetTestParameter(string testIndex, string paramName, string strVal, string lowerLimit, string upperLimit)
	{
		try
		{
			BaseTest baseTest = Root.FindTest(testIndex);
			if (baseTest != null)
			{
				if (baseTest.Properties.Parameters.HasParameter(paramName, exactMatch: true, out var _) && UpdateParamValue(baseTest, paramName, strVal) && UpdateParamLimits(baseTest, paramName, lowerLimit, upperLimit))
				{
					return true;
				}
				return false;
			}
			lastError = "SetTestParameter failed, invalid test index specified";
			errorsExist = true;
			return false;
		}
		catch (Exception ex)
		{
			lastError = "SetTestParameter failed, " + ex.ToString();
			errorsExist = true;
		}
		return false;
	}

	private bool UpdateParamValue(BaseTest test, string parameterName, string parameterValue, int index = 0, string array2DIndex = null)
	{
		try
		{
			return test.Properties.Parameters.SetParamValue(parameterName, parameterValue, index, array2DIndex);
		}
		catch (Exception ex)
		{
			lastError = "Failed to update parameter " + parameterName + ": " + ex.ToString();
			errorsExist = true;
			return false;
		}
	}

	private bool UpdateParamLimits(BaseTest test, string parameterName, string lowerLimit, string upperLimit, int index = 0)
	{
		try
		{
			bool result = true;
			if (!string.IsNullOrEmpty(lowerLimit) && !test.Properties.Parameters.SetParamLowerLimit(parameterName, lowerLimit, index))
			{
				result = false;
			}
			if (!string.IsNullOrEmpty(upperLimit) && !test.Properties.Parameters.SetParamUpperLimit(parameterName, upperLimit, index))
			{
				result = false;
			}
			return result;
		}
		catch (Exception ex)
		{
			lastError = "Failed to update parameter " + parameterName + ": " + ex.ToString();
			errorsExist = true;
			return false;
		}
	}

	private bool GetTestParameterValue(string paramName, out ParameterCls parameter)
	{
		parameter = null;
		try
		{
			if (!string.IsNullOrWhiteSpace(paramName) && !string.IsNullOrWhiteSpace(TestIndex) && Root.TestList.Count > 0)
			{
				BaseTest baseTest = Root.FindTest(TestIndex);
				if (baseTest != null && baseTest.Properties.Parameters.HasParameter(paramName))
				{
					parameter = baseTest.Properties.Parameters.GetParameter(paramName);
					return true;
				}
			}
		}
		catch (Exception ex)
		{
			lastError = "GetTestParameterValue failed, " + ex.Message;
		}
		return false;
	}
}
