using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using HIVECOMKERNELLib;
using QC.QsprShared.Dialogs;
using QC.QsprShared.GlobalData;
using QC.QsprShared.Trace;
using QC.QsprShared.Utilities;
using QC.QsprTestObjects.Tests.Base;
using QC.QTMDotNetKernel;
using QC.QTMDotNetKernelInterface;
using QC.QTMInterface;
using QC.QTMLabViewKernel;
using QC.QTMScriptKernel;
using QsprMiscShared.Config;

namespace QC.QsprTestObjects;

public class TestKernelManager
{
	public enum TPLMessages
	{
		HIVE_ON_FILTER_WINDOW_SET_FILTER = 34817,
		HIVE_ON_FILTER_WINDOW_PRINT_MSG,
		HIVE_ON_FILTER_WINDOW_PRINT_MSG_TIMESTAMP,
		HIVE_ON_FILTER_WINDOW_RESET_TIMER,
		HIVE_ON_FILTER_WINDOW_CLEAR,
		HIVE_ON_FILTER_WINDOW_SAVE_TPL,
		HIVE_ON_FILTER_WINDOW_STOP,
		HIVE_ON_FILTER_WINDOW_RESUME,
		HIVE_ON_FILTER_WINDOW_ENABLE_FILTER,
		HIVE_ON_FILTER_WINDOW_ENABLE_ALL_FILTERS,
		HIVE_ON_FILTER_WINDOW_DISABLE_ALL_FILTERS,
		HIVE_ON_FILTER_WINDOW_BK_COLOR,
		HIVE_ON_FILTER_WINDOW_ENABLE_GRIDLINE,
		HIVE_ON_FILTER_WINDOW_REFRESH,
		HIVE_ON_TPL2_START_TEST,
		HIVE_ON_TPL2_STOP_TEST,
		HIVE_ON_TPL2_START_CONTEXT,
		HIVE_ON_TPL2_STOP_CONTEXT,
		HIVE_ON_TPL2_SAVE,
		HIVE_ON_TTD_PROCESS_UPDATE_TO_XAPP,
		HIVE_ON_TTD_GONOGO_DONE_TO_XAPP,
		HIVE_ON_TTD_STOP_TO_XAPP,
		HIVE_FROM_TPL2_TREE_TO_LIST,
		HIVE_FROM_TPL2_LIST_TO_TREE,
		HIVE_ON_FACADE_QUERY_TREE_NODE_NOTE,
		HIVE_ON_TPL2_CLEAR
	}

	public enum HTMLMessages
	{
		HIVE_ON_HTML_NAVIGATE = 34804,
		HIVE_ON_HTML_REFRESH
	}

	public struct paramDetails
	{
		public int dataSetID;

		public string paramName;

		public string paramValue;

		public string units;

		public string minLimit;

		public string maxLimit;
	}

	public const string LOOP_CONDITION_SEPARATOR = ",";

	public const string LOOP_SEPARATOR = "_;_";

	private const string LOOPVALUE_SEP = "__";

	private const string LOOPSCENARIO_SEP = "_ _";

	private const string PARAM_LOOP_SEP = " _ ";

	private const string SEQ_WIN_NAME = "Sequence Window";

	internal ScriptKernel scriptKernel;

	internal DotNetKernel dotNetKernel;

	internal LabViewKernel labVIEWKernel;

	internal HKernelClass HiveKernel;

	internal Scheduler scheduler;

	private List<string> gvChangedList = new List<string>();

	internal bool HiveTestRunning;

	private bool FirstFilterMsg = true;

	private _IHKernelEvents_OnTextOutEventHandler eTextOutHandler;

	private _IHKernelEvents_TestDoneEventHandler eTestDoneHandler;

	private _IHKernelEvents_OnPrintMsgEventHandler ePrintMsgHandler;

	private _IHKernelEvents_OnTPLWindowMsgEventHandler eTPLWindowMsgHandler;

	private _IHKernelEvents_OnHTMLWindowMsgEventHandler eHTMLWindowMsgHandler;

	private _IHKernelEvents_SetSUITETestParamDetailsEventHandler eSUITETestParamDetailsHandler;

	private _IHKernelEvents_OnSetGlobalVariableEventHandler eSetGlobalVarHandler;

	private List<paramDetails> currOutputParamDetails;

	private int currDataSetID;

	private List<int> currRefIDs = new List<int>();

	private string currTest;

	private string currInternalTest = "";

	private bool sendAllLoopConditions;

	private List<string> userSelectedLoopNamesCollection = new List<string>();

	private string currentDataSetLoopCond;

	private string currentDataSetLoopCondForParam;

	private Dictionary<int, string> allRefIDConditions = new Dictionary<int, string>();

