using System;
using System.Windows.Forms;
using System.Xml.Serialization;
using QC.QsprShared.Dialogs;
using QC.QsprShared.Trace;
using QC.QsprTestObjects.Loops;
using QC.QsprTestObjects.Parameters;
using QC.QsprTestObjects.Registry;
using QC.QsprTestObjects.Tests.Base;
using QC.QTMScriptKernel;

namespace QC.QsprTestObjects.Tests.Script;

[Serializable]
public class ScriptTest : BaseTest
{
	private ScriptTestProperties scriptTestProperties = new ScriptTestProperties();

	private ScriptKernel scriptKernel => Root.Scheduler.KernelMgr.scriptKernel;

	public ScriptTestProperties ScriptProperties
	{
		get
		{
			return scriptTestProperties;
		}
		set
		{
			scriptTestProperties = value;
		}
	}

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

	public ScriptTest()
	{
	}

	public ScriptTest(string name, string targetName, RootTest root)
		: base(name, targetName, root)
	{
	}

	public override bool StartTest()
	{
		TraceMessage.Write(ToString(), "ENTER: ScriptTest.StartTest(" + Properties.TestName + ")", TraceMessageType.Trace);
		try
		{
			if (LoadContainer())
			{
				object objParameters = Properties.Parameters.GetParameterObject(Root.Scheduler);
				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();
				}
				object objProperties = new object[9] { text, Properties.TargetName, scriptTestProperties.RedirectStandardOutput, scriptTestProperties.CreateNoWindow, scriptTestProperties.UseShellExecute, scriptTestProperties.Verb, scriptTestProperties.WorkingDirectory, scriptTestProperties.DebugWindowName, scriptTestProperties.ExitAfterStart };
				RunTest(Properties.RealName, ref objParameters, ref objProperties);
				return true;
			}
			AppMessageDialog.Show("Script: " + Properties.TestName + " not found.", "QSPR MessageCenter", MessageBoxButtons.OK, MessageBoxIcon.Hand, DialogResult.OK, MessageDialogType.Error);
			ExceptionHandle("Script: " + Properties.DescriptionName + " not found.");
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "EXCEPTION: ScriptTest.StartTest(" + Properties.TestName + ") " + ex.ToString(), TraceMessageType.Trace);
		}
		TraceMessage.Write(ToString(), "EXIT: ScriptTest.StartTest(" + Properties.TestName + ")", TraceMessageType.Trace);
		return false;
	}

	public override void StopQTMTest()
	{
		TraceMessage.Write(ToString(), "ENTER: ScriptTest.StopQTMTest(" + Properties.TestName + ")", TraceMessageType.Trace);
		scriptKernel.CloseKernel();
		TraceMessage.Write(ToString(), "EXIT: ScriptTest.StopQTMTest(" + Properties.TestName + ")", TraceMessageType.Trace);
	}

	public override void RunQTMTest(string testName, string containerName, ref object objParameters, ref object objProperties)
	{
		TraceMessage.Write(ToString(), "ENTER: ScriptTest.RunQTMTest(" + testName + ")", TraceMessageType.Trace);
		scriptKernel.RunTest(containerName, ref objParameters, ref objProperties);
		TraceMessage.Write(ToString(), "EXIT: ScriptTest.RunQTMTest(" + testName + ")", TraceMessageType.Trace);
	}

	public override void Add(BaseTest baseTest)
	{
	}

	public override void Remove(BaseTest baseTest)
	{
	}

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

	public override void Display(int indent)
	{
	}

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

	public static bool Options(QTMRegistry registry, string containerName, ParameterCollection paramOld, out ParameterCollection paramNew)
	{
		paramNew = null;
		try
		{
			object parameters = null;
			if (paramOld != null)
			{
				parameters = paramOld.GetParameterObject(registry.scheduler);
			}
			ScriptOptionsDialog scriptOptionsDialog = new ScriptOptionsDialog(parameters);
			if (scriptOptionsDialog.ShowDialog() == DialogResult.OK)
			{
				paramNew = new ParameterCollection(scriptOptionsDialog.Parameters);
				return true;
			}
		}
		catch
		{
		}
		return false;
	}
}
