using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using QC.GlobalVariableManager;
using QC.QSPR;
using QC.QsprShared.Config;
using QC.QsprShared.Dialogs;
using QC.QsprShared.GlobalData;
using QC.QsprShared.Trace;
using QC.QsprTestObjects.Loops;
using QC.QsprTestObjects.Parameters;
using QC.QsprTestObjects.Registry;
using QC.QsprTestObjects.TestConditions;
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.QTMInterface;

namespace QC.QTMApplication.Automation;

public class AutomationHandler
{
	public delegate ArrayList GetTesterConfigHandler();

	public delegate void OnPluginMenuCmdHandler(object sender, string menuKey);

	public delegate void ToolsClickHandler(string menuKey);

	public delegate void OnOpenFile(string fileName);

	public delegate void OnNewTree(string menu);

	public delegate void OnSetCloseHidesUI(bool hide);

	public delegate bool OnGetCloseHidesUI();

	public delegate bool OnSaveFile(SaveAsFormat format, bool suppressTreeUpgradeWarning = false);

	public delegate bool OnSaveAsFile(string fileName, bool saveAsLatestFormat);

	public delegate bool OnAddFolder(Position insertPosition, out FolderTest newFolder, BaseTest destinationNode = null, bool isTestCaseFolder = false);

	public delegate void OnEndTestNameEdit(BaseTest baseTest, bool commit);

	public delegate bool OnDeleteNode(BaseTest nodeToDelete = null);

	public delegate bool OnImportTestTree(bool asLink, BaseTest DroppedNode, string fName, FolderTest oldRootFolder, ImportTestsInfo importInfo);

	public delegate void OnUpdateWindowBG(Color bgColor);

	public delegate bool OnAddContainer(QC.QsprTestObjects.Tests.Base.QTMTestTypes testType, string[] containerNames);

	public delegate string OnAddTest(AddTestInfo newTestInfo);

	public delegate bool OnAddScriptParam(AddScriptParamInfo scriptParamInfo);

	public delegate bool OnEnableLoopRow(LoopRowInfo loopRowInfo, bool enableBasedOnValues);

	public delegate bool OnEnableDataLogging(DataLoggingInfo dataLoggingInfo);

	public delegate string OnGetLoopValues(LoopRowInfo loopRowInfo);

	public delegate bool OnResetTestTree(BaseTest node);

	public delegate CallbackResultInfo OnRegisterTree(TTDPluginEventArgs e, CallbackResultInfo cInfo);

	public delegate CallbackResultInfo OnRunTest(string TestName);

	public delegate bool OnRenameNode(TestInfo testInfo);

	public delegate CallbackResultInfo OnSyncTest(string testName);

	public delegate CallbackResultInfo OnSetTestParamLimits(ParamInfo paramInfo);

	public delegate void OnSetTreeChanged(bool changed = true);

	public delegate string OnCopyTest(string destinationFolder, string newName, string testIndex, string position);

	public delegate bool OnExportNodeToTextFile(ExportNodeInfo exportInfo);

	private enum FindTestValueType
	{
		RealName,
		TestName,
		TestIndex,
		TestPath
	}

	public GetTesterConfigHandler GetTesterConfig;

	public OnPluginMenuCmdHandler OnPluginMenuCmd;

	public ToolsClickHandler HandleToolsClick;

	public OnOpenFile OpenFile;

	public OnNewTree NewTree;

	public OnSetCloseHidesUI SetCloseHidesUI;

	public OnGetCloseHidesUI GetCloseHidesUI;

	public OnSaveFile SaveFile;

	public OnSaveAsFile SaveAsFile;

	public OnAddFolder AddFolder;

	public OnEndTestNameEdit EndTestNameEdit;

	public OnDeleteNode DeleteNode;

	public OnImportTestTree ImportTestTree;

	public OnUpdateWindowBG UpdateWindowBG;

	public OnAddContainer AddContainer;

	public OnAddTest AddTest;

	public OnAddScriptParam AddScriptParam;

	public OnEnableLoopRow EnableLoopRow;

	public OnEnableDataLogging EnableParamDataLogging;

	public OnGetLoopValues GetLoopValues;

	public OnResetTestTree ResetTestTree;

	public OnRegisterTree RegisterTree;

	public OnRunTest RunTest;

	public OnRenameNode RenameNode;

	public OnSyncTest SyncTest;

	public OnSetTestParamLimits SetTestParamLimits;

	public OnSetTreeChanged SetTreeChanged;

	public OnCopyTest CopyTest;

	public OnExportNodeToTextFile ExportNodeToTextFile;

	private WPFTestTreeWindowColl treeViewColl;

	private readonly string[] separator = new string[1] { "_$_" };

	private TTDWpfTreeView treeView => treeViewColl.ttdWpfTreeView;

	private QSPRMainWindow QSPRWindow => treeViewColl.QSPRWindow;

	public RootTest Root => treeView.Root;

	public QTMData QTMData => treeView._scheduler.QTMData;

	public GlobalStatus GlobalStatus => treeView._scheduler.GlobalStatus;

	public AutomationHandler(WPFTestTreeWindowColl treeViewColl)
	{
		this.treeViewColl = treeViewColl;
	}

	public CallbackResultInfo AsynchronousMethods(TTDPluginEventArgs e)
	{
		CallbackResultInfo result = default(CallbackResultInfo);
		try
		{
			string key;
			if ((key = e.Key) != null && key == "Run Tree")
			{
				RunTreeInfo runTreeInfo = (RunTreeInfo)e.Message;
				result.Result = true;
				QTMData.TesterConfigurationList = GetTesterConfiguration();
				if (!runTreeInfo.UseLastSN && runTreeInfo.UUT_SN.Length > 0)
				{
					QTMData.WConfig.LastUsedSerialNumber = runTreeInfo.UUT_SN;
					QTMGlobalVariable.SetGlobalVariable("SN", runTreeInfo.UUT_SN);
				}
				bool flag = false;
				object[] array = QTMData.TesterConfigurationList.ToArray();
				for (int i = 0; i < array.Length; i++)
				{
					TesterConfigurationInfo testerConfigurationInfo = (TesterConfigurationInfo)array[i];
					if (testerConfigurationInfo.Name == "Index")
					{
						flag = true;
					}
				}
				if (!flag)
				{
					TesterConfigurationInfo testerConfigurationInfo2 = new TesterConfigurationInfo();
					testerConfigurationInfo2.Name = "Index";
					testerConfigurationInfo2.Value = runTreeInfo.PhoneIndex.ToString();
					testerConfigurationInfo2.Description = "Unit Index";
					testerConfigurationInfo2.Editable = false;
					testerConfigurationInfo2.DisplayAtGoNoGo = false;
					testerConfigurationInfo2.IncludeInTestResults = true;
					QTMData.TesterConfigurationList.Add(testerConfigurationInfo2);
				}
				OnTTDPluginMenu("Run Tree");
			}
		}
		catch (Exception ex)
		{
			AppMessageDialog.Show("Failed to execute " + e.Key + "; Error: " + ex.ToString(), DialogResult.OK, MessageDialogType.Exception);
			result.Message = ex.Message;
			result.Result = false;
		}
		return result;
	}

	private void HandleRegisterTestPlan(Hashtable propertiesTable)
	{
		TestPlanProperties testPlanProperties = new TestPlanProperties(WPFTestTreeWindowColl.TTDView.Root.RootProperties);
		testPlanProperties.Status = (string)propertiesTable["STATUS"];
		testPlanProperties.TestPlanName = (string)propertiesTable["TESTPLANNAME"];
		testPlanProperties.TestPlanVersionID = (int)propertiesTable["TESTPLANID"];
		WPFTestTreeWindowColl.TTDView.Root.RootProperties = testPlanProperties;
	}

	public bool OpenTestPlan(string testPlanMessage)
	{
		string[] array = testPlanMessage.Split(';');
		string text = array[0];
		string s = array[1];
		string databaseServer = array[2];
		string databaseName = array[3];
		if (string.IsNullOrWhiteSpace(text))
		{
			return false;
		}
		if (!long.TryParse(text, out var result))
		{
			result = -1L;
		}
		if (!int.TryParse(s, out var result2))
		{
			return false;
		}
		using (Dictionary<string, TestCasesPlugin>.ValueCollection.Enumerator enumerator = WPFTestTreeWindowColl.TTDView.TestCasesPluginsColl.Values.GetEnumerator())
		{
			if (enumerator.MoveNext())
			{
				TestCasesPlugin current = enumerator.Current;
				try
				{
					if (result > -1)
					{
						current.LoadTestPlan(result, result2, databaseServer, databaseName);
					}
					else
					{
						current.LoadTestPlanByName(text, result2, databaseServer, databaseName);
					}
				}
				catch (IOException ex)
				{
					AppMessageDialog.Show("IO Error opening Test Plan\n\n" + ex.Message.ToString(), "IO Error", DialogResult.OK, MessageDialogType.Error);
				}
				return true;
			}
		}
		return false;
	}