	public bool IsSafeToExit
	{
		get
		{
			try
			{
				if (QTMData.Kernels.HiveKernel)
				{
					int isSafeToExitFlag = 1;
					HiveKernel.IsSafeToExit(ref isSafeToExitFlag);
					return (isSafeToExitFlag != 0) ? true : false;
				}
			}
			catch (Exception ex)
			{
				TraceMessage.Write("QC.QTMApplication.HiveTest", "IsInAutomation " + ex.Message, TraceMessageType.Exception);
			}
			return true;
		}
	}

	public void InitializeScriptKernel(TestMessageBroadCaster msgBroadCaster)
	{
		scriptKernel = new ScriptKernel();
		scriptKernel.OnScriptDone += msgBroadCaster.OnTestDone;
		scriptKernel.OnScriptTextOut += msgBroadCaster.OnTextOut;
	}

	public void InitializeDotNetKernel(TestMessageBroadCaster msgBroadCaster)
	{
		dotNetKernel = new DotNetKernel(scheduler.Env);
		dotNetKernel.OnTestDone += msgBroadCaster.OnTestDone;
		dotNetKernel.OnTextOut += msgBroadCaster.OnTextOut;
		dotNetKernel.OnPrintDebug += msgBroadCaster.OnPrintMessages;
		dotNetKernel.OnSaveDebugMsg += msgBroadCaster.OnSaveDebugMsg;
		dotNetKernel.OnClearDebugMsg += msgBroadCaster.OnClearDebugMsg;
		dotNetKernel.OnSaveAllDebugMsg += msgBroadCaster.OnSaveAllDebugMsg;
		dotNetKernel.OnClearAllDebugMsg += msgBroadCaster.OnClearAllDebugMsg;
		dotNetKernel.OnTPL2WindowMsg += msgBroadCaster.OnTPL2Window;
		dotNetKernel.OnHTMLWindowMsg += msgBroadCaster.OnHTMLWindow;
		dotNetKernel.OnGenericMsg += msgBroadCaster.OnGenericMsg;
		dotNetKernel.OnGenericParamMsg += msgBroadCaster.OnGenericParamMsg;
		dotNetKernel.OnCustomMsg += msgBroadCaster.OnCustomMsg;
		dotNetKernel.OnSetDebugWindowInFocusMsg += msgBroadCaster.OnSetDebugWindowInFocusMsg;
		dotNetKernel.domainList.Add(AppDomain.CurrentDomain);
	}

	public void InitializeLabVIEWKernel(TestMessageBroadCaster msgBroadCaster)
	{
		labVIEWKernel = new LabViewKernel();
		labVIEWKernel.OnVIDone += msgBroadCaster.OnTestDone;
	}

	public void InitializeHiveKernel(TestMessageBroadCaster msgBroadCaster)
	{
		HiveKernel = new HKernelClass();
		eTextOutHandler = OnTextOut;
		eTestDoneHandler = OnTestDone;
		ePrintMsgHandler = OnPrintMsg;
		eTPLWindowMsgHandler = OnTPL2WindowMsg;
		eHTMLWindowMsgHandler = OnHTMLWindowMsg;
		eSUITETestParamDetailsHandler = OnSUITETestParamDetailsMsg;
		eSetGlobalVarHandler = OnSetGlobalVariable;
		HiveKernel.OnTextOut += eTextOutHandler;
		HiveKernel.TestDone += eTestDoneHandler;
		HiveKernel.OnPrintMsg += ePrintMsgHandler;
		HiveKernel.OnTPLWindowMsg += eTPLWindowMsgHandler;
		HiveKernel.OnHTMLWindowMsg += eHTMLWindowMsgHandler;
		HiveKernel.SetSUITETestParamDetails += eSUITETestParamDetailsHandler;
		HiveKernel.OnSetGlobalVariable += eSetGlobalVarHandler;
		UpdateSUITELoopConditions();
		QC.QTMDotNetKernelInterface.GlobalVariable.OnGlobalVariableChange += GlobalVariable_OnGlobalVariableChange;
	}

	public bool LoadScriptTest(string testDLL, ref string listOfTests)
	{
		scriptKernel.LoadTestDLL(testDLL, ref listOfTests);
		return true;
	}

	public bool LoadHiveTest(string testDLL, ref string ListOfTests, ref bool HideTests)
	{
		HideTests = false;
		try
		{
			string ListOfTestsToBeShown = "";
			HiveKernel.LoadTestDLLEx(testDLL, ref ListOfTests, ref ListOfTestsToBeShown);
			if (ListOfTestsToBeShown.Length == 0)
			{
				HideTests = true;
			}
			else
			{
				ListOfTests = ListOfTestsToBeShown;
			}
			return true;
		}
		catch (Exception ex)
		{
			AppMessageDialog.Show("Error Loading Hive Dll " + ex.Message, DialogResult.OK, MessageDialogType.Exception);
			return false;
		}
	}

