using System;
using System.Windows.Forms;
using System.Xml.Serialization;
using QC.GlobalVariableManager;
using QC.QsprShared.Dialogs;
using QC.QsprShared.Trace;
using QC.QsprTestObjects.Loops;
using QC.QsprTestObjects.Parameters;
using QC.QsprTestObjects.Tests.Base;
using QC.QTMDotNetKernel;
using QC.QTMInterface;

namespace QC.QsprTestObjects.Tests.DotNet;

[Serializable]
public class DotNetTest : BaseTest
{
	protected DotNetTestProperties dotNetTestProperties = new DotNetTestProperties();

	internal DotNetKernel dotNetKernel => Root.Scheduler.KernelMgr.dotNetKernel;

	[XmlIgnore]
	public override TestProperties Properties
	{
		get
		{
			dotNetTestProperties.Owner = this;
			return dotNetTestProperties;
		}
		set
		{
			dotNetTestProperties = (DotNetTestProperties)value;
			dotNetTestProperties.Owner = this;
		}
	}

	public DotNetTestProperties DotNetProperties
	{
		get
		{
			return dotNetTestProperties;
		}
		set
		{
			dotNetTestProperties = value;
		}
	}

	public DotNetTest()
	{
	}

	public DotNetTest(string name, string targetName, RootTest root)
		: base(name, targetName, root)
	{
		string[] array = name.Split('.');
		Properties.TestName = array[array.Length - 1];
	}

	public override BaseTest Clone()
	{
		DotNetTest dotNetTest = new DotNetTest();
		dotNetTest.Properties = Properties.Clone();
		base.Copy(dotNetTest);
		return dotNetTest;
	}

	public override void GetAllParamInfo(string realName, string containerName, ref object objParameters)
	{
		try
		{
			ParameterCollection oldParams = new ParameterCollection(objParameters);
			objParameters = null;
			dotNetKernel.GetAllParamInfo(containerName, realName, ref objParameters);
			GetSelectedParameter(oldParams, ref objParameters);
		}
		catch
		{
		}
	}

	protected void GetSelectedParameter(ParameterCollection oldParams, ref object objParameters)
	{
		try
		{
			object[] array = (object[])objParameters;
			ParameterCollection parameterCollection = new ParameterCollection(array[0]);
			ParameterCollection parameterCollection2 = new ParameterCollection(array[1]);
			ParameterCollection parameterCollection3 = new ParameterCollection(array[2]);
			ParameterCollection parameterCollection4 = new ParameterCollection();
			foreach (ParameterCls item in parameterCollection2)
			{
				if (oldParams.HasParameter(item.Name))
				{
					ParameterCls parameter = oldParams.GetParameter(item.Name);
					item.Selected = parameter.Selected;
					parameterCollection4.Add(item);
				}
				else if (item.Selected)
				{
					parameterCollection4.Add(item);
				}
			}
			foreach (ParameterCls item2 in parameterCollection3)
			{
				if (oldParams.HasParameter(item2.Name))
				{
					item2.Selected = true;
					parameterCollection4.Add(item2);
				}
			}
			parameterCollection4 = parameterCollection4.Sort();
			foreach (ParameterCls item3 in parameterCollection4)
			{
				parameterCollection.Add(item3);
			}
			objParameters = parameterCollection.GetParameterObjectInputFormat();
		}
		catch
		{
		}
	}