	public CallbackResultInfo SynchronousMethods(TTDPluginEventArgs e)
	{
		CallbackResultInfo callbackResultInfo = default(CallbackResultInfo);
		callbackResultInfo.Result = false;
		try
		{
			switch (e.Key)
			{
			case "Suppress All Messages":
				GlobalStatus.SuppressAllMessagesMode = (bool)e.Message;
				callbackResultInfo.Result = true;
				break;
			case "Register Test Plan":
				HandleRegisterTestPlan((Hashtable)e.Message);
				break;
			case "Bring Tree In Focus":
				callbackResultInfo.Result = true;
				break;
			case "Status Bar":
				callbackResultInfo.Result = true;
				break;
			case "Tree Changed":
				GlobalStatus.RegistrationStatus.PluginDataChanged = true;
				callbackResultInfo.Result = true;
				break;
			case "Open":
				OpenFile((string)e.Message);
				callbackResultInfo.Result = true;
				break;
			case "Open Test Plan":
				callbackResultInfo.Result = OpenTestPlan((string)e.Message);
				break;
			case "Select Tests By Index":
				callbackResultInfo.Result = SelectTests((string)e.Message, byName: false);
				break;
			case "Select Tests By Name":
				callbackResultInfo.Result = SelectTests((string)e.Message, byName: true);
				break;
			case "Select Test Node":
			{
				SelectNodeInfo sInfo = (SelectNodeInfo)e.Message;
				callbackResultInfo.Result = SelectTestNode(sInfo);
				break;
			}
			case "Update Global Variables with Tester Config":
				Root.Scheduler.QTMData.UpdateGlobalVble(GetFromTesterConfig: true, treeView.GetTesterConfiguration());
				callbackResultInfo.Result = true;
				break;
			case "Set Serial Number":
				callbackResultInfo = SetSerialNumber(e, callbackResultInfo);
				return callbackResultInfo;
			case "Set User Input":
				callbackResultInfo = SetUserInput(e, callbackResultInfo);
				return callbackResultInfo;
			case "Set Test Parameter In All Tests":
				callbackResultInfo = SetTestParamInAllTests(e, callbackResultInfo);
				return callbackResultInfo;
			case "Set Skip Run Sequence":
				callbackResultInfo = SetSkipRun(e, callbackResultInfo);
				return callbackResultInfo;
			case "Set Test Parameter":
				callbackResultInfo = SetTestParameter(e, callbackResultInfo);
				return callbackResultInfo;
			case "Set Test Param GV Get":
			case "Set Test Param GV Set":
			case "Set Test Param GV Get LL":
			case "Set Test Param GV Get UL":
				callbackResultInfo = SetTestParamGV(e, callbackResultInfo);
				return callbackResultInfo;
			case "Set Test Parameter In Folder":
				callbackResultInfo = SetTestParameterInFolder(e, callbackResultInfo);
				return callbackResultInfo;
			case "Set Root Name":
				if (e.Message != null)
				{
					callbackResultInfo.Result = SetRootName(e.Message.ToString());
				}
				break;
			case "Import Tests":
				if (e.Message != null && e.Message is ImportTestsInfo)
				{
					ImportTestsInfo importInfo = (ImportTestsInfo)e.Message;
					callbackResultInfo.Result = ImportTests(importInfo);
				}
				break;
			case "Rename Test":
				callbackResultInfo = RenameTest(e, callbackResultInfo);
				return callbackResultInfo;
			case "Get GoNoGo Status":
			{
				TestResult result = TestResult.Failed;
				callbackResultInfo.Result = GetTreeResult(out result);
				callbackResultInfo.Message = result;
				break;
			}
			case "Exit":
				callbackResultInfo.Result = true;
				break;
			case "New":
				NewTree("New");
				callbackResultInfo.Result = true;
				break;
			case "DeleteTest":
				if (e.Message != null && !string.IsNullOrEmpty(e.Message.ToString()))
				{
					callbackResultInfo.Result = DeleteTest(e.Message.ToString());
				}
				else
				{
					callbackResultInfo.Result = false;
				}
				break;
			case "Save":
				callbackResultInfo.Result = SaveFile(SaveAsFormat.QSPR);
				break;
			case "SaveAs":
			case "Save As QSPR Format":
				callbackResultInfo.Result = SaveAsFile(e.Message.ToString(), saveAsLatestFormat: true);
				break;
			case "Suppress SN Dialog":
				callbackResultInfo = SuppressSnDialog(e, callbackResultInfo);
				return callbackResultInfo;
			case "Suppress Results Dialog":
				callbackResultInfo = SuppressResultsDialog(e, callbackResultInfo);
				return callbackResultInfo;
			case "Set Window Background Color":
				callbackResultInfo = SetBackgroundColor(e, callbackResultInfo);
				return callbackResultInfo;
			case "Hide Window":
				QSPRWindow.HideWindow();
				callbackResultInfo.Result = true;
				break;
			case "Show Window":
				QSPRWindow.ShowWindow();
				callbackResultInfo.Result = true;
				break;
			case "Create Folder":
				callbackResultInfo = CreateFolder(e, callbackResultInfo);
				return callbackResultInfo;
			case "Set Loop Type":
				callbackResultInfo.Result = SetLoopType(e.Message as LoopInfo);
				break;
			case "Set Loop Type In Folder":
				callbackResultInfo = SetLoopTypeInFolder(e, callbackResultInfo);
				return callbackResultInfo;
			case "Enable Loop Row":
				callbackResultInfo = EnableLoopRows(e, callbackResultInfo, enableBasedOnValues: false);
				return callbackResultInfo;
			case "Enable Loop Values":
				callbackResultInfo = EnableLoopRows(e, callbackResultInfo, enableBasedOnValues: true);
				return callbackResultInfo;
			case "Enable DataLogging":
				callbackResultInfo = EnableDataLogging(e, callbackResultInfo);
				return callbackResultInfo;
			case "Get Loop Values":
				callbackResultInfo = GetLoopValue(e, callbackResultInfo);
				return callbackResultInfo;
			case "Get Loop Names":
				callbackResultInfo = GetLoopNames(e, callbackResultInfo);
				return callbackResultInfo;
			case "Get Loop Rows Enabled":
				callbackResultInfo = GetLoopRowsEnabled(e, callbackResultInfo);
				return callbackResultInfo;
			case "Get Param Looped Test Name":
				callbackResultInfo = GetParamLoopedTestName(e, callbackResultInfo);
				return callbackResultInfo;
			case "Get Loop Param Name":
				callbackResultInfo = GetLoopParamName(e, callbackResultInfo);
				return callbackResultInfo;
			case "Set Node Description":
				callbackResultInfo = SetNodeDescription(e.Message as NodeDescriptionInfo);
				return callbackResultInfo;
			case "Synchronize":
				callbackResultInfo = SynchronizeTest(e, callbackResultInfo);
				return callbackResultInfo;
			case "Set Node Notes":
				callbackResultInfo = SetNodeNotes(e.Message as NodeNotesInfo);
				return callbackResultInfo;
			case "Get Node Description":
				callbackResultInfo = GetNodeDescription(e.Message as NodeDescriptionInfo);
				return callbackResultInfo;
			case "Get Node Notes":
				callbackResultInfo = GetNodeNotes(e.Message as NodeNotesInfo);
				return callbackResultInfo;
			case "Get Test Index":
				callbackResultInfo = GetTestIndex(e, callbackResultInfo);
				return callbackResultInfo;
			case "Add DLL":
			case "Add Script":
				callbackResultInfo = AddTestContainer(e, callbackResultInfo);
				return callbackResultInfo;
			case "AddTest":
				callbackResultInfo = AddTestNode(e, callbackResultInfo);
				return callbackResultInfo;
			case "Copy":
				callbackResultInfo = CopyTestNode(e, callbackResultInfo);
				return callbackResultInfo;
			case "Add DLL Path":
				callbackResultInfo = AddDllPath(e, callbackResultInfo);
				return callbackResultInfo;
			case "Enable DLL Paths":
				callbackResultInfo = EnableDllPaths(e, callbackResultInfo);
				return callbackResultInfo;
			case "Enable Regular Expression Support":
				callbackResultInfo = EnableRegex(e, callbackResultInfo);
				return callbackResultInfo;
			case "Create Condition On Test Parameter":
			case "Create Condition On Test Result":
			case "Create Condition On Global Variable":
				callbackResultInfo = CreateCondition(e, callbackResultInfo);
				return callbackResultInfo;
			case "Set Test Condition":
				callbackResultInfo = SetCondition(e, callbackResultInfo);
				return callbackResultInfo;
			case "Set Test Condition Active":
				callbackResultInfo = SetConditionActive(e, callbackResultInfo);
				return callbackResultInfo;
			case "Set Test Property":
				callbackResultInfo = SetTestProperty(e, callbackResultInfo);
				return callbackResultInfo;
			case "Export Node to Text File":
				callbackResultInfo = ExportToTextFile(e, callbackResultInfo);
				return callbackResultInfo;
			case "Add Script Parameter":
				callbackResultInfo = AddScriptParameter(e, callbackResultInfo);
				return callbackResultInfo;
			case "RegisterTree":
				callbackResultInfo = RegisterTree(e, callbackResultInfo);
				return callbackResultInfo;
			case "Set Parameter Limits":
				callbackResultInfo = SetParamLimits(e, callbackResultInfo);
				return callbackResultInfo;
			case "Get Version":
				callbackResultInfo.Message = GetVersion();
				callbackResultInfo.Result = true;
				break;
			case "Get Test Parameter Value":
				callbackResultInfo = GetTestParamValue(e, callbackResultInfo);
				return callbackResultInfo;
			case "Get Test Property Value":
				callbackResultInfo = GetTestPropertyValue(e, callbackResultInfo);
				return callbackResultInfo;
			case "Get Next Node":
				callbackResultInfo = GetNextNode(e, callbackResultInfo);
				return callbackResultInfo;
			case "Get Parent Node":
				callbackResultInfo = GetParentNode(e, callbackResultInfo);
				return callbackResultInfo;
			case "Get Parameter Limit":
				callbackResultInfo = GetParamLimit(e, callbackResultInfo);
				return callbackResultInfo;
			case "Set Parameter Limit":
				callbackResultInfo = SetParamLimit(e, callbackResultInfo);
				return callbackResultInfo;
			case "Get Parameter Units":
				callbackResultInfo = GetParamUnits(e, callbackResultInfo);
				return callbackResultInfo;
			case "Set Parameter Units":
				callbackResultInfo = SetParamUnits(e, callbackResultInfo);
				return callbackResultInfo;
			case "Is Node Folder":
				callbackResultInfo = IsNodeFolder(e, callbackResultInfo);
				return callbackResultInfo;
			case "Get Test Checked":
				callbackResultInfo = GetTestChecked(e, callbackResultInfo);
				return callbackResultInfo;
			case "Set Test Checked":
				callbackResultInfo = SetTestChecked(e, callbackResultInfo);
				return callbackResultInfo;
			case "Run Test":
				callbackResultInfo = RunTestNode(e, callbackResultInfo);
				return callbackResultInfo;
			case "Get Close Hides UI":
				callbackResultInfo.Message = GetCloseHidesUI();
				callbackResultInfo.Result = true;
				break;
			case "Set Close Hides UI":
				SetCloseHidesUI(bool.Parse(e.Message.ToString()));
				callbackResultInfo.Result = true;
				break;
			case "Resume Tree":
				if (GlobalStatus.Pause)
				{
					HandleToolsClick("Pause");
					callbackResultInfo.Result = true;
				}
				else
				{
					callbackResultInfo.Message = "Unable to resume, the tree was not paused";
					callbackResultInfo.Result = false;
				}
				break;
			}
		}
		catch (Exception ex)
		{
			callbackResultInfo.Message = ex.ToString();
			callbackResultInfo.Result = false;
		}
		return callbackResultInfo;
	}

	private CallbackResultInfo CopyTestNode(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		try
		{
			string[] array = e.Message.ToString().Split(separator, StringSplitOptions.None);
			string destinationFolder = array[0];
			string newName = array[1];
			string testIndex = array[2];
			string position = array[3];
			string text = CopyTest(destinationFolder, newName, testIndex, position);
			if (text != null)
			{
				cInfo.Message = text;
				cInfo.Result = true;
			}
			else
			{
				cInfo.Message = null;
				cInfo.Result = false;
			}
		}
		catch (Exception)
		{
			cInfo.Result = false;
			cInfo.Message = null;
		}
		return cInfo;
	}

	private CallbackResultInfo SetCondition(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		try
		{
			SetConditionInfo condition = (SetConditionInfo)e.Message;
			if (SetCondition(condition))
			{
				cInfo.Result = true;
				cInfo.Message = "Condition set for " + condition.testIndex + ".";
			}
			else
			{
				cInfo.Result = false;
				cInfo.Message = "Failed to set condition.";
			}
		}
		catch (Exception ex)
		{
			cInfo.Result = false;
			cInfo.Message = "Failed to set condition. " + Environment.NewLine + ex.ToString();
		}
		return cInfo;
	}

	public bool SetCondition(SetConditionInfo conditionInfo)
	{
		bool result = false;
		try
		{
			if (Root.TestList.Count > 0 && !string.IsNullOrEmpty(conditionInfo.testIndex))
			{
				BaseTest baseTest = Root.FindTest(conditionInfo.testIndex);
				if (baseTest != null && !(baseTest is RootTest) && (!(baseTest is FolderTest) || conditionInfo.conditionType == TestConditionType.PreCondition))
				{
					if (conditionInfo.conditionType == TestConditionType.PreCondition)
					{
						if (baseTest.Properties.PreCondition != null && baseTest.Properties.PreCondition.Conditions.Count > 0)
						{
							baseTest.Properties.PreCondition.Enabled = true;
							baseTest.Properties.PreCondition.Expression = conditionInfo.formula;
							baseTest.Properties.PreCondition.NegateResult = conditionInfo.RunWhenConditionMet;
							SetTreeChanged();
							ResetTestTree(baseTest);
							result = true;
						}
						else
						{
							TraceMessage.Write(ToString(), "SetCondition failed, no pre-conditions exist for test " + conditionInfo.testIndex, TraceMessageType.Error);
						}
					}
					else if (baseTest.Properties.PostCondition != null && baseTest.Properties.PostCondition.Conditions.Count > 0)
					{
						baseTest.Properties.PostCondition.Enabled = true;
						baseTest.Properties.PostCondition.Expression = conditionInfo.formula;
						baseTest.Properties.PostCondition.Action = (PostConditionActions)conditionInfo.action;
						baseTest.Properties.PostCondition.NegateResult = !conditionInfo.RunWhenConditionMet;
						if (conditionInfo.action == PostConditionAction.Branch)
						{
							string indexFromTestName = GetIndexFromTestName(conditionInfo.branchToTestName, conditionInfo.testIndex);
							if (string.IsNullOrEmpty(indexFromTestName))
							{
								throw new Exception("TestName to branch to is not valid, for Branch action.\nEither the testName doesn't exist in the tree, or the test comes before the node containing the condition.");
							}
							baseTest.Properties.PostCondition.BranchTestIndex = indexFromTestName;
						}
						SetTreeChanged();
						ResetTestTree(baseTest);
						result = true;
					}
					else
					{
						TraceMessage.Write(ToString(), "SetCondition failed, no post-conditions exist for test " + conditionInfo.testIndex, TraceMessageType.Error);
					}
				}
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "SetCondition failed, " + ex.Message, TraceMessageType.Exception);
			throw ex;
		}
		return result;
	}

	private CallbackResultInfo CreateCondition(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		try
		{
			CreateConditionInfo conditionInfo = (CreateConditionInfo)e.Message;
			bool flag = false;
			if (e.Key == "Create Condition On Test Parameter")
			{
				flag = CreateConditionOnTestParam(conditionInfo);
			}
			else if (e.Key == "Create Condition On Test Result")
			{
				flag = CreateConditionOnTestResult(conditionInfo);
			}
			else if (e.Key == "Create Condition On Global Variable")
			{
				flag = CreateConditionOnGlobal(conditionInfo);
			}
			if (flag)
			{
				cInfo.Result = true;
				cInfo.Message = "Test condition " + conditionInfo.conditionName + " created for test " + conditionInfo.testIndex + ".";
			}
			else
			{
				cInfo.Result = false;
				cInfo.Message = "Failed to create test condition.";
			}
		}
		catch (Exception ex)
		{
			cInfo.Result = false;
			cInfo.Message = "Failed to create test condition. " + Environment.NewLine + ex.ToString();
		}
		return cInfo;
	}

	private bool AddConditionToTestNode(CreateConditionInfo conditionInfo, BaseCondition condition)
	{
		bool result = false;
		try
		{
			if (Root.TestList.Count > 0 && !string.IsNullOrEmpty(conditionInfo.testIndex))
			{
				BaseTest baseTest = Root.FindTest(conditionInfo.testIndex);
				if (baseTest != null && !(baseTest is RootTest))
				{
					BaseTest baseTest2 = baseTest;
					if (!(baseTest is FolderTest) || conditionInfo.conditionType == TestConditionType.PreCondition)
					{
						if (conditionInfo.conditionType == TestConditionType.PreCondition)
						{
							TestPreCondition testPreCondition = new TestPreCondition();
							testPreCondition.root = Root;
							condition.Copy(testPreCondition);
							testPreCondition.TestIndex = GetIndexFromTestName(conditionInfo.testName, "");
							if (testPreCondition.IsValid)
							{
								if (baseTest2.Properties.PreCondition == null)
								{
									baseTest2.Properties.PreCondition = new ConditionMgr();
									baseTest2.Properties.PreCondition.Enabled = false;
								}
								if (testPreCondition.TestConditionType == ConditionType.TestParameters)
								{
									BaseTest baseTest3 = Root.FindTest(testPreCondition.TestIndex);
									if (baseTest3 != null && baseTest3 != null && !baseTest3.Properties.Parameters.HasParameter(testPreCondition.ParameterName))
									{
										return false;
									}
								}
								baseTest2.Properties.PreCondition.AddCondition(testPreCondition);
								result = true;
							}
						}
						else
						{
							TestPostCondition testPostCondition = new TestPostCondition();
							testPostCondition.root = Root;
							condition.Copy(testPostCondition);
							if (conditionInfo.conditionBasedOn == ConditionBasedOn.TestParameterValue)
							{
								bool flag = false;
								foreach (ParameterCls parameter in baseTest2.Properties.Parameters)
								{
									if (parameter.Name == conditionInfo.paramName && parameter.IsOutput)
									{
										flag = true;
										break;
									}
								}
								if (!flag)
								{
									TraceMessage.Write(ToString(), "CreateCondition failed, " + baseTest2.Properties.TestName + " has no such output parameter: " + conditionInfo.paramName, TraceMessageType.Error);
									return false;
								}
							}
							if (testPostCondition.IsValid)
							{
								if (baseTest2.Properties.PostCondition == null)
								{
									baseTest2.Properties.PostCondition = new ConditionMgr();
									baseTest2.Properties.PostCondition.Enabled = false;
								}
								baseTest2.Properties.PostCondition.AddCondition(testPostCondition);
								result = true;
							}
						}
					}
				}
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "CreateCondition failed, " + ex.Message, TraceMessageType.Exception);
			throw ex;
		}
		return result;
	}

