using System;
using System.IO;
using System.Windows.Forms;
using QC.QsprShared.Dialogs;
using QC.QsprTestObjects.Tests.Base;
using QC.QTMInterface;

namespace QC.QsprTestObjects.Registry;

[Serializable]
public class QTMThisPCTarget : QTMTarget
{
	public override QTMTargetType TargetType => QTMTargetType.ThisPC;

	public QTMThisPCTarget()
	{
	}

	public QTMThisPCTarget(string name)
		: base(name)
	{
	}

	public override void RunTest(BaseTest sender, string containerName, ref object objParams, ref object objProperties)
	{
		sender.RuntimeAttributes.TestStartTime = DateTime.Now;
		sender.RuntimeAttributes.TestRan = true;
		sender.RunQTMTest(sender.Properties.RealName, containerName, ref objParams, ref objProperties);
	}

	public override void GetResultsParamInfo(BaseTest sender, ref object obj)
	{
		sender.GetResultsParamInfo(ref obj);
	}

	public override bool LoadContainer(QTMTestContainer testContainer, ref string listOfTests)
	{
		if (File.Exists(testContainer.Name))
		{
			try
			{
				bool HideTests = false;
				bool flag = false;
				switch (testContainer.ContainerType)
				{
				case QC.QsprTestObjects.Tests.Base.QTMTestTypes.HiveTest:
					flag = testContainer.theRegistry.scheduler.KernelMgr.LoadHiveTest(testContainer.Name, ref listOfTests, ref HideTests);
					testContainer.HideTests = HideTests;
					return flag;
				case QC.QsprTestObjects.Tests.Base.QTMTestTypes.ScriptTest:
					return testContainer.theRegistry.scheduler.KernelMgr.LoadScriptTest(testContainer.Name, ref listOfTests);
				case QC.QsprTestObjects.Tests.Base.QTMTestTypes.LabVIEWTest:
					return testContainer.theRegistry.scheduler.KernelMgr.LoadLabViewTest(testContainer.Name, ref listOfTests);
				case QC.QsprTestObjects.Tests.Base.QTMTestTypes.DotNetTest:
					flag = testContainer.theRegistry.scheduler.KernelMgr.LoadDotNetTest(testContainer.Name, ref listOfTests, ref HideTests);
					testContainer.HideTests = HideTests;
					return flag;
				case QC.QsprTestObjects.Tests.Base.QTMTestTypes.DotNetTestEx:
					flag = testContainer.theRegistry.scheduler.KernelMgr.LoadDotNetTest(testContainer.Name, ref listOfTests, ref HideTests);
					testContainer.HideTests = HideTests;
					return flag;
				}
			}
			catch (Exception ex)
			{
				AppMessageDialog.Show(testContainer.Name + " load failed. \nException " + ex.Message + "\nHint:\n*Dependencies not found.", "QSPR - Message Center", MessageBoxButtons.OK, MessageBoxIcon.Hand, DialogResult.OK, MessageDialogType.Exception);
			}
		}
		else
		{
			AppMessageDialog.Show(testContainer.Name + " not found. Load failed.", "QSPR - Message Center", MessageBoxButtons.OK, MessageBoxIcon.Hand, DialogResult.OK, MessageDialogType.Error);
		}
		return false;
	}

	public override void UnloadContainer(QC.QsprTestObjects.Tests.Base.QTMTestTypes sender, string name)
	{
		switch (sender)
		{
		case QC.QsprTestObjects.Tests.Base.QTMTestTypes.HiveTest:
			theRegistry.scheduler.KernelMgr.UnloadHiveTest(name);
			break;
		case QC.QsprTestObjects.Tests.Base.QTMTestTypes.ScriptTest:
			theRegistry.scheduler.KernelMgr.UnloadScriptTest(name);
			break;
		case QC.QsprTestObjects.Tests.Base.QTMTestTypes.LabVIEWTest:
			theRegistry.scheduler.KernelMgr.UnloadLabViewTest(name);
			break;
		case QC.QsprTestObjects.Tests.Base.QTMTestTypes.DotNetTest:
			theRegistry.scheduler.KernelMgr.UnloadDotNetTest(name);
			break;
		case QC.QsprTestObjects.Tests.Base.QTMTestTypes.DotNetTestEx:
			theRegistry.scheduler.KernelMgr.UnloadDotNetTest(name);
			break;
		}
	}

	public override void GetAllParamInfo(BaseTest sender, string realName, string containerName, ref object obj)
	{
		sender.GetAllParamInfo(realName, containerName, ref obj);
	}

	public override void StopTest(BaseTest sender)
	{
		sender.StopQTMTest();
	}

	public override void OnTestMessage(TestMsgType msgType, string testFullPath, TestResult result)
	{
		theRegistry.scheduler.KernelMgr.OnTestMessage(msgType);
	}

	public override void SetGlobalVariable(string Name, string Value)
	{
		theRegistry.scheduler.Env.GlobalVariable.SetGlobalVariable(Name, Value);
	}

	public override void SetGlobalVariable(string Name, object Value)
	{
		theRegistry.scheduler.Env.GlobalVariable.SetGlobalVariableObject(Name, Value);
	}

	public override void SetGlobalVariable(string Name, int Value)
	{
		if (!(Name.Trim() == "CTSUI_STOP") && !(Name.Trim() == "CTSUI_ABORT"))
		{
			theRegistry.scheduler.Env.GlobalVariable.SetGlobalVariable(Name, Value.ToString());
		}
	}

	public override bool MenuMessages(string message)
	{
		return true;
	}

	public override bool GetContainerNames(QC.QsprTestObjects.Tests.Base.QTMTestTypes testType, out string[] containerNames)
	{
		containerNames = null;
		try
		{
			string filter = "";
			switch (testType)
			{
			case QC.QsprTestObjects.Tests.Base.QTMTestTypes.HiveTest:
				filter = "DLL files (*.DLL)|*.DLL|All files (*.*)|*.*";
				break;
			case QC.QsprTestObjects.Tests.Base.QTMTestTypes.ScriptTest:
				filter = "All files (*.*)|*.*|EXE files (*.exe)|*.exe|Batch Files (*.bat)|*.bat";
				break;
			case QC.QsprTestObjects.Tests.Base.QTMTestTypes.LabVIEWTest:
				filter = "VI files (*.VI)|*.VI|All files (*.*)|*.*";
				break;
			case QC.QsprTestObjects.Tests.Base.QTMTestTypes.DotNetTest:
				filter = "DLL files (*.DLL)|*.DLL|All files (*.*)|*.*";
				break;
			case QC.QsprTestObjects.Tests.Base.QTMTestTypes.DotNetTestEx:
				filter = "DLL files (*.DLL)|*.DLL|All files (*.*)|*.*";
				break;
			}
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.Filter = filter;
			openFileDialog.FilterIndex = 1;
			openFileDialog.RestoreDirectory = true;
			openFileDialog.Multiselect = true;
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				containerNames = openFileDialog.FileNames;
				return true;
			}
		}
		catch
		{
		}
		return false;
	}

	public override void CloseKernels()
	{
		theRegistry.scheduler.KernelMgr.ShutdownHiveKernel();
		theRegistry.scheduler.KernelMgr.ShutdownScriptKernel();
		theRegistry.scheduler.KernelMgr.ShutdownLabVIEWKernel();
		theRegistry.scheduler.KernelMgr.ShutdownDotNetKernel();
	}
}