	public override bool StartTest()
	{
		TraceMessage.WriteAlways(ToString(), "ENTER: DotNetTest.StartTest(" + Properties.TestName + ")", TraceMessageType.Trace);
		TraceMessage.WriteAlways(ToString(), "StartTest: " + Properties.TestName, TraceMessageType.Trace);
		try
		{
			if (!LoadContainer())
			{
				AppMessageDialog.Show("DotNetTest: " + Properties.TestName + " not found.", "QSPR MessageCenter", MessageBoxButtons.OK, MessageBoxIcon.Hand, DialogResult.OK, MessageDialogType.Error);
				ExceptionHandle("DotNetTest: " + Properties.TestName + " not found.");
				runtimeAttributes.Exception = true;
				Root.Scheduler.GlobalStatus.GlobalTestResult = TestResult.Exception;
				return false;
			}
			try
			{
				BaseTest baseTest = null;
				baseTest = this;
				UpdateDynamicLimits(baseTest);
			}
			catch
			{
			}
			object objParameters = Properties.Parameters.GetParameterObject(Root.Scheduler);
			object[] array = new object[4];
			if (Properties.DescriptionName.Trim() == "")
			{
				((ScenarioHandler)Root.Scheduler.QTMData.scenarioHandler).Update(Root.Scheduler.GlobalStatus);
				Root.Scheduler.GlobalStatus.LoopDetails = Root.Scheduler.GlobalStatus.LoopDetailsComplete;
			}
			else
			{
				((ScenarioHandler)Root.Scheduler.QTMData.scenarioHandler).Update(Root.Scheduler.GlobalStatus);
			}
			string text = Properties.TestName;
			if (Properties.DescriptionName.Trim() != "")
			{
				text = Properties.DescriptionName.Trim();
			}
			array[0] = text;
			array[1] = Properties.TargetName;
			array[2] = DotNetProperties.FailString.Trim();
			array[3] = DotNetProperties.CreateNewInstance;
			object objProperties = array;
			RunTest(Properties.RealName.Trim(), ref objParameters, ref objProperties);
		}
		catch (Exception ex)
		{
			TraceMessage.WriteAlways(ToString(), "EXCEPTION: DotNetTest.StartTest(" + Properties.TestName + ") " + ex.ToString(), TraceMessageType.Trace);
		}
		TraceMessage.WriteAlways(ToString(), "EXIT: DotNetTest.StartTest(" + Properties.TestName + ")", TraceMessageType.Trace);
		return true;
	}

	public override void RunQTMTest(string testName, string containerName, ref object objParameters, ref object objProperties)
	{
		TraceMessage.WriteAlways(ToString(), "ENTER: DotNetTest.RunQTMTest(" + testName + ")", TraceMessageType.Trace);
		dotNetKernel.RunTest(containerName, testName.Trim(), ref objParameters, ref objProperties, CheckForFastModeSkipAll());
		VerifySkipAllManualIsSet(testName.Trim());
		TraceMessage.WriteAlways(ToString(), "EXIT: DotNetTest.RunQTMTest(" + testName + ")", TraceMessageType.Trace);
	}

	private void VerifySkipAllManualIsSet(string testName)
	{
		if (Root.RootProperties.TreeType == TreeTypes.TestPlan && (testName.IndexOf("TestPlanStartup", StringComparison.OrdinalIgnoreCase) >= 0 || name.IndexOf("TestPlanShutdown", StringComparison.OrdinalIgnoreCase) >= 0) && QTMGlobalVariable.GetGlobalVariable("FastModeSkipAll", out var gvValue) && string.Compare(gvValue, "true", ignoreCase: true) == 0)
		{
			QTMGlobalVariable.SetGlobalVariable("TestCaseExecutionMode", "SkipAllManual");
		}
	}

	private bool CheckForFastModeSkipAll()
	{
		if (Root.RootProperties.TreeType == TreeTypes.TestPlan && QTMGlobalVariable.GetGlobalVariable("FastModeSkipAll", out var gvValue) && string.Compare(gvValue, "true", ignoreCase: true) == 0 && QTMGlobalVariable.GetGlobalVariable("TestCaseExecutionMode", out var gvValue2) && string.Compare(gvValue2, "SkipAllManual", ignoreCase: true) == 0)
		{
			return true;
		}
		return false;
	}

	public override void StopQTMTest()
	{
		TraceMessage.WriteAlways(ToString(), "ENTER: DotNetTest.StopQTMTest(" + Properties.TestName + ")", TraceMessageType.Trace);
		dotNetKernel.StopTest();
		TraceMessage.WriteAlways(ToString(), "EXIT: DotNetTest.StopQTMTest(" + Properties.TestName + ")", TraceMessageType.Trace);
	}

	public DotNetTest Copy(DotNetTest b)
	{
		Properties = b.Properties;
		base.Copy(b);
		return this;
	}

	public override void Add(BaseTest baseTest)
	{
	}

	public override void Remove(BaseTest baseTest)
	{
	}

	public override void Display(int indent)
	{
	}

	public override void Insert(BaseTest baseTest, int index)
	{
	}
}