	public bool CreateConditionOnTestParam(CreateConditionInfo conditionInfo)
	{
		BaseCondition baseCondition = new BaseCondition();
		baseCondition.root = Root;
		baseCondition.Name = conditionInfo.conditionName;
		baseCondition.TestConditionType = ConditionType.TestParameters;
		baseCondition.ParameterName = conditionInfo.paramName;
		baseCondition.ParameterSign = (QC.QsprTestObjects.TestConditions.ConditionSign)conditionInfo.conditionSign;
		switch (conditionInfo.valueType)
		{
		case ConditionValueType.Constant:
			baseCondition.CompareToType = CompareType.Constant;
			baseCondition.ParameterValue = conditionInfo.value;
			break;
		case ConditionValueType.GlobalVariable:
			baseCondition.CompareToType = CompareType.GlobalVariable;
			baseCondition.GVToCompareName = conditionInfo.value;
			break;
		case ConditionValueType.LowerLimit:
			baseCondition.CompareToType = CompareType.LowerLimit;
			break;
		case ConditionValueType.UpperLimit:
			baseCondition.CompareToType = CompareType.UpperLimit;
			break;
		}
		return AddConditionToTestNode(conditionInfo, baseCondition);
	}

	public bool CreateConditionOnTestResult(CreateConditionInfo conditionInfo)
	{
		BaseCondition baseCondition = new BaseCondition();
		baseCondition.root = Root;
		baseCondition.Name = conditionInfo.conditionName;
		baseCondition.TestConditionType = ConditionType.TestResult;
		if (conditionInfo.testResult == TestResultCondition.Passed)
		{
			baseCondition.Result = TestResult.Passed;
		}
		else
		{
			baseCondition.Result = TestResult.Failed;
		}
		return AddConditionToTestNode(conditionInfo, baseCondition);
	}

	public bool CreateConditionOnGlobal(CreateConditionInfo conditionInfo)
	{
		BaseCondition baseCondition = new BaseCondition();
		baseCondition.root = Root;
		baseCondition.Name = conditionInfo.conditionName;
		baseCondition.TestConditionType = ConditionType.GlobalVariables;
		baseCondition.GlobalVariableName = conditionInfo.globalVarName;
		baseCondition.ParameterSign = (QC.QsprTestObjects.TestConditions.ConditionSign)conditionInfo.conditionSign;
		switch (conditionInfo.valueType)
		{
		case ConditionValueType.Constant:
			baseCondition.CompareToType = CompareType.Constant;
			baseCondition.GlobalVariableValue = conditionInfo.value;
			break;
		case ConditionValueType.GlobalVariable:
			baseCondition.CompareToType = CompareType.GlobalVariable;
			baseCondition.GVToCompareName = conditionInfo.value;
			break;
		default:
			return false;
		}
		return AddConditionToTestNode(conditionInfo, baseCondition);
	}