	public bool LoadLabViewTest(string testDLL, ref string ListOfTests)
	{
		labVIEWKernel.LoadTestDLL(testDLL, ref ListOfTests);
		return true;
	}

	public bool LoadDotNetTest(string testDLL, ref string listOfTests, ref bool hideTests)
	{
		string oldTestList = listOfTests;
		if (!dotNetKernel.LoadTestDLL(testDLL, ref listOfTests, isExclusive: false, ref hideTests))
		{
			return false;
		}
		ParseTestList(oldTestList, ref listOfTests);
		return true;
	}

	public bool UnloadLabViewTest(string testDLL)
	{
		labVIEWKernel.UnloadTestDLL(testDLL);
		return true;
	}

	public bool UnloadScriptTest(string testDLL)
	{
		return true;
	}

	public bool UnloadDotNetTest(string testDLL)
	{
		if (dotNetKernel.UnLoadTestDLL(testDLL, isExclusive: false))
		{
			return true;
		}
		return false;
	}

	public bool UnloadHiveTest(string testDLL)
	{
		return true;
	}

	public void ShutdownLabVIEWKernel()
	{
		if (labVIEWKernel != null)
		{
			labVIEWKernel.CloseKernel();
		}
	}

	public void ShutdownDotNetKernel()
	{
		if (dotNetKernel != null)
		{
			dotNetKernel.Shutdown();
		}
	}

	public void ShutdownHiveKernel()
	{
	}

	public void ShutdownScriptKernel()
	{
	}

	protected void ParseTestList(string oldTestList, ref string listOfTests)
	{
		try
		{
			string[] array = listOfTests.Split('\ufffd');
			listOfTests = array[0];
			string text = array[1];
			Hashtable hashtable = new Hashtable();
			string[] array2 = text.Split(',');
			foreach (string text2 in array2)
			{
				if (text2.Trim().Length > 0 && !hashtable.Contains(text2))
				{
					hashtable.Add(text2, text2);
				}
			}
			string[] array3 = oldTestList.Split(',');
			foreach (string text3 in array3)
			{
				if (text3.Trim().Length > 0 && hashtable.Contains(text3))
				{
					listOfTests = listOfTests + text3 + ',';
				}
			}
		}
		catch
		{
		}
	}

	internal void StartNewSUITETest()
	{
		currOutputParamDetails = new List<paramDetails>();
		currDataSetID = 0;
		currTest = "";
		currInternalTest = "";
	}

	public void UpdateSUITELoopConditions()
	{
		try
		{
			if (scheduler.QTMData.WConfig != null)
			{
				sendAllLoopConditions = scheduler.QTMData.WConfig.SendAllInputConditions;
			}
			userSelectedLoopNamesCollection = new List<string>();
			if (scheduler.QTMData.WConfig == null || string.IsNullOrEmpty(scheduler.QTMData.WConfig.ListOfSUITELoop))
			{
				return;
			}
			string[] array = scheduler.QTMData.WConfig.ListOfSUITELoop.Split(new string[1] { "," }, StringSplitOptions.RemoveEmptyEntries);
			foreach (string text in array)
			{
				if (text.Contains("_;_"))
				{
					string[] array2 = text.Split(new string[1] { "_;_" }, StringSplitOptions.RemoveEmptyEntries);
					if (array2[1] == "1")
					{
						userSelectedLoopNamesCollection.Add(array2[0]);
					}
				}
				else
				{
					userSelectedLoopNamesCollection.Add(text);
				}
			}
		}
		catch (Exception ex)
		{
			AppMessageDialog.Show("Not able to load SUITE loop conditions. " + ex.Message, "QSPR - Message Center", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, DialogResult.OK, MessageDialogType.Information);
			TraceMessage.Write("QC.QTMApplication.HiveTest", "UpdateSUITELoopConditions " + ex.Message, TraceMessageType.Exception);
		}
	}

	private void GlobalVariable_OnGlobalVariableChange(object sender, GlobalVariableEventArgs e)
	{
		try
		{
			if (e.GVName != null && !HiveTestRunning && !gvChangedList.Contains(e.GVName.ToUpper()))
			{
				gvChangedList.Add(e.GVName.ToUpper());
			}
		}
		catch
		{
		}
	}

	public void OnTestDone(string strTestName, int iTestResult)
	{
		HiveTestRunning = false;
		scheduler.mutTest.WaitOne();
		TraceMessage.Write("QC.QTMApplication.HiveTest", "OnTestDone " + strTestName, TraceMessageType.Trace);
		new List<paramDetails>();
		object arrayParam = strTestName;
		try
		{
			HiveKernel.GetResultsParamInfo(ref arrayParam);
			if (currOutputParamDetails.Count > 0)
			{
				try
				{
					object[,] array = new object[15, currOutputParamDetails.Count];
					TraceMessage.Write("SUITEData - Count", currOutputParamDetails.Count.ToString(), TraceMessageType.Trace);
					int num = 0;
					foreach (paramDetails currOutputParamDetail in currOutputParamDetails)
					{
						array[0, num] = currOutputParamDetail.paramName;
						if (double.TryParse(currOutputParamDetail.paramValue, out var _))
						{
							array[1, num] = "Double";
						}
						else
						{
							array[1, num] = "String";
						}
						array[2, num] = 1;
						array[3, num] = currOutputParamDetail.paramValue;
						array[4, num] = "Output";
						array[5, num] = currOutputParamDetail.maxLimit;
						array[6, num] = currOutputParamDetail.minLimit;
						array[7, num] = currOutputParamDetail.units;
						array[8, num] = "";
						array[9, num] = "";
						array[10, num] = 0;
						array[11, num] = true;
						array[12, num] = true;
						array[13, num] = false;
						array[14, num] = "";
						TraceMessage.Write("SUITEData - setparam", currOutputParamDetail.paramName, TraceMessageType.Trace);
						num++;
					}
					arrayParam = array;
				}
				catch
				{
				}
			}
			if (currRefIDs != null && currRefIDs.Count > 0)
			{
				foreach (int currRefID in currRefIDs)
				{
					if (!allRefIDConditions.ContainsKey(currRefID))
					{
						continue;
					}
					if (scheduler.GlobalStatus.LoopDetailsComplete.Length > 0)
					{
						if (!scheduler.GlobalStatus.LoopDetailsComplete.Contains(allRefIDConditions[currRefID]))
						{
							scheduler.GlobalStatus.LoopDetailsComplete += "_ _";
							scheduler.GlobalStatus.LoopDetailsComplete += allRefIDConditions[currRefID];
							TraceMessage.Write("SUITEData - updateGlobalLoop", scheduler.GlobalStatus.LoopDetailsComplete, TraceMessageType.Trace);
						}
						else
						{
							TraceMessage.Write("SUITEData - updateGlobalLoop Not updated ", "", TraceMessageType.Trace);
						}
					}
					else
					{
						scheduler.GlobalStatus.LoopDetailsComplete += allRefIDConditions[currRefID];
						TraceMessage.Write("SUITEData - updateGlobalLoop", scheduler.GlobalStatus.LoopDetailsComplete, TraceMessageType.Trace);
					}
				}
				if (scheduler.GlobalStatus.LoopDetailsComplete.Length > 0 && scheduler.GlobalStatus.LoopDetailsComplete.Contains("_ _"))
				{
					string[] array2 = scheduler.GlobalStatus.LoopDetailsComplete.Split(new string[1] { "_ _" }, StringSplitOptions.RemoveEmptyEntries);
					scheduler.GlobalStatus.LoopDetailsComplete = "";
					string[] array3 = array2;
					foreach (string text in array3)
					{
						if (!scheduler.GlobalStatus.LoopDetailsComplete.Contains(text))
						{
							if (scheduler.GlobalStatus.LoopDetailsComplete.Length > 0)
							{
								scheduler.GlobalStatus.LoopDetailsComplete += "_ _";
							}
							scheduler.GlobalStatus.LoopDetailsComplete += text;
						}
					}
					TraceMessage.Write("SUITEData - after updateGlobalLoop", scheduler.GlobalStatus.LoopDetailsComplete, TraceMessageType.Trace);
				}
			}
		}
		catch
		{
		}
		finally
		{
			scheduler.mutTest.ReleaseMutex();
		}
		scheduler.TestMsgBroadCaster.OnTestDone(strTestName, iTestResult, arrayParam);
	}

	public void OnTextOut(string windowName, string windowText)
	{
		scheduler.TestMsgBroadCaster.OnTextOut(windowName, windowText, TraceLevel.Error, NoEndOfLine: true);
	}

	public void OnHTMLWindowMsg(int MsgID, string windowCaption, string textStr)
	{
		switch ((HTMLMessages)MsgID)
		{
		case HTMLMessages.HIVE_ON_HTML_NAVIGATE:
			scheduler.TestMsgBroadCaster.OnHTMLWindow(windowCaption, HTMLCommand.HTML_NAVIGATE, textStr);
			break;
		case HTMLMessages.HIVE_ON_HTML_REFRESH:
			scheduler.TestMsgBroadCaster.OnHTMLWindow(windowCaption, HTMLCommand.HTML_REFRESH, textStr);
			break;
		}
	}