	private string GetVersion()
	{
		AssemblyInformationalVersionAttribute assemblyInformationalVersionAttribute = (AssemblyInformationalVersionAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), inherit: false)[0];
		return assemblyInformationalVersionAttribute.InformationalVersion;
	}

	private void OnTTDPluginMenu(string menuKey)
	{
		OnPluginMenuCmd(this, menuKey);
	}

	private ArrayList GetTesterConfiguration()
	{
		return GetTesterConfig();
	}

	public bool GetTreeResult(out TestResult result)
	{
		result = TestResult.Failed;
		try
		{
			if (Root != null && Root.TestList.Count > 0)
			{
				if (GlobalStatus.Pause)
				{
					result = TestResult.Pausing;
					return true;
				}
				QTMGlobalVariable.GetGlobalVariable("GONOGO_RUNNING", out var gvValue);
				if (!string.IsNullOrEmpty(gvValue) && Convert.ToBoolean(gvValue))
				{
					result = TestResult.Running;
					return true;
				}
				result = GlobalStatus.GlobalTestResult;
				return true;
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "Get Tree Result failed, " + ex.Message, TraceMessageType.Exception);
		}
		return false;
	}

	public bool ImportTests(ImportTestsInfo importInfo)
	{
		try
		{
			bool result = false;
			if (!string.IsNullOrEmpty(importInfo.DestinationName))
			{
				importInfo.DestinationName = GetRegexMatchString(importInfo.DestinationName);
				BaseTest matchingNode = Root;
				while (SearchNodes(matchingNode, importInfo.DestinationName, WorkspaceConfig.SearchOptions.TestName, childrenOnly: false, out matchingNode))
				{
					if (matchingNode != null)
					{
						if (importInfo.InsertInFolder)
						{
							matchingNode.Properties.Expanded = true;
						}
						else
						{
							matchingNode.Properties.Expanded = false;
						}
						if (!string.IsNullOrEmpty(importInfo.FilePath) && ImportTestTree(asLink: false, matchingNode, importInfo.FilePath, null, importInfo))
						{
							result = true;
						}
					}
				}
				return result;
			}
			if (importInfo.InsertInFolder)
			{
				Root.Properties.Expanded = true;
			}
			else
			{
				Root.Properties.Expanded = false;
			}
			if (!string.IsNullOrEmpty(importInfo.FilePath))
			{
				return ImportTestTree(asLink: false, Root, importInfo.FilePath, null, importInfo);
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "ImportTests failed, " + ex.Message, TraceMessageType.Exception);
		}
		return false;
	}

	private CallbackResultInfo SetNodeDescription(NodeDescriptionInfo descInfo)
	{
		CallbackResultInfo cInfo = default(CallbackResultInfo);
		try
		{
			return NodeDescription(descInfo, ref cInfo, getNodesDescription: false);
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "SetNodeDescription failed, " + ex.Message, TraceMessageType.Exception);
		}
		cInfo.Result = false;
		return cInfo;
	}

	private CallbackResultInfo NodeDescription(NodeDescriptionInfo descInfo, ref CallbackResultInfo cInfo, bool getNodesDescription)
	{
		BaseTest matchingNode = null;
		bool result = false;
		if (string.IsNullOrEmpty(descInfo.NodeName))
		{
			matchingNode = Root;
			if (matchingNode != null)
			{
				if (getNodesDescription)
				{
					descInfo.NodeDescription = matchingNode.Properties.Description;
				}
				else
				{
					matchingNode.Properties.Description = descInfo.NodeDescription;
				}
				result = true;
			}
			if (Root.TestList.Count == 1 && Root.Properties.TestName == Root.TestList[0].Properties.TestName)
			{
				matchingNode = Root.TestList[0];
				if (matchingNode != null)
				{
					if (getNodesDescription)
					{
						descInfo.NodeDescription += matchingNode.Properties.Description;
					}
					else
					{
						matchingNode.Properties.Description = descInfo.NodeDescription;
					}
				}
			}
		}
		else
		{
			BaseTest currentNode = Root;
			descInfo.NodeName = GetRegexMatchString(descInfo.NodeName);
			while (SearchNodes(currentNode, descInfo.NodeName, WorkspaceConfig.SearchOptions.TestName, childrenOnly: false, out matchingNode))
			{
				currentNode = matchingNode;
				if (matchingNode != null)
				{
					if (getNodesDescription)
					{
						descInfo.NodeDescription = matchingNode.Properties.Description;
						break;
					}
					matchingNode.Properties.Description = descInfo.NodeDescription;
					result = true;
				}
			}
		}
		cInfo.Message = descInfo;
		cInfo.Result = result;
		return cInfo;
	}

	private CallbackResultInfo GetNodeDescription(NodeDescriptionInfo descInfo)
	{
		CallbackResultInfo cInfo = default(CallbackResultInfo);
		try
		{
			return NodeDescription(descInfo, ref cInfo, getNodesDescription: true);
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "GetNodeDescription failed, " + ex.Message, TraceMessageType.Exception);
		}
		cInfo.Result = false;
		return cInfo;
	}

	private CallbackResultInfo SetNodeNotes(NodeNotesInfo notesInfo)
	{
		CallbackResultInfo cInfo = default(CallbackResultInfo);
		try
		{
			return NodeNotes(notesInfo, ref cInfo, getNotes: false);
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "SetNodeNotes failed, " + ex.Message, TraceMessageType.Exception);
		}
		cInfo.Result = false;
		return cInfo;
	}

	private CallbackResultInfo NodeNotes(NodeNotesInfo notesInfo, ref CallbackResultInfo cInfo, bool getNotes)
	{
		BaseTest matchingNode = null;
		bool result = false;
		if (string.IsNullOrEmpty(notesInfo.NodeName))
		{
			matchingNode = Root;
			if (matchingNode != null)
			{
				if (getNotes)
				{
					notesInfo.NodeNotes = matchingNode.Properties.Notes;
				}
				else
				{
					matchingNode.Properties.Notes = notesInfo.NodeNotes;
				}
				result = true;
			}
			if (Root.TestList.Count == 1 && Root.Properties.TestName == Root.TestList[0].Properties.TestName)
			{
				matchingNode = Root.TestList[0];
				if (matchingNode != null)
				{
					if (getNotes)
					{
						notesInfo.NodeNotes += matchingNode.Properties.Notes;
					}
					else
					{
						matchingNode.Properties.Notes = notesInfo.NodeNotes;
					}
				}
			}
		}
		else
		{
			BaseTest currentNode = Root;
			notesInfo.NodeName = GetRegexMatchString(notesInfo.NodeName);
			while (SearchNodes(currentNode, notesInfo.NodeName, WorkspaceConfig.SearchOptions.TestName, childrenOnly: false, out matchingNode))
			{
				currentNode = matchingNode;
				if (matchingNode != null)
				{
					if (getNotes)
					{
						notesInfo.NodeNotes = matchingNode.Properties.Notes;
						break;
					}
					matchingNode.Properties.Notes = notesInfo.NodeNotes;
					result = true;
				}
			}
		}
		cInfo.Result = result;
		cInfo.Message = notesInfo;
		return cInfo;
	}

	private CallbackResultInfo GetNodeNotes(NodeNotesInfo notesInfo)
	{
		CallbackResultInfo cInfo = default(CallbackResultInfo);
		try
		{
			return NodeNotes(notesInfo, ref cInfo, getNotes: true);
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "GetNodeNotes failed, " + ex.Message, TraceMessageType.Exception);
		}
		cInfo.Result = false;
		return cInfo;
	}

	public bool SetLoopType(LoopInfo loopInfo)
	{
		try
		{
			bool result = false;
			if (Root.TestList.Count > 0 && loopInfo != null && !string.IsNullOrEmpty(loopInfo.FolderName))
			{
				if (loopInfo.LoopType == QC.QTMInterface.LoopTypes.Sequence)
				{
					return SetSequenceLoop(loopInfo);
				}
				if (loopInfo.IsTestIndex)
				{
					BaseTest baseTest = Root.FindTest(loopInfo.FolderName);
					if (baseTest != null)
					{
						if (!(baseTest is FolderTest))
						{
							loopInfo.TestName = baseTest.Properties.TestName;
						}
						result = SetLoopForAutomation(loopInfo, baseTest);
					}
					ResetTestTree(baseTest);
					return result;
				}
				loopInfo.FolderName = GetRegexMatchString(loopInfo.FolderName);
				BaseTest baseTest2 = Root;
				BaseTest matchingNode = null;
				while (SearchNodes(baseTest2, loopInfo.FolderName, WorkspaceConfig.SearchOptions.TestName, childrenOnly: false, out matchingNode))
				{
					baseTest2 = matchingNode;
					if (baseTest2 != null)
					{
						result = SetLoopForAutomation(loopInfo, baseTest2);
						ResetTestTree(baseTest2);
					}
				}
				return result;
			}
			throw new Exception("FolderName or LoopInfo object is not valid.");
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "SetLoopType failed, " + ex.Message, TraceMessageType.Exception);
			throw ex;
		}
	}

	private CallbackResultInfo SetLoopTypeInFolder(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		try
		{
			if (e.Message != null && e.Message is LoopInfo)
			{
				cInfo = SetLoopInFolder((LoopInfo)e.Message);
			}
			else
			{
				cInfo.Result = false;
				cInfo.Message = "Please provide LoopInfo object to the message.";
			}
		}
		catch (Exception ex)
		{
			cInfo.Message = "Failed to set Loop Type " + Environment.NewLine + ex.ToString();
		}
		return cInfo;
	}

	public CallbackResultInfo SetLoopInFolder(LoopInfo loopInfo)
	{
		CallbackResultInfo result = default(CallbackResultInfo);
		try
		{
			bool result2 = false;
			if (Root.TestList.Count <= 0 || loopInfo == null || string.IsNullOrEmpty(loopInfo.FolderName) || string.IsNullOrEmpty(loopInfo.InFolderName))
			{
				throw new Exception("FolderName or LoopInfo object is not valid.");
			}
			List<BaseTest> list = new List<BaseTest>();
			BaseTest matchingNode = Root;
			while (SearchNodes(matchingNode, loopInfo.InFolderName, WorkspaceConfig.SearchOptions.TestName, childrenOnly: false, out matchingNode))
			{
				if (matchingNode is FolderTest && string.Compare(matchingNode.Properties.TestName, loopInfo.InFolderName) == 0 && (matchingNode as FolderTest).TestList.Count > 0)
				{
					list.Add(matchingNode);
				}
			}
			if (list.Count > 0)
			{
				foreach (BaseTest item in list)
				{
					BaseTest matchingNode2 = item;
					while (SearchNodes(matchingNode2, loopInfo.FolderName, WorkspaceConfig.SearchOptions.TestName, childrenOnly: true, out matchingNode2))
					{
						if (matchingNode2 != null && string.Compare(matchingNode2.Properties.TestName, loopInfo.FolderName) == 0 && SetLoopForAutomation(loopInfo, matchingNode2))
						{
							result2 = true;
							ResetTestTree(matchingNode2);
						}
					}
				}
			}
			result.Result = result2;
		}
		catch (Exception ex)
		{
			string text = "SetLoopTypeInFolder failed, " + ex.Message;
			TraceMessage.Write(ToString(), text, TraceMessageType.Exception);
			result.Message = text;
			result.Result = false;
		}
		return result;
	}

	private bool SetLoopForAutomation(LoopInfo loopInfo, BaseTest folderNode)
	{
		LoopScenarioValues loopScenarioValues = null;
		switch (loopInfo.LoopType)
		{
		case QC.QTMInterface.LoopTypes.None:
			loopScenarioValues = null;
			break;
		case QC.QTMInterface.LoopTypes.Forever:
			loopScenarioValues = AssignForeverLoop(loopInfo, folderNode);
			break;
		case QC.QTMInterface.LoopTypes.CountDown:
			loopScenarioValues = AssignCountDownLoop(loopInfo, folderNode);
			break;
		case QC.QTMInterface.LoopTypes.ParameterValue:
			loopScenarioValues = AssignParamValueLoop(loopInfo, folderNode);
			break;
		case QC.QTMInterface.LoopTypes.Sequence:
			loopScenarioValues = AssignSequenceLoop(loopInfo, folderNode);
			break;
		}
		if (folderNode is FolderTest)
		{
			((FolderTest)folderNode).FolderProperties.Scenarios = loopScenarioValues;
			folderNode.UpdateLoopValues();
		}
		else
		{
			folderNode.Properties.Scenarios = loopScenarioValues;
			folderNode.UpdateLoopValues();
		}
		if (loopScenarioValues == null && loopInfo.LoopType != 0)
		{
			return false;
		}
		if (loopScenarioValues != null && loopInfo.LoopType == QC.QTMInterface.LoopTypes.None)
		{
			return false;
		}
		return true;
	}

	private LoopScenarioValues AssignCountDownLoop(LoopInfo loopInfo, BaseTest folderNode)
	{
		LoopScenarioValues loopScenarioValues = new LoopScenarioValues();
		LoopCondition loopCondition = new LoopCondition();
		if (string.IsNullOrEmpty(loopInfo.LoopName))
		{
			loopInfo.LoopName = "CountDownLoop";
		}
		loopCondition.LoopName = loopInfo.LoopName;
		loopCondition.LoopType = QC.QsprTestObjects.Loops.LoopTypes.BYCOUNT;
		loopCondition.LoopValuesAsString = loopInfo.Count.ToString();
		loopCondition.ScenarioID = -1L;
		loopCondition.DataType = DataType.Int;
		loopCondition.OriginalValue = loopInfo.Count.ToString();
		loopScenarioValues.LoopConditions.Add(loopCondition);
		return loopScenarioValues;
	}

	private LoopScenarioValues AssignForeverLoop(LoopInfo loopInfo, BaseTest folderNode)
	{
		LoopScenarioValues loopScenarioValues = new LoopScenarioValues();
		LoopCondition loopCondition = new LoopCondition();
		if (string.IsNullOrEmpty(loopInfo.LoopName))
		{
			loopInfo.LoopName = "ForeverLoop";
		}
		loopCondition.LoopName = loopInfo.LoopName;
		loopCondition.LoopValuesAsString = "Loop Forever";
		loopCondition.LoopType = QC.QsprTestObjects.Loops.LoopTypes.BYCOUNT;
		loopCondition.DataType = DataType.Int;
		loopCondition.ScenarioID = -1L;
		loopCondition.OriginalValue = "Loop Forever";
		loopScenarioValues.LoopConditions.Add(loopCondition);
		loopScenarioValues.ConditionCheckAsString = "True";
		return loopScenarioValues;
	}

	private LoopScenarioValues AssignParamValueLoop(LoopInfo loopInfo, BaseTest folderNode)
	{
		if (string.IsNullOrEmpty(loopInfo.ParameterValue))
		{
			throw new Exception("Parameter Value can't be empty for Parameter Value loop type");
		}
		LoopScenarioValues loopScenarioValues = new LoopScenarioValues();
		LoopCondition loopCondition = new LoopCondition();
		if (string.IsNullOrEmpty(loopInfo.LoopName) && !string.IsNullOrEmpty(loopInfo.ParameterName))
		{
			loopInfo.LoopName = loopInfo.ParameterName;
		}
		else if (string.IsNullOrEmpty(loopInfo.LoopName))
		{
			loopInfo.LoopName = "ParameterValueLoop";
		}
		loopCondition.LoopName = loopInfo.LoopName;
		if (!string.IsNullOrEmpty(loopInfo.ParameterName))
		{
			loopCondition.ParamName = loopInfo.ParameterName;
		}
		if (loopInfo.ParameterValue.ToUpper().Contains("SSS"))
		{
			QC.QsprTestObjects.Loops.LoopTypes assignedLoopType;
			string text = LoopCondition.ParseSSSValue(loopInfo.ParameterValue, out assignedLoopType);
			loopCondition.LoopType = assignedLoopType;
			loopCondition.LoopValuesAsString = text;
			loopCondition.OriginalValue = text;
		}
		else
		{
			loopCondition.LoopType = QC.QsprTestObjects.Loops.LoopTypes.LIST;
			loopCondition.OriginalValue = loopInfo.ParameterValue;
			loopCondition.LoopValuesAsString = loopInfo.ParameterValue;
		}
		if (folderNode is FolderTest)
		{
			if (string.IsNullOrEmpty(loopInfo.TestName))
			{
				loopCondition.TestName = (loopInfo.TestName = "ALLTests");
			}
			else
			{
				loopCondition.TestName = loopInfo.TestName;
				loopCondition.TestIndex = GetIndexFromTestName(loopInfo.TestName, folderNode.Index);
			}
		}
		else
		{
			loopCondition.TestName = folderNode.Properties.TestName;
			loopCondition.TestIndex = folderNode.Index;
		}
		loopCondition.DataType = DataType.Int;
		loopScenarioValues.LoopConditions.Add(loopCondition);
		List<string> list = new List<string>();
		List<string> list2 = new List<string>();
		List<string> list3 = new List<string>();
		string[] array = loopCondition.LoopValuesAsString.Split(',');
		for (int i = 0; i < array.Length; i++)
		{
			_ = array[i];
			list.Add("True");
			list2.Add("");
			list3.Add("");
		}
		loopScenarioValues.ConditionCheckAsString = string.Join(",", list.ToArray());
		loopScenarioValues.DontRunConditionAsString = string.Join(",", list2.ToArray());
		loopScenarioValues.DescriptionsAsString = string.Join(";", list3.ToArray());
		return AppendLoopScenarioValues(loopScenarioValues, folderNode);
	}

	private bool GetSequenceMatch(FolderTest parentNode, string[] testNames, out BaseTest firstMatch)
	{
		firstMatch = null;
		try
		{
			ObservableCollection<BaseTest> testList = parentNode.TestList;
			IEnumerable<BaseTest> enumerable = from node in testList.OfType<BaseTest>()
				where node.Properties.TestName == testNames[0]
				select node;
			foreach (BaseTest item in enumerable)
			{
				bool flag = true;
				if (testNames.Length <= 1)
				{
					return false;
				}
				int num = item.Parent.TestList.IndexOf(item);
				if (item.Parent.TestList.Count <= num)
				{
					continue;
				}
				BaseTest baseTest = item.Parent.TestList[num + 1];
				num++;
				for (int i = 1; i < testNames.Length; i++)
				{
					try
					{
						if (baseTest.Properties.TestName == testNames[i])
						{
							baseTest = baseTest.Parent.TestList[num + 1];
							num++;
							continue;
						}
						flag = false;
					}
					catch
					{
						flag = false;
					}
					break;
				}
				if (flag)
				{
					firstMatch = item;
					return true;
				}
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "GetSequenceMatch failed, " + ex.ToString(), TraceMessageType.Exception);
			throw ex;
		}
		return false;
	}

	private bool SetSequenceLoop(LoopInfo loopInfo)
	{
		try
		{
			if (loopInfo.LoopType != QC.QTMInterface.LoopTypes.Sequence)
			{
				return false;
			}
			string text = "";
			text = ((!loopInfo.IsTestIndex) ? GetIndexFromTestName(loopInfo.FolderName, "") : loopInfo.FolderName);
			BaseTest baseTest = Root.FindTest(text);
			string[] array = loopInfo.ParameterValue.Split(new string[1] { "," }, StringSplitOptions.RemoveEmptyEntries);
			if (!(baseTest is FolderTest))
			{
				return false;
			}
			BaseTest firstMatch = null;
			if (GetSequenceMatch(baseTest as FolderTest, array, out firstMatch))
			{
				loopInfo.TestName = string.Join("_;_", array);
				loopInfo.ParameterValue = firstMatch.Index;
				BaseTest baseTest2 = firstMatch;
				for (int i = 1; i < array.Length; i++)
				{
					int num = baseTest2.Parent.TestList.IndexOf(baseTest2);
					baseTest2 = baseTest2.Parent.TestList[num + 1];
					loopInfo.ParameterValue = loopInfo.ParameterValue + ',' + baseTest2.Index;
				}
				loopInfo.Count = array.Length;
				if (SetLoopForAutomation(loopInfo, baseTest))
				{
					return true;
				}
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "SetSequenceLoop failed, " + ex.ToString(), TraceMessageType.Exception);
			throw ex;
		}
		return false;
	}

	private LoopScenarioValues AssignSequenceLoop(LoopInfo loopInfo, BaseTest folderNode)
	{
		LoopScenarioValues loopScenarioValues = new LoopScenarioValues();
		LoopCondition loopCondition = new LoopCondition();
		LoopScenarioValues scenarios = (folderNode as FolderTest).Properties.Scenarios;
		if (scenarios != null && scenarios.LoopConditions != null && scenarios.LoopConditions.Count > 0 && (scenarios.LoopConditions[0] as LoopCondition).LoopType != QC.QsprTestObjects.Loops.LoopTypes.BYSEQUENCE)
		{
			return null;
		}
		if (string.IsNullOrEmpty(loopInfo.ParameterName))
		{
			loopInfo.LoopName = "Sequence";
		}
		else
		{
			loopInfo.LoopName = loopInfo.ParameterName;
		}
		loopInfo.LoopName = ScenarioHandler.GetUniqueLoopName(scenarios, loopInfo.LoopName);
		loopCondition.LoopName = loopInfo.LoopName;
		loopCondition.TestName = loopInfo.TestName;
		loopCondition.DataType = DataType.String;
		loopCondition.LoopType = QC.QsprTestObjects.Loops.LoopTypes.BYSEQUENCE;
		loopCondition.LoopOnType = LoopOnTypes.INPUTPARAM;
		loopCondition.ScenarioID = -1L;
		loopCondition.TestIndex = loopInfo.ParameterValue;
		int[] list = Enumerable.Range(0, loopInfo.Count).ToArray();
		IEnumerable<IEnumerable<int>> source = Permutation.Permute(list);
		for (int i = 0; i < source.Count(); i++)
		{
			IEnumerable<int> source2 = source.ToArray()[i];
			int[] values = source2.ToArray();
			if (i == 0)
			{
				loopCondition.LoopValuesAsString = string.Join("_", values);
			}
			else
			{
				loopCondition.LoopValuesAsString = loopCondition.LoopValuesAsString + "," + string.Join("_", values);
			}
		}
		loopScenarioValues.LoopConditions.Add(loopCondition);
		loopScenarioValues.ConditionCheckAsString = "True";
		return loopScenarioValues;
	}

	private LoopScenarioValues AppendLoopScenarioValues(LoopScenarioValues newLoopScenarioValues, BaseTest folderNode)
	{
		LoopScenarioValues loopScenarioValues = null;
		loopScenarioValues = ((!(folderNode is FolderTest)) ? folderNode.Properties.Scenarios : ((FolderTest)folderNode).FolderProperties.Scenarios);
		if (loopScenarioValues == null)
		{
			return newLoopScenarioValues;
		}
		if (loopScenarioValues.Count > 0)
		{
			QC.QsprTestObjects.Loops.LoopTypes loopType = ((LoopCondition)loopScenarioValues.LoopConditions[0]).LoopType;
			if (loopType == QC.QsprTestObjects.Loops.LoopTypes.LIST || loopType == QC.QsprTestObjects.Loops.LoopTypes.STARTSTOPSTEP)
			{
				foreach (LoopCondition loopCondition in newLoopScenarioValues.LoopConditions)
				{
					bool flag = true;
					for (int i = 0; i < loopScenarioValues.LoopConditions.Count; i++)
					{
						if (loopCondition.LoopName == ((LoopCondition)loopScenarioValues.LoopConditions[i]).LoopName || loopCondition.ParamName == ((LoopCondition)loopScenarioValues.LoopConditions[i]).ParamName)
						{
							loopScenarioValues.LoopConditions[i] = loopCondition;
							flag = false;
						}
					}
					if (flag)
					{
						loopScenarioValues.LoopConditions.Add(loopCondition);
					}
					flag = true;
				}
				return loopScenarioValues;
			}
			return newLoopScenarioValues;
		}
		return newLoopScenarioValues;
	}

	private CallbackResultInfo SuppressSnDialog(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		if (e.Message != null && !string.IsNullOrEmpty(e.Message.ToString()))
		{
			QTMData.WConfig.SuppressSerialNumberDialog = (bool)e.Message;
			cInfo.Result = true;
		}
		return cInfo;
	}

	private CallbackResultInfo SuppressResultsDialog(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		if (e.Message != null && !string.IsNullOrEmpty(e.Message.ToString()))
		{
			QTMData.WConfig.SuppressResultsDialog = (bool)e.Message;
			cInfo.Result = true;
		}
		return cInfo;
	}

	private CallbackResultInfo SetBackgroundColor(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		try
		{
			UpdateWindowBG((Color)e.Message);
			cInfo.Result = true;
		}
		catch (Exception ex)
		{
			cInfo.Message = "Failed to set window background. Please provide a valid color from System.Drawing.Color. " + Environment.NewLine + ex.ToString();
		}
		return cInfo;
	}

	private CallbackResultInfo RunTestNode(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		if (e.Message != null)
		{
			cInfo = RunTest(e.Message.ToString());
		}
		return cInfo;
	}

	private CallbackResultInfo SetTestChecked(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		if (e.Message != null)
		{
			string[] array = e.Message.ToString().Split(separator, StringSplitOptions.None);
			cInfo.Result = SetTestChecked(array[0], int.Parse(array[1]));
		}
		return cInfo;
	}

	private CallbackResultInfo GetTestChecked(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		if (e.Message != null)
		{
			cInfo.Message = GetTestChecked(e.Message.ToString());
			cInfo.Result = true;
		}
		return cInfo;
	}

	private CallbackResultInfo IsNodeFolder(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		if (e.Message != null)
		{
			if (IsNodeAFolder(e.Message.ToString()))
			{
				cInfo.Message = 1;
			}
			else
			{
				cInfo.Message = 0;
			}
			cInfo.Result = true;
		}
		return cInfo;
	}

	internal bool IsNodeAFolder(string testIndex)
	{
		bool result = false;
		try
		{
			if (Root.TestList.Count > 0 && !string.IsNullOrEmpty(testIndex))
			{
				BaseTest baseTest = Root.FindTest(testIndex);
				if (baseTest != null)
				{
					return baseTest is FolderTest;
				}
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "IsNodeAFolder failed, " + ex.Message, TraceMessageType.Exception);
			throw ex;
		}
		return result;
	}

	internal int GetTestChecked(string testIndex)
	{
		int result = 0;
		try
		{
			if (Root.TestList.Count > 0 && !string.IsNullOrEmpty(testIndex))
			{
				BaseTest baseTest = Root.FindTest(testIndex);
				if (baseTest != null)
				{
					CheckState checkStateTest = baseTest.Properties.CheckStateTest;
					if (checkStateTest == CheckState.Checked || checkStateTest == CheckState.Indeterminate)
					{
						return 1;
					}
					return 0;
				}
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "GetTestChecked failed, " + ex.Message, TraceMessageType.Exception);
			throw ex;
		}
		return result;
	}

	internal bool SetTestChecked(string testIndex, int checkedState)
	{
		bool result = false;
		try
		{
			if (Root.TestList.Count > 0 && !string.IsNullOrEmpty(testIndex))
			{
				BaseTest baseTest = Root.FindTest(testIndex);
				if (baseTest != null)
				{
					if (checkedState == 1)
					{
						baseTest.Properties.CheckStateTest = CheckState.Checked;
					}
					else
					{
						baseTest.Properties.CheckStateTest = CheckState.Unchecked;
					}
					SetTreeChanged();
					Root.Scheduler.GlobalStatus.RegistrationStatus.TreeSelectionChanged = true;
					result = true;
				}
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "SetTestChecked failed, " + ex.Message, TraceMessageType.Exception);
			throw ex;
		}
		return result;
	}

	private CallbackResultInfo SetParamUnits(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		if (e.Message != null && e.Message is ParamInfo)
		{
			ParamInfo paramInfo = (ParamInfo)e.Message;
			cInfo = SetTestParameterValue(paramInfo, e.Key);
		}
		return cInfo;
	}

	internal CallbackResultInfo SetTestParameterValue(ParamInfo paramInfo, string messageKey)
	{
		CallbackResultInfo result = default(CallbackResultInfo);
		result.Result = false;
		result.Message = "";
		try
		{
			if (!string.IsNullOrEmpty(paramInfo.ParameterName) && !string.IsNullOrEmpty(paramInfo.TestName) && Root.TestList.Count > 0 && paramInfo.IsTestIndex)
			{
				BaseTest baseTest = Root.FindTest(paramInfo.TestName);
				if (baseTest != null && baseTest.Properties.Parameters.HasParameter(paramInfo.ParameterName))
				{
					ParameterCls parameter = baseTest.Properties.Parameters.GetParameter(paramInfo.ParameterName);
					if (messageKey == "Set Parameter Limit")
					{
						if (paramInfo.LowerLimit != null)
						{
							parameter.LowerLimit = paramInfo.LowerLimit;
							result.Result = true;
						}
						else if (paramInfo.UpperLimit != null)
						{
							parameter.UpperLimit = paramInfo.UpperLimit;
							result.Result = true;
						}
						else
						{
							result.Message = "Error setting parameter limit, invalid value";
							result.Result = false;
						}
						return result;
					}
					if (messageKey == "Set Parameter Units")
					{
						if (paramInfo.Units != null)
						{
							parameter.Unit = paramInfo.Units;
							result.Result = true;
						}
						else
						{
							result.Message = "Error setting parameter untis, invalid value";
							result.Result = false;
						}
						return result;
					}
				}
			}
		}
		catch (Exception ex)
		{
			result.Message = "Error setting test param value";
			result.Result = false;
			TraceMessage.Write(ToString(), "GetTestParameterValue failed, " + ex.Message, TraceMessageType.Exception);
		}
		return result;
	}

	private CallbackResultInfo GetParamUnits(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		if (e.Message != null && e.Message is ParamInfo)
		{
			ParamInfo paramInfo = (ParamInfo)e.Message;
			string text = GetTestParameterValue(paramInfo, e.Key).ToString();
			if (text != null)
			{
				cInfo.Message = text;
				cInfo.Result = true;
			}
			else
			{
				cInfo.Message = null;
				cInfo.Result = false;
			}
		}
		return cInfo;
	}

	private CallbackResultInfo SetParamLimit(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		if (e.Message != null && e.Message is ParamInfo)
		{
			ParamInfo paramInfo = (ParamInfo)e.Message;
			cInfo = SetTestParameterValue(paramInfo, e.Key);
		}
		return cInfo;
	}

	private CallbackResultInfo GetParamLimit(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		if (e.Message != null && e.Message is ParamInfo)
		{
			ParamInfo paramInfo = (ParamInfo)e.Message;
			string text = GetTestParameterValue(paramInfo, e.Key).ToString();
			if (text != null)
			{
				cInfo.Message = text;
				cInfo.Result = true;
			}
			else
			{
				cInfo.Message = null;
				cInfo.Result = false;
			}
		}
		return cInfo;
	}

	private CallbackResultInfo GetParentNode(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		if (e.Message != null && e.Message is string)
		{
			string testIndex = e.Message.ToString();
			BaseTest baseTest = Root.FindTest(testIndex);
			if (baseTest != null)
			{
				string index = baseTest.Parent.Index;
				cInfo.Message = index;
				cInfo.Result = true;
			}
		}
		return cInfo;
	}

	private CallbackResultInfo GetNextNode(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		if (e.Message != null && e.Message is GetNextNodeInfo)
		{
			GetNextNodeInfo nodeInfo = (GetNextNodeInfo)e.Message;
			string nextNode = GetNextNode(nodeInfo);
			cInfo.Message = nextNode;
			cInfo.Result = true;
		}
		return cInfo;
	}

	private string GetNextNode(GetNextNodeInfo nodeInfo)
	{
		string result = null;
		try
		{
			if (!string.IsNullOrEmpty(nodeInfo.currentIndex))
			{
				BaseTest baseTest = null;
				baseTest = ((!(Root.Index == nodeInfo.currentIndex)) ? Root.FindTest(nodeInfo.currentIndex) : Root);
				if (baseTest != null)
				{
					return GetNextNode(baseTest, nodeInfo.nodePosition);
				}
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "GetNextNode failed, " + ex.Message, TraceMessageType.Exception);
			throw ex;
		}
		return result;
	}

	private string GetNextNode(BaseTest node, NextNodePosition pos)
	{
		if (node == null)
		{
			return null;
		}
		switch (pos)
		{
		case NextNodePosition.NextNode:
		{
			string text = null;
			if ((text = GetNextNode(node, NextNodePosition.FirstChild)) != null)
			{
				return text;
			}
			if ((text = GetNextNode(node, NextNodePosition.NextSibling)) != null)
			{
				return text;
			}
			while (node != Root)
			{
				if (node.Parent != null)
				{
					node = node.Parent;
					if ((text = GetNextNode(node, NextNodePosition.NextSibling)) != null)
					{
						return text;
					}
					continue;
				}
				return null;
			}
			return null;
		}
		case NextNodePosition.FirstChild:
			if (node is FolderTest)
			{
				FolderTest folderTest = node as FolderTest;
				if (folderTest.TestList.Count > 0)
				{
					return folderTest.TestList[0].Index;
				}
				return null;
			}
			return null;
		case NextNodePosition.LastChild:
			if (node is FolderTest)
			{
				FolderTest folderTest2 = node as FolderTest;
				if (folderTest2.TestList.Count > 0)
				{
					return folderTest2.TestList[folderTest2.TestList.Count - 1].Index;
				}
				return null;
			}
			return null;
		case NextNodePosition.PreviousSibling:
			if (node.Parent != null && node.Parent.TestList.Count > 1)
			{
				int num2 = node.Parent.TestList.IndexOf(node);
				if (num2 > 0)
				{
					return node.Parent.TestList[num2 - 1].Index;
				}
				return null;
			}
			return null;
		case NextNodePosition.NextSibling:
			if (node.Parent != null && node.Parent.TestList.Count > 1)
			{
				int num = node.Parent.TestList.IndexOf(node);
				if (num < node.Parent.TestList.Count - 1)
				{
					return node.Parent.TestList[num + 1].Index;
				}
				return null;
			}
			return null;
		default:
			return null;
		}
	}

	private CallbackResultInfo GetTestPropertyValue(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		if (e.Message != null && e.Message is TestPropertyInfo)
		{
			TestPropertyInfo propertyInfo = (TestPropertyInfo)e.Message;
			cInfo.Message = GetTestPropertyValue(propertyInfo);
			cInfo.Result = cInfo.Message != null;
		}
		return cInfo;
	}

	private object GetTestPropertyValue(TestPropertyInfo propertyInfo)
	{
		object obj = null;
		try
		{
			if (!string.IsNullOrEmpty(propertyInfo.testIndex))
			{
				BaseTest baseTest = null;
				baseTest = ((!(Root.Index == propertyInfo.testIndex)) ? Root.FindTest(propertyInfo.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)[propertyInfo.propertyName];
					obj = propertyDescriptor.GetValue(propertyGrid.SelectedObject);
					if (propertyInfo.convertToString)
					{
						obj = obj.ToString();
					}
				}
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "GetTestPropertyValue failed, " + ex.Message, TraceMessageType.Exception);
			throw ex;
		}
		return obj;
	}

	private CallbackResultInfo GetTestParamValue(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		if (e.Message != null && e.Message is ParamInfo)
		{
			ParamInfo paramInfo = (ParamInfo)e.Message;
			cInfo.Message = GetTestParameterValue(paramInfo, e.Key);
			cInfo.Result = cInfo.Message != null;
		}
		return cInfo;
	}

	private CallbackResultInfo SetParamLimits(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		if (e.Message != null && e.Message is ParamInfo)
		{
			ParamInfo paramInfo = (ParamInfo)e.Message;
			cInfo = SetTestParamLimits(paramInfo);
		}
		return cInfo;
	}

	internal object GetTestParameterValue(ParamInfo paramInfo, string messageKey)
	{
		object result = null;
		try
		{
			if (!string.IsNullOrEmpty(paramInfo.ParameterName) && !string.IsNullOrEmpty(paramInfo.TestName) && Root.TestList.Count > 0 && paramInfo.IsTestIndex)
			{
				BaseTest baseTest = Root.FindTest(paramInfo.TestName);
				if (baseTest != null)
				{
					if (baseTest.Properties.Parameters.HasParameter(paramInfo.ParameterName))
					{
						ParameterCls parameter = baseTest.Properties.Parameters.GetParameter(paramInfo.ParameterName);
						switch (messageKey)
						{
						case "Get Test Parameter Value":
							if (!paramInfo.ConvertToString)
							{
								try
								{
									TypeConverter converter = TypeDescriptor.GetConverter(Type.GetType(parameter.DataTypeAsString, throwOnError: false, ignoreCase: true));
									result = converter.ConvertFromString(parameter.Value);
								}
								catch (Exception)
								{
									if (parameter.Value != null)
									{
										result = parameter.Value;
									}
								}
							}
							else
							{
								result = parameter.Value;
							}
							break;
						case "Get Parameter Limit":
							if (paramInfo.ParameterValue == "0")
							{
								result = parameter.LowerLimit;
							}
							else if (paramInfo.ParameterValue == "1")
							{
								result = parameter.UpperLimit;
							}
							break;
						case "Get Parameter Units":
							result = parameter.Unit;
							break;
						}
					}
					return result;
				}
			}
		}
		catch (Exception ex2)
		{
			TraceMessage.Write(ToString(), "GetTestParameterValue failed, " + ex2.Message, TraceMessageType.Exception);
		}
		return result;
	}

	private CallbackResultInfo AddScriptParameter(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		try
		{
			AddScriptParamInfo scriptParamInfo = (AddScriptParamInfo)e.Message;
			if (AddScriptParam(scriptParamInfo))
			{
				cInfo.Result = true;
				cInfo.Message = "Script parameter added to test: " + scriptParamInfo.scriptTestName + ".";
			}
			else
			{
				cInfo.Result = false;
				cInfo.Message = "Failed to add test script parameter.";
			}
		}
		catch (Exception ex)
		{
			cInfo.Result = false;
			cInfo.Message = "Failed to add test script parameter. " + ex.Message + ex.InnerException;
		}
		return cInfo;
	}

	private CallbackResultInfo ExportToTextFile(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		try
		{
			ExportNodeInfo exportInfo = (ExportNodeInfo)e.Message;
			if (ExportNodeToTextFile(exportInfo))
			{
				cInfo.Result = true;
				cInfo.Message = "Test node exported: " + exportInfo.testIndex + ".";
			}
			else
			{
				cInfo.Result = false;
				cInfo.Message = "Failed to export test node.";
			}
		}
		catch (Exception ex)
		{
			cInfo.Result = false;
			cInfo.Message = "Failed to export test node. " + ex.Message + ex.InnerException;
		}
		return cInfo;
	}

	private CallbackResultInfo SetTestProperty(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		try
		{
			TestPropertyInfo testProperty = (TestPropertyInfo)e.Message;
			if (SetTestProperty(testProperty))
			{
				cInfo.Result = true;
				cInfo.Message = "Test property set for " + testProperty.testIndex + ".";
			}
			else
			{
				cInfo.Result = false;
				cInfo.Message = "Failed to set test property.";
			}
		}
		catch (Exception ex)
		{
			cInfo.Result = false;
			cInfo.Message = "Failed to set test property. " + ex.Message + ex.InnerException;
		}
		return cInfo;
	}

	public bool SetTestProperty(TestPropertyInfo propertyInfo)
	{
		bool result = false;
		try
		{
			if (!string.IsNullOrEmpty(propertyInfo.testIndex))
			{
				BaseTest baseTest = Root.FindTest(propertyInfo.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)[propertyInfo.propertyName];
					propertyDescriptor.GetValue(propertyGrid.SelectedObject);
					string text = propertyInfo.value.ToString();
					TypeConverter converter = TypeDescriptor.GetConverter(propertyDescriptor.PropertyType);
					if (converter is BooleanConverter)
					{
						if (text == "0")
						{
							text = "False";
						}
						else if (text == "1")
						{
							text = "True";
						}
					}
					object value = converter.ConvertFromString(text);
					propertyDescriptor.SetValue(propertyGrid.SelectedObject, value);
					try
					{
						GridItem gridItem = propertyGrid.SelectedGridItem;
						while (gridItem.Parent != null)
						{
							gridItem = gridItem.Parent;
						}
						GridItem gridItem2 = gridItem.GridItems[propertyDescriptor.Category].GridItems[propertyInfo.propertyName];
						if (gridItem2 != null)
						{
							gridItem2.Select();
						}
						else
						{
							TraceMessage.Write(ToString(), "TestProperty not found: Category=" + propertyDescriptor.Category + " PropertyName=" + propertyInfo.propertyName, TraceMessageType.Exception);
						}
					}
					catch (Exception ex)
					{
						TraceMessage.Write(ToString(), "SetTestProperty exception: " + ex.Message, TraceMessageType.Exception);
					}
					result = true;
				}
			}
		}
		catch (Exception ex2)
		{
			TraceMessage.Write(ToString(), "SetTestProperty failed, " + ex2.Message, TraceMessageType.Exception);
			throw ex2;
		}
		return result;
	}

	private CallbackResultInfo SetConditionActive(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		try
		{
			SetConditionInfo conditionActive = (SetConditionInfo)e.Message;
			if (SetConditionActive(conditionActive))
			{
				cInfo.Result = true;
				cInfo.Message = "Condition set for " + conditionActive.testIndex + ".";
			}
			else
			{
				cInfo.Result = false;
				cInfo.Message = "Failed to set condition.";
			}
		}
		catch (Exception ex)
		{
			cInfo.Result = false;
			cInfo.Message = "Failed to set condition. " + ex.Message + ex.InnerException;
		}
		return cInfo;
	}

	public bool SetConditionActive(SetConditionInfo conditionInfo)
	{
		bool result = false;
		try
		{
			if (Root.TestList.Count > 0 && !string.IsNullOrEmpty(conditionInfo.testIndex))
			{
				BaseTest baseTest = Root.FindTest(conditionInfo.testIndex);
				if (baseTest != null && !(baseTest is RootTest) && (!(baseTest is FolderTest) || conditionInfo.conditionType == TestConditionType.PreCondition))
				{
					if (conditionInfo.conditionType == TestConditionType.PreCondition)
					{
						if (baseTest.Properties.PreCondition != null && baseTest.Properties.PreCondition.Conditions.Count > 0)
						{
							baseTest.Properties.PreCondition.Enabled = conditionInfo.conditionActive;
							SetTreeChanged();
							ResetTestTree(baseTest);
							result = true;
						}
					}
					else if (baseTest.Properties.PostCondition != null && baseTest.Properties.PostCondition.Conditions.Count > 0)
					{
						baseTest.Properties.PostCondition.Enabled = conditionInfo.conditionActive;
						SetTreeChanged();
						ResetTestTree(baseTest);
						result = true;
					}
				}
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "SetConditionActive failed, " + ex.Message, TraceMessageType.Exception);
			throw ex;
		}
		return result;
	}

	private CallbackResultInfo EnableRegex(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		try
		{
			int result = 0;
			if (int.TryParse(e.Message.ToString(), out result))
			{
				if (result == 0)
				{
					GlobalStatus.RegexSupportEnabled = false;
					cInfo.Message = "Regular expression support disabled";
				}
				else
				{
					GlobalStatus.RegexSupportEnabled = true;
					cInfo.Message = "Regular expression support enabled";
				}
				cInfo.Result = true;
			}
			else
			{
				cInfo.Message = "Failed to enable/disable regular expression support. Enable flag is not an integer value.";
				cInfo.Result = false;
			}
		}
		catch (Exception ex)
		{
			cInfo.Result = false;
			cInfo.Message = "Failed to enable/disable regular expression support. " + Environment.NewLine + ex.ToString();
		}
		return cInfo;
	}

	private CallbackResultInfo EnableDllPaths(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		try
		{
			int result = 0;
			if (int.TryParse(e.Message.ToString(), out result))
			{
				if (result == 0)
				{
					QTMData.WConfig.UseDllSearch = false;
					cInfo.Message = "Dll search paths are disabled";
				}
				else
				{
					QTMData.WConfig.UseDllSearch = true;
					cInfo.Message = "Dll search paths are enabled";
				}
				cInfo.Result = true;
			}
			else
			{
				cInfo.Message = "Failed to enable/disable dll search paths. Enable flag is not an integer value.";
				cInfo.Result = false;
			}
		}
		catch (Exception ex)
		{
			cInfo.Result = false;
			cInfo.Message = "Failed to enable/disable dll search paths. " + Environment.NewLine + ex.ToString();
		}
		return cInfo;
	}

	private CallbackResultInfo AddDllPath(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		try
		{
			string text = e.Message.ToString();
			if (QTMData.WConfig.DLLPaths == null)
			{
				QTMData.WConfig.DLLPaths = new List<string>();
			}
			bool flag = true;
			foreach (string dLLPath in QTMData.WConfig.DLLPaths)
			{
				if (dLLPath.ToUpper() == text.ToUpper())
				{
					flag = false;
					break;
				}
			}
			if (flag)
			{
				QTMData.WConfig.DLLPaths.Add(text);
				cInfo.Message = "Added dll path: " + text;
			}
			else
			{
				cInfo.Message = "Dll path already exists in the list: " + text;
			}
			cInfo.Result = true;
		}
		catch (Exception ex)
		{
			cInfo.Result = false;
			cInfo.Message = "Failed to add dll path " + Environment.NewLine + ex.ToString();
		}
		return cInfo;
	}

	private CallbackResultInfo AddTestNode(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		try
		{
			AddTestInfo newTestInfo = (AddTestInfo)e.Message;
			string text = AddTest(newTestInfo);
			if (text != null)
			{
				cInfo.Message = text;
				cInfo.Result = true;
			}
			else
			{
				cInfo.Message = null;
				cInfo.Result = false;
			}
		}
		catch (Exception)
		{
			cInfo.Result = false;
			cInfo.Message = null;
		}
		return cInfo;
	}

	private CallbackResultInfo AddTestContainer(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		try
		{
			if (((QTMRegistry)QTMData.theRegistry).Targets.Count > 0)
			{
				AddTestContainerInfo addTestContainerInfo = (AddTestContainerInfo)e.Message;
				if (!File.Exists(addTestContainerInfo.containerPath))
				{
					cInfo.Message = "Failed to add test container. The path doesn't exist: " + addTestContainerInfo.containerPath;
					cInfo.Result = false;
				}
				else if (AddContainer((QC.QsprTestObjects.Tests.Base.QTMTestTypes)addTestContainerInfo.testType, new string[1] { addTestContainerInfo.containerPath }))
				{
					cInfo.Message = "Added test container " + addTestContainerInfo.containerPath;
					cInfo.Result = true;
				}
				else
				{
					cInfo.Message = "Failed to add test container " + addTestContainerInfo.containerPath;
					cInfo.Result = false;
				}
			}
			else
			{
				cInfo.Message = "Failed to add test container, no targets exist in the tree registry";
				cInfo.Result = false;
			}
		}
		catch (Exception ex)
		{
			cInfo.Result = false;
			cInfo.Message = "Failed to add test container " + Environment.NewLine + ex.ToString();
		}
		return cInfo;
	}

	private CallbackResultInfo GetTestIndex(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		try
		{
			if (e.Message is string testName)
			{
				string indexFromTestName = GetIndexFromTestName(testName, "");
				if (indexFromTestName != null)
				{
					cInfo.Message = indexFromTestName;
					cInfo.Result = true;
				}
				else
				{
					cInfo.Message = null;
					cInfo.Result = false;
				}
			}
		}
		catch (Exception ex)
		{
			cInfo.Message = "Failed to get test index " + Environment.NewLine + ex.ToString();
		}
		return cInfo;
	}

	public string GetIndexFromTestName(string testName, string indexToStartFrom)
	{
		try
		{
			if (testName == "")
			{
				BaseTest root = Root;
				if (root != null)
				{
					return root.Index;
				}
			}
			else if (testName != null)
			{
				BaseTest baseTest = null;
				baseTest = ((!string.IsNullOrEmpty(indexToStartFrom)) ? Root.FindTest(indexToStartFrom) : Root);
				testName = GetRegexMatchString(testName);
				BaseTest baseTest2 = baseTest;
				while (SearchNodes(baseTest2, testName, WorkspaceConfig.SearchOptions.TestName, childrenOnly: false, out baseTest))
				{
					baseTest2 = baseTest;
					if (baseTest2 != null)
					{
						return baseTest2.Index;
					}
				}
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "GetIndexFromTestName failed, " + ex.Message, TraceMessageType.Exception);
		}
		return null;
	}

	private CallbackResultInfo SynchronizeTest(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		try
		{
			string testName = e.Message as string;
			cInfo = SyncTest(testName);
		}
		catch (Exception ex)
		{
			cInfo.Message = "Failed to synchronize" + Environment.NewLine + ex.ToString();
		}
		return cInfo;
	}

	private CallbackResultInfo GetLoopParamName(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		try
		{
			if (e.Message != null)
			{
				string[] array = e.Message.ToString().Split(separator, StringSplitOptions.None);
				string paramLoopedName = GetParamLoopedName(array[0], array[1], isParamName: true);
				cInfo.Result = true;
				cInfo.Message = paramLoopedName;
			}
			else
			{
				cInfo.Result = false;
				cInfo.Message = "The parameters were null.";
			}
		}
		catch (Exception ex)
		{
			cInfo.Message = "Failed to get the parameter looped test name " + Environment.NewLine + ex.ToString();
		}
		return cInfo;
	}

	internal string GetLoopRowsEnabled(string testIndex)
	{
		try
		{
			BaseTest baseTest = Root.FindTest(testIndex);
			if (baseTest != null)
			{
				string text = "";
				for (int i = 0; i < baseTest.Properties.Scenarios.ConditionCheck.Count; i++)
				{
					if ((bool)baseTest.Properties.Scenarios.ConditionCheck[i])
					{
						text = ((!(text == "")) ? (text + "," + (i + 1)) : (i + 1).ToString());
					}
				}
				return text;
			}
		}
		catch
		{
		}
		return null;
	}

	internal string GetParamLoopedName(string testIndex, string loopName, bool isParamName)
	{
		try
		{
			BaseTest baseTest = Root.FindTest(testIndex);
			if (baseTest != null)
			{
				foreach (object loopCondition in baseTest.Properties.Scenarios.LoopConditions)
				{
					if (((LoopCondition)loopCondition).LoopName == loopName)
					{
						if (isParamName)
						{
							return ((LoopCondition)loopCondition).ParamName;
						}
						if (((LoopCondition)loopCondition).TestName != "ALLTests")
						{
							return ((LoopCondition)loopCondition).TestName;
						}
						return "";
					}
				}
			}
		}
		catch
		{
		}
		return null;
	}

	private CallbackResultInfo GetParamLoopedTestName(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		try
		{
			if (e.Message != null)
			{
				string[] array = e.Message.ToString().Split(separator, StringSplitOptions.None);
				string paramLoopedName = GetParamLoopedName(array[0], array[1], isParamName: false);
				cInfo.Result = true;
				cInfo.Message = paramLoopedName;
			}
			else
			{
				cInfo.Result = false;
				cInfo.Message = "The parameters were null.";
			}
		}
		catch (Exception ex)
		{
			cInfo.Message = "Failed to get the parameter looped test name " + Environment.NewLine + ex.ToString();
		}
		return cInfo;
	}

	private CallbackResultInfo GetLoopRowsEnabled(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		try
		{
			if (e.Message != null)
			{
				string loopRowsEnabled = GetLoopRowsEnabled(e.Message.ToString());
				cInfo.Result = true;
				cInfo.Message = loopRowsEnabled;
			}
			else
			{
				cInfo.Result = false;
				cInfo.Message = "The test index is null.";
			}
		}
		catch (Exception ex)
		{
			cInfo.Message = "Failed to get loop names " + Environment.NewLine + ex.ToString();
		}
		return cInfo;
	}

	private CallbackResultInfo GetLoopNames(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		try
		{
			if (e.Message != null)
			{
				string loopNames = GetLoopNames(e.Message.ToString());
				cInfo.Result = true;
				cInfo.Message = loopNames;
			}
			else
			{
				cInfo.Result = false;
				cInfo.Message = "The test index is null.";
			}
		}
		catch (Exception ex)
		{
			cInfo.Message = "Failed to get loop names " + Environment.NewLine + ex.ToString();
		}
		return cInfo;
	}

	internal string GetLoopNames(string testIndex)
	{
		try
		{
			BaseTest baseTest = Root.FindTest(testIndex);
			if (baseTest != null)
			{
				string text = "";
				foreach (object loopCondition in baseTest.Properties.Scenarios.LoopConditions)
				{
					text = ((!(text == "")) ? (text + "," + ((LoopCondition)loopCondition).LoopName) : ((LoopCondition)loopCondition).LoopName);
				}
				return text;
			}
		}
		catch
		{
		}
		return null;
	}

	private CallbackResultInfo GetLoopValue(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		try
		{
			if (e.Message != null && e.Message is LoopRowInfo)
			{
				string message = GetLoopValues((LoopRowInfo)e.Message);
				cInfo.Result = true;
				cInfo.Message = message;
			}
			else
			{
				cInfo.Result = false;
				cInfo.Message = "The test index is null.";
			}
		}
		catch (Exception ex)
		{
			cInfo.Message = "Failed to get loop names " + Environment.NewLine + ex.ToString();
		}
		return cInfo;
	}

	private CallbackResultInfo EnableLoopRows(TTDPluginEventArgs e, CallbackResultInfo cInfo, bool enableBasedOnValues)
	{
		try
		{
			if (e.Message != null && e.Message is LoopRowInfo)
			{
				cInfo.Result = EnableLoopRow((LoopRowInfo)e.Message, enableBasedOnValues);
			}
			else
			{
				cInfo.Result = false;
				cInfo.Message = "Please provide LoopRowInfo object to the message.";
			}
		}
		catch (Exception ex)
		{
			cInfo.Message = "Failed to enable Loop Row " + Environment.NewLine + ex.ToString();
		}
		return cInfo;
	}

	private CallbackResultInfo EnableDataLogging(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		try
		{
			if (e.Message != null && e.Message is DataLoggingInfo)
			{
				cInfo.Result = EnableParamDataLogging((DataLoggingInfo)e.Message);
			}
			else
			{
				cInfo.Result = false;
				cInfo.Message = "Please provide DataLoggingInfo object to the message.";
			}
		}
		catch (Exception ex)
		{
			cInfo.Message = "Failed to enable DataLogging " + Environment.NewLine + ex.ToString();
		}
		return cInfo;
	}

	private CallbackResultInfo CreateFolder(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		try
		{
			if (e.Message != null && e.Message is CreateFolderInfo)
			{
				CreateFolderInfo createFolderInfo = (CreateFolderInfo)e.Message;
				BaseTest baseTest = null;
				if (createFolderInfo != null && !string.IsNullOrEmpty(createFolderInfo.NewFolderName))
				{
					if (!string.IsNullOrEmpty(createFolderInfo.DestinationName))
					{
						if (createFolderInfo.IsTestIndex)
						{
							baseTest = Root.FindTest(createFolderInfo.DestinationName);
							if (baseTest != null)
							{
								if (baseTest is FolderTest)
								{
									baseTest.Properties.Expanded = true;
								}
								else
								{
									createFolderInfo.InsertPosition = Position.NextSibling;
								}
							}
						}
						else
						{
							string regexMatchString = GetRegexMatchString(createFolderInfo.DestinationName);
							baseTest = Root;
							BaseTest matchingNode = null;
							if (SearchNodes(baseTest, regexMatchString, WorkspaceConfig.SearchOptions.TestName, childrenOnly: false, out matchingNode))
							{
								baseTest = matchingNode;
								if (baseTest != null && baseTest is FolderTest)
								{
									baseTest.Properties.Expanded = true;
								}
							}
							if (matchingNode == null)
							{
								cInfo.Result = false;
								cInfo.Message = "Destination name does not exist in the tree.";
								return cInfo;
							}
						}
					}
					else
					{
						baseTest = Root;
						Root.Properties.Expanded = true;
					}
					FolderTest newFolder = null;
					if (AddFolder(createFolderInfo.InsertPosition, out newFolder, baseTest) && newFolder != null)
					{
						newFolder.Properties.TestName = createFolderInfo.NewFolderName;
						EndTestNameEdit(newFolder, commit: true);
						cInfo.Message = newFolder.Index;
						cInfo.Result = true;
						return cInfo;
					}
				}
			}
			cInfo.Result = false;
			cInfo.Message = "FolderName or folderInfo object is not valid.";
		}
		catch (Exception ex)
		{
			cInfo.Result = false;
			cInfo.Message = ex.ToString();
		}
		return cInfo;
	}

	public bool AddNewNode(BaseTest ParentNode, object NewNode, Position insertPosition)
	{
		TraceMessage.Write(ToString(), "AddNewNode", TraceMessageType.Trace);
		try
		{
			bool flag = false;
			bool flag2 = false;
			int num = 0;
			if (NewNode is BaseTest)
			{
				_ = (BaseTest)NewNode;
			}
			else if (NewNode is QTMTest)
			{
				GetNewTestNode((QTMTest)NewNode);
			}
			if (ParentNode == Root)
			{
				if (Root.Properties.Expanded)
				{
					if (insertPosition == Position.Last)
					{
						num = Root.TestList.Count;
						flag = true;
					}
					else
					{
						num = 0;
						flag = true;
					}
				}
				else
				{
					num = Root.TestList.Count;
					flag = true;
				}
			}
			else if (ParentNode is FolderTest)
			{
				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 == 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, (BaseTest)NewNode);
			}
			else if (flag2)
			{
				ParentNode.Parent.TestList.Insert(num, (BaseTest)NewNode);
			}
			else
			{
				((FolderTest)ParentNode).TestList.Add((BaseTest)NewNode);
			}
			GlobalStatus.RegistrationStatus.TreeChanged = true;
			return true;
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "AddNewNode " + ex.Message, TraceMessageType.Exception);
		}
		return false;
	}

	private BaseTest GetNewTestNode(QTMTest NewNode)
	{
		BaseTest baseTest = null;
		QTMTestContainer tContainer = new QTMTestContainer();
		treeView._scheduler.Registry.GetContainerForTest(NewNode.Name, NewNode.TargetName, out tContainer);
		switch (tContainer.ContainerType)
		{
		case QC.QsprTestObjects.Tests.Base.QTMTestTypes.HiveTest:
		{
			string[] array = tContainer.Name.Split('\\');
			HiveTest hiveTest = new HiveTest(NewNode.Name, tContainer.TargetName, Root);
			hiveTest.HiveProperties.DLLName = array[array.Length - 1];
			baseTest = hiveTest;
			break;
		}
		case QC.QsprTestObjects.Tests.Base.QTMTestTypes.ScriptTest:
		{
			ScriptTest scriptTest = new ScriptTest(NewNode.Name, tContainer.TargetName, Root);
			baseTest = scriptTest;
			break;
		}
		case QC.QsprTestObjects.Tests.Base.QTMTestTypes.LabVIEWTest:
		{
			LabVIEWTest labVIEWTest = new LabVIEWTest(NewNode.Name, tContainer.TargetName, Root);
			baseTest = labVIEWTest;
			break;
		}
		case QC.QsprTestObjects.Tests.Base.QTMTestTypes.DotNetTest:
		{
			DotNetTest dotNetTest = new DotNetTest(NewNode.Name, tContainer.TargetName, Root);
			baseTest = dotNetTest;
			FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(tContainer.Name);
			dotNetTest.DotNetProperties.MinDllVersion = versionInfo.FileVersion;
			dotNetTest.DotNetProperties.DllName = Path.GetFileName(versionInfo.FileName);
			break;
		}
		case QC.QsprTestObjects.Tests.Base.QTMTestTypes.DotNetTestEx:
		{
			DotNetTestEx dotNetTestEx = new DotNetTestEx(NewNode.Name, tContainer.TargetName, Root);
			baseTest = dotNetTestEx;
			break;
		}
		}
		if (baseTest != null)
		{
			baseTest.Properties.CheckStateTest = CheckState.Checked;
		}
		return baseTest;
	}

	private bool DeleteTest(string testName)
	{
		try
		{
			if (!string.IsNullOrEmpty(testName))
			{
				bool result = false;
				testName = GetRegexMatchString(testName);
				BaseTest matchingNode = Root;
				while (SearchNodes(matchingNode, testName, WorkspaceConfig.SearchOptions.TestName, childrenOnly: false, out matchingNode))
				{
					result = true;
					if (matchingNode != null)
					{
						BaseTest parent = matchingNode.Parent;
						DeleteNode(matchingNode);
						matchingNode = parent;
					}
				}
				return result;
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "DeleteTest failed, " + ex.Message, TraceMessageType.Exception);
		}
		return false;
	}

	private CallbackResultInfo RenameTest(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		if (e.Message != null && e.Message is TestInfo)
		{
			TestInfo testInfo = (TestInfo)e.Message;
			cInfo.Result = RenameNode(testInfo);
		}
		return cInfo;
	}

	private bool SetRootName(string newName)
	{
		TraceMessage.Write(ToString(), "SetRootName, newName=" + newName, TraceMessageType.Trace);
		try
		{
			if (!string.IsNullOrEmpty(newName) && Root != null && Root.TestList.Count > 0)
			{
				Root.Properties.TestName = newName;
				return true;
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "Set RootName failed, " + ex.Message, TraceMessageType.Exception);
		}
		return false;
	}

	private CallbackResultInfo SetTestParameterInFolder(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		if (e.Message != null && e.Message is ParamInfo)
		{
			ParamInfo paramInfo = (ParamInfo)e.Message;
			try
			{
				if (!string.IsNullOrEmpty(paramInfo.ParameterName) && !string.IsNullOrEmpty(paramInfo.FolderName) && Root.TestList.Count > 0)
				{
					List<BaseTest> list = new List<BaseTest>();
					paramInfo.FolderName = GetRegexMatchString(paramInfo.FolderName);
					BaseTest baseTest = Root;
					BaseTest matchingNode = null;
					while (SearchNodes(baseTest, paramInfo.FolderName, WorkspaceConfig.SearchOptions.TestName, childrenOnly: false, out matchingNode))
					{
						baseTest = matchingNode;
						if (baseTest is FolderTest && ((FolderTest)baseTest).TestList.Count > 0)
						{
							list.Add(baseTest);
						}
					}
					if (list.Count > 0)
					{
						foreach (FolderTest item in list)
						{
							foreach (BaseTest test in item.TestList)
							{
								if (test.Properties.Parameters.HasParameter(paramInfo.ParameterName, exactMatch: true, out var _))
								{
									cInfo.Message = UpdateParamValue(paramInfo, test);
								}
							}
						}
					}
					if (string.IsNullOrEmpty(cInfo.Message.ToString()))
					{
						cInfo.Result = true;
					}
					else
					{
						cInfo.Result = false;
					}
					return cInfo;
				}
			}
			catch (Exception ex)
			{
				TraceMessage.Write(ToString(), "SetTestParameterInAllTests failed, " + ex.Message, TraceMessageType.Exception);
			}
			cInfo.Result = false;
		}
		return cInfo;
	}

	private CallbackResultInfo SetTestParamGV(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		if (e.Message != null && e.Message is ParamInfo)
		{
			ParamInfo paramInfo = (ParamInfo)e.Message;
			try
			{
				string testName = paramInfo.TestName;
				string parameterName = paramInfo.ParameterName;
				string parameterValue = paramInfo.ParameterValue;
				BaseTest baseTest = Root.FindTest(testName);
				string matchedParamName = "";
				if (baseTest.Properties.Parameters.HasParameter(parameterName, exactMatch: true, out matchedParamName))
				{
					switch (e.Key)
					{
					case "Set Test Param GV Get":
						if (!baseTest.Properties.Parameters.SetParamGlobalVariable(parameterName, parameterValue))
						{
							cInfo.Message = "Set GV Get failed";
						}
						break;
					case "Set Test Param GV Set":
						if (!baseTest.Properties.Parameters.SetParamGlobalVariableOther(parameterName, parameterValue))
						{
							cInfo.Message = "Set GV Set failed";
						}
						break;
					case "Set Test Param GV Get LL":
						if (!baseTest.Properties.Parameters.SetParamLLGlobalVariable(parameterName, parameterValue))
						{
							cInfo.Message = "Set GV Get LL failed";
						}
						break;
					case "Set Test Param GV Get UL":
						if (!baseTest.Properties.Parameters.SetParamULGlobalVariable(parameterName, parameterValue))
						{
							cInfo.Message = "Set GV Get UL failed";
						}
						break;
					}
					if (cInfo.Message == null || string.IsNullOrEmpty(cInfo.Message.ToString()))
					{
						cInfo.Result = true;
					}
					else
					{
						cInfo.Result = false;
					}
					return cInfo;
				}
			}
			catch (Exception ex)
			{
				TraceMessage.Write(ToString(), "SetTestParamLimits failed, " + ex.Message, TraceMessageType.Exception);
			}
			cInfo.Result = false;
		}
		return cInfo;
	}

	private CallbackResultInfo SetTestParameter(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		if (e.Message != null && e.Message is ParamInfo)
		{
			ParamInfo paramInfo = (ParamInfo)e.Message;
			cInfo = default(CallbackResultInfo);
			try
			{
				if (!string.IsNullOrEmpty(paramInfo.ParameterName) && !string.IsNullOrEmpty(paramInfo.TestName) && Root.TestList.Count > 0)
				{
					if (paramInfo.IsTestIndex)
					{
						BaseTest baseTest = FindTest(paramInfo.TestName, FindTestValueType.TestIndex, isPartialName: false);
						if (baseTest != null)
						{
							if (baseTest.Properties.Parameters.HasParameter(paramInfo.ParameterName, exactMatch: true, out var _))
							{
								cInfo.Message = UpdateParamValue(paramInfo, baseTest);
								cInfo.Message = string.Concat(cInfo.Message, UpdateParamLimits(paramInfo, baseTest));
							}
							if (string.IsNullOrEmpty(cInfo.Message.ToString()))
							{
								cInfo.Result = true;
							}
							else
							{
								cInfo.Result = false;
							}
							return cInfo;
						}
						cInfo.Message = "SetTestParameter failed, invalid test index specified";
						cInfo.Result = false;
						return cInfo;
					}
					paramInfo.TestName = GetRegexMatchString(paramInfo.TestName);
					BaseTest baseTest2 = Root;
					BaseTest matchingNode = null;
					while (SearchNodes(baseTest2, paramInfo.TestName, WorkspaceConfig.SearchOptions.TestName, childrenOnly: false, out matchingNode))
					{
						baseTest2 = matchingNode;
						if (baseTest2 != null && baseTest2.Properties.Parameters.HasParameter(paramInfo.ParameterName, exactMatch: true, out var _))
						{
							cInfo.Message = UpdateParamValue(paramInfo, baseTest2);
						}
					}
					if (string.IsNullOrEmpty(cInfo.Message.ToString()))
					{
						cInfo.Result = true;
					}
					else
					{
						cInfo.Result = false;
					}
					return cInfo;
				}
			}
			catch (Exception ex)
			{
				TraceMessage.Write(ToString(), "SetTestParameter failed, " + ex.Message, TraceMessageType.Exception);
			}
			cInfo.Result = false;
		}
		return cInfo;
	}

	private string UpdateParamValue(ParamInfo paramInfo, BaseTest temp)
	{
		try
		{
			temp.Properties.Parameters.SetParamValue(paramInfo.ParameterName, paramInfo.ParameterValue, paramInfo.Index, paramInfo.Array2DIndex);
		}
		catch (Exception ex)
		{
			return $"Failed to update parameter {paramInfo.ParameterName}; Error {ex.Message}: ";
		}
		return "";
	}

	private string UpdateParamLimits(ParamInfo paramInfo, BaseTest temp)
	{
		try
		{
			if (!string.IsNullOrEmpty(paramInfo.LowerLimit))
			{
				temp.Properties.Parameters.SetParamLowerLimit(paramInfo.ParameterName, paramInfo.LowerLimit, paramInfo.Index);
			}
			if (!string.IsNullOrEmpty(paramInfo.UpperLimit))
			{
				temp.Properties.Parameters.SetParamUpperLimit(paramInfo.ParameterName, paramInfo.UpperLimit, paramInfo.Index);
			}
		}
		catch (Exception ex)
		{
			return $"Failed to update parameter {paramInfo.ParameterName}; Error {ex.Message}: ";
		}
		return "";
	}

	private void ClearSelectedNodes()
	{
		List<BaseTest> selectedTests = GetSelectedTests();
		foreach (BaseTest item in selectedTests)
		{
			item.Properties.IsSelected = false;
		}
	}

	private BaseTest GetNextSibling(BaseTest test)
	{
		BaseTest result = null;
		if (test.Parent != null)
		{
			int num = test.Parent.TestList.IndexOf(test) + 1;
			if (test.Parent.TestList.Count > num)
			{
				result = test.Parent.TestList[num];
			}
		}
		return result;
	}

	private bool SearchNodes(BaseTest currentNode, string searchString, WorkspaceConfig.SearchOptions searchtype, bool childrenOnly, out BaseTest matchingNode)
	{
		matchingNode = null;
		bool result = false;
		try
		{
			while (currentNode != null)
			{
				if (currentNode is FolderTest)
				{
					FolderTest folderTest = currentNode as FolderTest;
					if (folderTest.TestList.Count > 0)
					{
						if (SearchANode(folderTest.TestList[0], searchString, searchtype))
						{
							matchingNode = folderTest.TestList[0];
							result = true;
							break;
						}
						if (SearchNodes(folderTest.TestList[0], searchString, searchtype, childrenOnly: true, out matchingNode))
						{
							result = true;
							break;
						}
					}
				}
				BaseTest baseTest = GetNextSibling(currentNode);
				while (baseTest == null && !childrenOnly)
				{
					FolderTest parent;
					for (parent = currentNode.Parent; parent != null; parent = parent.Parent)
					{
						baseTest = GetNextSibling(parent);
						if (baseTest != null)
						{
							break;
						}
					}
					if (parent == null)
					{
						baseTest = parent;
						currentNode = parent;
						break;
					}
				}
				currentNode = baseTest;
				if (currentNode != null && SearchANode(currentNode, searchString, searchtype))
				{
					matchingNode = currentNode;
					result = true;
					break;
				}
			}
		}
		catch (FormatException ex)
		{
			Console.WriteLine(ex.Message);
		}
		return result;
	}

	private bool SearchANode(BaseTest currentNode, string searchString, WorkspaceConfig.SearchOptions searchtype)
	{
		bool flag = false;
		string matchedParamName = "";
		string text = "";
		string searchString2 = searchString;
		switch (searchtype)
		{
		case WorkspaceConfig.SearchOptions.TestName:
			text = currentNode.Properties.TestName;
			MatchCase(ref text, ref searchString2);
			if (text.Contains(searchString2))
			{
				flag = true;
			}
			else if (GlobalStatus.AutomationMode && GlobalStatus.RegexSupportEnabled)
			{
				Regex regex = new Regex(searchString);
				text = TranslateTestPath(currentNode);
				if (regex.IsMatch(text))
				{
					flag = true;
				}
			}
			break;
		case WorkspaceConfig.SearchOptions.RealName:
			text = currentNode.Properties.RealName;
			MatchCase(ref text, ref searchString);
			if (text.Contains(searchString))
			{
				flag = true;
			}
			break;
		case WorkspaceConfig.SearchOptions.ParameterName:
			if (currentNode.Properties.Parameters.HasParameter(searchString, exactMatch: false, out matchedParamName))
			{
				flag = true;
			}
			break;
		case WorkspaceConfig.SearchOptions.RetryCount:
			if (currentNode.Properties.RetryCount > 0)
			{
				flag = true;
			}
			break;
		case WorkspaceConfig.SearchOptions.StopOnError:
			if (currentNode.Properties.StopOnError != 0)
			{
				flag = true;
			}
			break;
		case WorkspaceConfig.SearchOptions.Loop:
			if (currentNode.Properties.LoopSetup != string.Empty && currentNode.Properties.LoopSetup != "False")
			{
				flag = true;
			}
			break;
		case WorkspaceConfig.SearchOptions.BreakPoint:
			if (currentNode.Properties.BreakPoint == BreakPointTypes.Enable)
			{
				flag = true;
			}
			break;
		case WorkspaceConfig.SearchOptions.BranchOnError:
			if (currentNode.Properties.BranchOnError != string.Empty)
			{
				flag = true;
			}
			break;
		case WorkspaceConfig.SearchOptions.PreCondition:
			if (currentNode.Properties.PreCondition != null)
			{
				flag = true;
			}
			break;
		case WorkspaceConfig.SearchOptions.PostCondition:
			if (currentNode.Properties.PostCondition != null)
			{
				flag = true;
			}
			break;
		case WorkspaceConfig.SearchOptions.BookKeepingTest:
			if (currentNode.Properties.BookKeeping)
			{
				flag = true;
			}
			break;
		case WorkspaceConfig.SearchOptions.TestErrorCode:
			text = currentNode.Properties.TestErrorCode;
			if (text != null)
			{
				MatchCase(ref text, ref searchString);
				if (text.Contains(searchString))
				{
					flag = true;
				}
			}
			break;
		case WorkspaceConfig.SearchOptions.Comments:
			text = currentNode.Properties.Comment;
			if (text == null)
			{
				break;
			}
			if (string.IsNullOrEmpty(searchString))
			{
				flag = true;
				break;
			}
			MatchCase(ref text, ref searchString);
			if (text.Contains(searchString))
			{
				flag = true;
			}
			break;
		}
		if (flag)
		{
			return true;
		}
		return false;
	}

	private string TranslateTestPath(BaseTest testNode, bool excludeRootFromPath = true)
	{
		string text = testNode.Properties.TestName;
		for (FolderTest folderTest = testNode.Parent; folderTest != null; folderTest = ((folderTest == Root) ? null : folderTest.Parent))
		{
			text = folderTest.Properties.TestName + "\\" + text;
		}
		if (excludeRootFromPath && !text.StartsWith("\\"))
		{
			text = "\\" + text;
		}
		return text;
	}

	private void MatchCase(ref string propertyString, ref string searchString)
	{
		if (!QTMData.WConfig.SearchCaseMatch)
		{
			propertyString = propertyString.ToUpper();
			searchString = searchString.ToUpper();
		}
	}

	private string GetRegexMatchString(string name)
	{
		string text = name;
		if (GlobalStatus.RegexSupportEnabled)
		{
			if (text.StartsWith("^") && text.EndsWith("$"))
			{
				text = text.Remove(0, 1);
				return "\\\\" + text;
			}
			if (text.StartsWith("^"))
			{
				text = text.Remove(0, 1) + "(.*)";
			}
			text = ((!text.EndsWith("$")) ? (text + "$") : ("(.*)" + text));
			if (!text.StartsWith("\\\\"))
			{
				text = "\\\\" + text;
			}
		}
		return text;
	}

	private CallbackResultInfo SetSkipRun(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		if (e.Message != null && e.Message is SkipRunSequenceInfo)
		{
			SkipRunSequenceInfo skipRunSequenceInfo = (SkipRunSequenceInfo)e.Message;
			try
			{
				if (!string.IsNullOrEmpty(skipRunSequenceInfo.TestName) && Root.TestList.Count > 0)
				{
					bool result = false;
					skipRunSequenceInfo.TestName = GetRegexMatchString(skipRunSequenceInfo.TestName);
					BaseTest baseTest = Root;
					BaseTest matchingNode = null;
					while (SearchNodes(baseTest, skipRunSequenceInfo.TestName, WorkspaceConfig.SearchOptions.TestName, childrenOnly: false, out matchingNode))
					{
						baseTest = matchingNode;
						if (baseTest != null)
						{
							result = true;
							switch (skipRunSequenceInfo.SkipRunNewValue)
							{
							case SkipRunSequence.Always:
								baseTest.Properties.CheckStateTest = CheckState.Unchecked;
								break;
							case SkipRunSequence.Never:
								baseTest.Properties.CheckStateTest = CheckState.Checked;
								break;
							case SkipRunSequence.LinkedToAnotherDocument:
								baseTest.Properties.SkipWhenLinked = true;
								break;
							}
						}
					}
					cInfo.Result = result;
				}
			}
			catch (Exception ex)
			{
				TraceMessage.Write(ToString(), "SetSkipRun failed, " + ex.Message, TraceMessageType.Exception);
			}
		}
		return cInfo;
	}

	private CallbackResultInfo SetTestParamInAllTests(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		if (e.Message != null && e.Message is ParamInfo)
		{
			ParamInfo paramInfo = (ParamInfo)e.Message;
			cInfo = default(CallbackResultInfo);
			try
			{
				if (!string.IsNullOrEmpty(paramInfo.ParameterName) && Root.TestList.Count > 0)
				{
					List<BaseTest> list = Root.FindAllTestWithParameter(paramInfo.ParameterName);
					if (list.Count > 0)
					{
						cInfo.Result = true;
					}
					foreach (BaseTest item in list)
					{
						bool flag = item.Properties.Parameters.SetParamValue(paramInfo.ParameterName, paramInfo.ParameterValue, paramInfo.Index, paramInfo.Array2DIndex);
						cInfo.Result &= flag;
					}
				}
			}
			catch (Exception ex)
			{
				TraceMessage.Write(ToString(), "SetTestParameterInAllTests failed, " + ex.Message, TraceMessageType.Exception);
				cInfo.Result = false;
			}
		}
		return cInfo;
	}

	private CallbackResultInfo SetUserInput(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		if (e.Message != null && e.Message is UserInputInfo && ((UserInputInfo)e.Message).Caption != null && ((UserInputInfo)e.Message).Label != null && ((UserInputInfo)e.Message).UserInput != null)
		{
			string gvName = ((UserInputInfo)e.Message).Caption + ":" + ((UserInputInfo)e.Message).Label;
			_ = (UserInputInfo)e.Message;
			QTMGlobalVariable.SetGlobalVariable(gvName, ((UserInputInfo)e.Message).UserInput);
			cInfo.Result = true;
		}
		else
		{
			cInfo.Message = "Invalid userInput";
			cInfo.Result = false;
		}
		return cInfo;
	}

	private CallbackResultInfo SetSerialNumber(TTDPluginEventArgs e, CallbackResultInfo cInfo)
	{
		if (e.Message != null && e.Message is SetSNInfo && ((SetSNInfo)e.Message).SerialNumber != null)
		{
			SetSNInfo setSNInfo = (SetSNInfo)e.Message;
			QTMData.WConfig.LastUsedSerialNumber = setSNInfo.SerialNumber;
			QTMGlobalVariable.SetGlobalVariable("SN", setSNInfo.SerialNumber);
			string gvName = ((setSNInfo.PhoneIndex != 0) ? $"Multiple UUT ID:UUT ID {setSNInfo.PhoneIndex}:" : "Single UUT ID:UUT ID :");
			QTMGlobalVariable.SetGlobalVariable(gvName, setSNInfo.SerialNumber);
			cInfo.Result = true;
		}
		else
		{
			cInfo.Message = "Invalid userInput";
			cInfo.Result = false;
		}
		return cInfo;
	}

	private bool SelectTestNode(SelectNodeInfo sInfo)
	{
		FindTestValueType valueType = (sInfo.IsRealName ? FindTestValueType.TestName : FindTestValueType.RealName);
		BaseTest baseTest = FindTest(sInfo.TestName, valueType, sInfo.IsPartialName);
		if (baseTest != null)
		{
			baseTest.Properties.IsSelected = true;
			return true;
		}
		return false;
	}

	private bool SelectTests(string values, bool byName)
	{
		string[] array = values.Split(',');
		Root.Properties.CheckStateTest = CheckState.Checked;
		Root.Properties.CheckStateTest = CheckState.Unchecked;
		if (array.Length <= 0)
		{
			return false;
		}
		string[] array2 = array;
		foreach (string value in array2)
		{
			FindTestValueType valueType = (byName ? FindTestValueType.TestName : FindTestValueType.TestIndex);
			BaseTest baseTest = FindTest(value, valueType, isPartialName: false);
			if (baseTest != null)
			{
				baseTest.Properties.CheckStateTest = CheckState.Checked;
			}
		}
		BaseTest baseTest2 = FindTest("TestPlanStartup", FindTestValueType.TestName, isPartialName: false);
		if (baseTest2 != null)
		{
			baseTest2.Properties.CheckStateTest = CheckState.Checked;
			if (baseTest2 is DotNetTest && baseTest2.Parent != null && !(baseTest2.Parent is RootTest))
			{
				baseTest2.Parent.Properties.CheckStateTest = CheckState.Checked;
			}
		}
		BaseTest baseTest3 = FindTest("TestPlanShutdown", FindTestValueType.TestName, isPartialName: false);
		if (baseTest3 != null)
		{
			baseTest3.Properties.CheckStateTest = CheckState.Checked;
			baseTest3.Properties.TreeMustRun = true;
			if (baseTest3 is DotNetTest && baseTest3.Parent != null && !(baseTest3.Parent is RootTest))
			{
				baseTest3.Parent.Properties.TreeMustRun = true;
				baseTest3.Parent.Properties.CheckStateTest = CheckState.Checked;
			}
		}
		return true;
	}

	private BaseTest FindTest(string value, FindTestValueType valueType, bool isPartialName)
	{
		BaseTest result = null;
		switch (valueType)
		{
		case FindTestValueType.TestIndex:
			result = Root.FindTest(value);
			break;
		case FindTestValueType.TestName:
			result = Root.FindTestFromTestName(value, isPartialName);
			break;
		case FindTestValueType.RealName:
			result = Root.FindTestFromRealName(value, isPartialName);
			break;
		case FindTestValueType.TestPath:
			result = Root.FindTestFromFullPath(value, isPartialName);
			break;
		}
		return result;
	}

	private List<BaseTest> GetSelectedTests()
	{
		return Root.SelectedTests;
	}
}