	public void OnTPL2WindowMsg(int MsgID, string windowCaption, string param1, string param2, string param3, string param4)
	{
		Tpl2Info values = default(Tpl2Info);
		try
		{
			TraceMessage.Write("QC.QTMApplication.HiveTest", "OnTPL2WindowMsg-Debug " + ((TPLMessages)MsgID).ToString() + " " + param1, TraceMessageType.Trace);
			switch ((TPLMessages)MsgID)
			{
			case TPLMessages.HIVE_ON_FILTER_WINDOW_SET_FILTER:
				values.FilterValue = param1;
				scheduler.TestMsgBroadCaster.OnTPL2Window(windowCaption, Tpl2Command.SetFilter, values);
				break;
			case TPLMessages.HIVE_ON_FILTER_WINDOW_PRINT_MSG_TIMESTAMP:
				values.FilterValue = param1;
				values.Message = param2;
				values.SystemTime = QTMUtility.GetDateFromString(param3);
				scheduler.TestMsgBroadCaster.OnTPL2Window(windowCaption, Tpl2Command.PrintMessage, values);
				break;
			case TPLMessages.HIVE_ON_TPL2_START_TEST:
				values.TestName = param1;
				values.SystemTime = QTMUtility.GetDateFromString(param2);
				scheduler.TestMsgBroadCaster.OnTPL2Window(windowCaption, Tpl2Command.StartTest, values);
				break;
			case TPLMessages.HIVE_ON_TPL2_STOP_TEST:
				values.SystemTime = QTMUtility.GetDateFromString(param1);
				scheduler.TestMsgBroadCaster.OnTPL2Window(windowCaption, Tpl2Command.StopTest, values);
				break;
			case TPLMessages.HIVE_ON_TPL2_START_CONTEXT:
				values.ContextName = param1;
				values.ContextType = param2;
				values.SystemTime = QTMUtility.GetDateFromString(param3);
				scheduler.TestMsgBroadCaster.OnTPL2Window(windowCaption, Tpl2Command.StartContext, values);
				break;
			case TPLMessages.HIVE_ON_TPL2_STOP_CONTEXT:
				values.SystemTime = QTMUtility.GetDateFromString(param1);
				scheduler.TestMsgBroadCaster.OnTPL2Window(windowCaption, Tpl2Command.StopContext, values);
				break;
			case TPLMessages.HIVE_ON_TPL2_SAVE:
				values.SaveFileName = param1;
				scheduler.TestMsgBroadCaster.OnTPL2Window(windowCaption, Tpl2Command.SaveToTPL2, values);
				break;
			case TPLMessages.HIVE_ON_TPL2_CLEAR:
				scheduler.TestMsgBroadCaster.OnTPL2Window(windowCaption, Tpl2Command.Clear, values);
				break;
			case TPLMessages.HIVE_ON_FILTER_WINDOW_PRINT_MSG:
			case TPLMessages.HIVE_ON_FILTER_WINDOW_RESET_TIMER:
			case TPLMessages.HIVE_ON_FILTER_WINDOW_CLEAR:
			case TPLMessages.HIVE_ON_FILTER_WINDOW_SAVE_TPL:
			case TPLMessages.HIVE_ON_FILTER_WINDOW_STOP:
			case TPLMessages.HIVE_ON_FILTER_WINDOW_RESUME:
			case TPLMessages.HIVE_ON_FILTER_WINDOW_ENABLE_FILTER:
			case TPLMessages.HIVE_ON_FILTER_WINDOW_ENABLE_ALL_FILTERS:
			case TPLMessages.HIVE_ON_FILTER_WINDOW_DISABLE_ALL_FILTERS:
			case TPLMessages.HIVE_ON_FILTER_WINDOW_BK_COLOR:
			case TPLMessages.HIVE_ON_FILTER_WINDOW_ENABLE_GRIDLINE:
			case TPLMessages.HIVE_ON_FILTER_WINDOW_REFRESH:
			case TPLMessages.HIVE_ON_TTD_PROCESS_UPDATE_TO_XAPP:
			case TPLMessages.HIVE_ON_TTD_GONOGO_DONE_TO_XAPP:
			case TPLMessages.HIVE_ON_TTD_STOP_TO_XAPP:
			case TPLMessages.HIVE_FROM_TPL2_TREE_TO_LIST:
			case TPLMessages.HIVE_FROM_TPL2_LIST_TO_TREE:
			case TPLMessages.HIVE_ON_FACADE_QUERY_TREE_NODE_NOTE:
				break;
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write("QC.QTMApplication.HiveTest", "OnTPL2WindowMsg " + ex.Message, TraceMessageType.Exception);
		}
	}

	public void OnPrintMsg(string windowName, string strTime, string strFilter, string logText)
	{
		TraceMessage.Write("QC.QTMApplication.HiveTest", "OnPrintMsg-Debug " + strTime + " " + strFilter + " " + logText, TraceMessageType.Trace);
		if (FirstFilterMsg)
		{
			scheduler.TestMsgBroadCaster.OnPrintMessages("Sequence Window", PrintMsgOptions.FilterColInfo, "Time");
			scheduler.TestMsgBroadCaster.OnPrintMessages("Sequence Window", PrintMsgOptions.HeaderInfo, "Time", "Filter", "Text");
			FirstFilterMsg = false;
		}
		scheduler.TestMsgBroadCaster.OnPrintMessages("Sequence Window", PrintMsgOptions.RowInfo, strTime, strFilter, logText);
	}

	public void OnTestMessage(TestMsgType msgType)
	{
		try
		{
			switch (msgType)
			{
			case TestMsgType.OnUnitStart:
			case TestMsgType.OnStartOfDebugTesting:
				gvChangedList.Clear();
				SendGlobalVarsToHive();
				break;
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write("QC.QTMApplication.HiveTest", "OnMessageFromScripter " + ex.Message, TraceMessageType.Exception);
		}
	}

	public void OnSetGlobalVariable(string name, string value)
	{
		scheduler.Env.GlobalVariable.SetGlobalVariable(name, value);
	}

	internal void SendGlobalVarsToHive(bool sendAll = true)
	{
		List<string> list = new List<string>();
		List<string> list2 = new List<string>();
		if (sendAll)
		{
			list.AddRange(scheduler.Env.GlobalVariable.GVariables.Keys.Cast<string>());
		}
		else
		{
			list.AddRange(gvChangedList);
		}
		foreach (string item in list)
		{
			scheduler.Env.GlobalVariable.GetGlobalVariable(item, out var gvValue);
			if (gvValue == null)
			{
				gvValue = "";
			}
			list2.Add(gvValue);
		}
		gvChangedList.Clear();
		if (HiveKernel != null)
		{
			HKernelClass hiveKernel = HiveKernel;
			object gvNames = list.ToArray();
			object gvValues = list2.ToArray();
			hiveKernel.SendGlobalVariables(ref gvNames, ref gvValues, list.Count);
		}
	}

	public void SaveSuiteParamToGV(string name, string value, bool isOutputParam)
	{
		if (!scheduler.QTMData.WConfig.EnableSuiteGV)
		{
			return;
		}
		List<SuiteParamGV> list = new List<SuiteParamGV>();
		list = ((!isOutputParam) ? scheduler.QTMData.WConfig.SuiteParamGVs.Where((SuiteParamGV x) => x.Type == SuiteValueType.LoopCondition && x.ParameterName == name).ToList() : scheduler.QTMData.WConfig.SuiteParamGVs.Where((SuiteParamGV x) => x.Type == SuiteValueType.OutputParam && x.ParameterName == name).ToList());
		if (list.Count > 0)
		{
			SuiteParamGV suiteParamGV = list.First((SuiteParamGV x) => x.TestName == "ANY" || x.TestName == currTest);
			scheduler.Env.GlobalVariable.SetGlobalVariable(suiteParamGV.GlobalVariableName, value);
		}
	}

	public void OnSUITETestParamDetailsMsg(int dataInfo, int id, string testOrParamName, string value, string units, string minLimit, string maxLimit)
	{
		if (!scheduler.QTMData.WConfig.EnableSUITEOutParam)
		{
			return;
		}
		switch (dataInfo)
		{
		case 1:
			currTest = testOrParamName;
			if (value != null)
			{
				currInternalTest = value;
			}
			else
			{
				currInternalTest = "";
			}
			scheduler.TestMsgBroadCaster.genericParamMsgDelegate("StartTest", id.ToString(), currTest, value, "", "", "", 2);
			break;
		case 2:
			try
			{
				if (sendAllLoopConditions || userSelectedLoopNamesCollection.Contains(testOrParamName.Trim()))
				{
					if (!scheduler.GlobalStatus.LoopDetailsComplete.StartsWith(testOrParamName.Trim() + "__") && !scheduler.GlobalStatus.LoopDetailsComplete.Contains("_ _" + testOrParamName.Trim() + "__"))
					{
						if (currentDataSetLoopCond.Length > 1)
						{
							currentDataSetLoopCond += "_ _";
						}
						currentDataSetLoopCond = currentDataSetLoopCond + testOrParamName.Trim() + "__" + value.ToString();
						if (allRefIDConditions.ContainsKey(currDataSetID))
						{
							allRefIDConditions.Remove(currDataSetID);
						}
						allRefIDConditions.Add(currDataSetID, currentDataSetLoopCond);
						currentDataSetLoopCondForParam = currentDataSetLoopCond;
						TraceMessage.Write("SUITEData - includ ed - id & input", currDataSetID + " " + testOrParamName, TraceMessageType.Trace);
						scheduler.TestMsgBroadCaster.genericParamMsgDelegate("StartLoop", id.ToString(), testOrParamName, value, "", "", "", 2);
						SaveSuiteParamToGV(testOrParamName, value, isOutputParam: false);
					}
					else
					{
						TraceMessage.Write("SUITEData - not included-Same as QSPR Global loop - id & input", currDataSetID + " " + testOrParamName, TraceMessageType.Trace);
					}
				}
				else
				{
					TraceMessage.Write("SUITEData - not included - id & input", currDataSetID + " " + testOrParamName, TraceMessageType.Trace);
				}
				break;
			}
			catch (Exception ex2)
			{
				TraceMessage.Write("SUITEData -input param exception", ex2.Message, TraceMessageType.Trace);
				break;
			}
		case 3:
		{
			paramDetails paramDetails = default(paramDetails);
			paramDetails.dataSetID = currDataSetID;
			paramDetails.paramName = testOrParamName;
			if (currentDataSetLoopCondForParam.Length > 1)
			{
				paramDetails.paramName = paramDetails.paramName + " _ " + currentDataSetLoopCondForParam;
			}
			paramDetails.paramValue = value;
			paramDetails.units = units;
			paramDetails.maxLimit = maxLimit;
			paramDetails.minLimit = minLimit;
			TraceMessage.Write("SUITEData - Out", testOrParamName, TraceMessageType.Trace);
			scheduler.TestMsgBroadCaster.genericParamMsgDelegate("OutputParam", id.ToString(), testOrParamName, value, units, minLimit, maxLimit, 2);
			SaveSuiteParamToGV(testOrParamName, value, isOutputParam: true);
			break;
		}
		case 4:
			if (!string.IsNullOrEmpty(currentDataSetLoopCond))
			{
				scheduler.TestMsgBroadCaster.genericParamMsgDelegate("TestResult", "1", "", "", "", "", "", 2);
				scheduler.TestMsgBroadCaster.genericParamMsgDelegate("StartTest", "0", currTest, currInternalTest, "", "", "", 2);
			}
			currDataSetID = id;
			currentDataSetLoopCond = "";
			currentDataSetLoopCondForParam = "";
			currRefIDs.Clear();
			TraceMessage.Write("SUITEData - id ", id.ToString(), TraceMessageType.Trace);
			break;
		case 5:
			TraceMessage.Write("SUITEData - Ref id ", id.ToString(), TraceMessageType.Trace);
			try
			{
				if (!currRefIDs.Contains(id))
				{
					currRefIDs.Add(id);
				}
				if (allRefIDConditions.ContainsKey(id))
				{
					string text = allRefIDConditions[id];
					if (currentDataSetLoopCond.Length > 1)
					{
						currentDataSetLoopCond += "_ _";
					}
					allRefIDConditions.Remove(currDataSetID);
					string[] array = text.Split(new string[1] { "_ _" }, StringSplitOptions.None);
					List<string> list = new List<string>();
					string[] array2 = array;
					foreach (string text2 in array2)
					{
						string[] array3 = text2.Split(new string[1] { "__" }, StringSplitOptions.None);
						string text3 = array3[0];
						if (!currentDataSetLoopCond.StartsWith(text3 + "__") && !currentDataSetLoopCond.Contains("_ _" + text3 + "__") && !list.Contains(text2))
						{
							list.Add(text2);
							scheduler.TestMsgBroadCaster.genericParamMsgDelegate("StartLoop", id.ToString(), array3[0], array3[1], "", "", "", 2);
							currentDataSetLoopCond += text2;
						}
					}
				}
				allRefIDConditions.Add(currDataSetID, currentDataSetLoopCond);
				break;
			}
			catch (Exception ex)
			{
				TraceMessage.Write("SUITEData - Ref id Exception ", ex.Message, TraceMessageType.Trace);
				break;
			}
		case 7:
			scheduler.TestMsgBroadCaster.genericParamMsgDelegate("TestResult", id.ToString(), "", "", "", "", "", 2);
			break;
		case 6:
			break;
		}
	}

	public TestProperties GetHiveTestProperties(string testName)
	{
		TestProperties testProperties = new TestProperties();
		try
		{
			object arrayProperties = null;
			HiveKernel.GetTestProperties(testName, ref arrayProperties);
			object[,] array = (object[,])arrayProperties;
			for (int i = 0; i < array.GetLength(1); i++)
			{
				try
				{
					switch ((string)array[0, i])
					{
					case "Bookkeeping":
						testProperties.BookKeeping = (string)array[1, i] == "1";
						break;
					case "Skip run sequence":
						testProperties.SkipWhenLinked = (string)array[1, i] == "Linked to another document";
						break;
					}
				}
				catch (Exception ex)
				{
					TraceMessage.Write("GetTestProperties for item " + i, ex.Message, TraceMessageType.Exception);
				}
			}
			testProperties.Description = GetHiveTestDescription(testName);
			return testProperties;
		}
		catch (Exception ex2)
		{
			TraceMessage.Write("GetTestProperties", ex2.Message, TraceMessageType.Exception);
			return testProperties;
		}
	}

	public string GetHiveTestDescription(string testName)
	{
		string TestDesc = "";
		try
		{
			HiveKernel.GetTestDescription(testName, ref TestDesc);
			if (TestDesc == "(null)")
			{
				TestDesc = "";
			}
			return TestDesc;
		}
		catch (Exception ex)
		{
			TraceMessage.Write("GetTestDescription", ex.Message, TraceMessageType.Exception);
		}
		return "";
	}

	public void HandleHiveTestToolsClick(string key)
	{
		string text;
		if (QTMData.Kernels.HiveKernel && (text = key) != null && text == "Exit")
		{
			HiveKernel.NotifyEventListeners(key);
		}
	}

	public void IsInAutomation(bool isInAutomation)
	{
		try
		{
			HiveKernel.IsInAutomation(isInAutomation ? 1 : 0);
		}
		catch (Exception ex)
		{
			TraceMessage.Write("QC.QTMApplication.HiveTest", "IsInAutomation " + ex.Message, TraceMessageType.Exception);
		}
	}

	public void SetGlobalVariable(string Name, string Value)
	{
	}

	public void SetGlobalVariable(string Name, int Value)
	{
	}

	public void SetTreeName(string TreeName)
	{
		TraceMessage.Write("HiveTest", "SetTreeName", TraceMessageType.Trace);
		try
		{
			if (QTMData.Kernels.HiveKernel)
			{
				HiveKernel.SetTreeName(TreeName);
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write("QC.QTMApplication.HiveTest", "SetTreeName " + ex.Message, TraceMessageType.Exception);
		}
	}

	public void EnableSUITEOutParams(bool enable)
	{
		try
		{
			if (QTMData.Kernels.HiveKernel)
			{
				HiveKernel.EnableSUITEOutParam(enable ? 1 : 0);
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write("QC.QTMApplication.HiveTest", "EnableSUITEOutParams " + ex.Message, TraceMessageType.Exception);
		}
	}

	public void SetTracingForHiveKernel(bool enableTracing)
	{
		try
		{
			HiveKernel.TraceLogs(enableTracing ? 1 : 0);
		}
		catch (Exception ex)
		{
			TraceMessage.Write("QC.QTMApplication.HiveTest", "SetTracingForHiveKernel " + ex.Message, TraceMessageType.Exception);
		}
	}

	public string GetDotNetTestDescription(string assemblyName, string testName)
	{
		return dotNetKernel.GetTestDescription(assemblyName, testName);
	}

	public TestProperties GetDotNetTestProperties(string assemblyName, string testName)
	{
		try
		{
			TestProperties testProperties = new TestProperties();
			testProperties.TestErrorCode = dotNetKernel.GetTestErrorCode(assemblyName, testName);
			testProperties.Description = dotNetKernel.GetTestDescription(assemblyName, testName);
			DotNetKernel.TestCaseProperties testCaseProperties = dotNetKernel.GetTestCaseProperties(assemblyName, testName);
			testProperties.CanAddToTestPlan = testCaseProperties.canAddToTestPlan;
			testProperties.IsSetupTestStep = testCaseProperties.isSetupTest;
			testProperties.IsTearDownTestStep = testCaseProperties.isTearDownTest;
			testProperties.IsManual = testCaseProperties.isManual;
			return testProperties;
		}
		catch (Exception ex)
		{
			TraceMessage.Write("DotNetTests.cs", "GetTestProperties failed, " + ex.Message, TraceMessageType.Exception);
		}
		return new TestProperties();
	}

	public bool DotNetOptions(string assemblyName, ref Hashtable listOfTestParameter)
	{
		return dotNetKernel.Options(assemblyName, ref listOfTestParameter, isExclusive: false);
	}

	public bool DotNetExOptions(string assemblyName, ref Hashtable listOfTestParameter)
	{
		return dotNetKernel.Options(assemblyName, ref listOfTestParameter, isExclusive: true);
	}
}
