using System;
using System.Runtime.InteropServices;
using QC.QSPRAutomation.Internal;
using QC.QTMInterface;

namespace QC.QSPRAutomationInterface;

[ProgId("QSPR3.XTTDocument")]
[ComSourceInterfaces(typeof(IXTTDocumentEvents))]
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class XTTDocument : IXTTDocument
{
	[ComVisible(false)]
	public delegate void EventTargetGoNoGoDone(int bPass);

	[ComVisible(false)]
	public delegate void EventTestStarted(string testName, string testPath);

	[ComVisible(false)]
	public delegate void EventTestDone(string testName, int testStatus);

	[ComVisible(false)]
	public delegate void EventTargetStatus();

	[ComVisible(false)]
	public delegate void EventSendStatus(string status);

	[ComVisible(false)]
	public delegate void EventGoNoGoStart(DateTime unitStartTime);

	[ComVisible(false)]
	public delegate void EventGoNoGoStartWithCount(DateTime unitStartTime, int testCount);

	[ComVisible(false)]
	public delegate void EventGoNoGoDidNotStart(string errorCode);

	[ComVisible(false)]
	public delegate void EventLoggingDone(string pluginName);

	private string lastCallInfo = "";

	private string lastError = "No Error";

	private Position importTestPosition = Position.Last;

	private bool sendAllTestDoneMessages = true;

	private bool sendAllTestStartMessages = true;

	private int goNoGoResult;

	[ComVisible(false)]
	private QSPRInstance qsprInstance;

	[ComVisible(false)]
	private int phoneIndex;

	[ComVisible(false)]
	private string sUUID = "";

	public int GoNoGoStatus
	{
		get
		{
			lastCallInfo = "GoNoGoStatus";
			int result = qsprInstance.GoNoGoStatus();
			qsprInstance.AppendToLocalLog("sending current run status: " + result);
			return result;
		}
	}

	public event EventTargetGoNoGoDone GoNoGoDone;

	public event EventTargetStatus XTTClose;

	public event EventTargetStatus XTTStop_;

	public event EventTargetStatus XTTPause;

	public event EventTargetStatus XTTResume;

	public event EventSendStatus SendStatus;

	public event EventTestStarted TestStarted;

	public event EventTestDone TestDone;

	public event EventGoNoGoStart GoNoGoStart;

	public event EventGoNoGoStartWithCount GoNoGoStartWithCount;

	public event EventGoNoGoDidNotStart GoNoGoDidNotStart;

	public event EventLoggingDone LoggingDone;

	public int GoNoGo()
	{
		lastCallInfo = "GoNoGo()";
		if (!qsprInstance.RunTree(sUUID, phoneIndex))
		{
			lastError = "GoNoGo failed";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		return 1;
	}

	public int ResumeTree()
	{
		lastCallInfo = "ResumeTree()";
		if (!qsprInstance.ResumeTree())
		{
			lastError = "ResumeTree failed";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		return 1;
	}

	public int RunTest(string TestName)
	{
		if (TestName == null)
		{
			lastCallInfo = "RunTest was called with an invalid TestName";
			lastError = "Failed to run the test, TestName cannot be null";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		lastCallInfo = "RunTest(" + TestName + ")";
		if (!qsprInstance.RunTest(TestName))
		{
			lastError = "RunTest failed";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		return 1;
	}

	public int CloseXTT()
	{
		lastCallInfo = "CloseXTT()";
		if (qsprInstance.NewXTT())
		{
			if (this.XTTClose != null)
			{
				this.XTTClose();
			}
			return 1;
		}
		qsprInstance.SetErrorsExist(1);
		lastError = "CloseXTT failed";
		return 0;
	}

	public int SetTestParameterInAllTests(string ParamName, object newVal)
	{
		if (string.IsNullOrEmpty(ParamName) || newVal == null)
		{
			lastCallInfo = "SetTestParameterInAllTests was called with some null or empty parameter values";
			lastError = "Failed to set the Test parameter in all tests, one or more arguments were null or empty";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		lastCallInfo = "SetTestParameterInAllTests(" + ParamName + ", " + newVal.ToString() + ")";
		string text = newVal.GetType().Name.ToString();
		string text2 = "";
		if (text.CompareTo("String") == 0)
		{
			text2 = (string)newVal;
		}
		else if (text.CompareTo("Int32") == 0)
		{
			text2 = ((int)newVal).ToString();
		}
		else if (text.CompareTo("Int32") == 0)
		{
			text2 = ((int)newVal).ToString();
		}
		else if (text.CompareTo("Int16") == 0)
		{
			text2 = ((short)newVal).ToString();
		}
		else if (text.CompareTo("Float") == 0)
		{
			text2 = ((float)newVal).ToString();
		}
		else
		{
			if (text.CompareTo("Double") != 0)
			{
				lastError = "Failed to set the Test parameter in all tests, invalid value";
				qsprInstance.SetErrorsExist(1);
				return 0;
			}
			text2 = ((double)newVal).ToString();
		}
		if (!qsprInstance.SetTestParameterInAllTests(ParamName, text2))
		{
			lastError = "Failed to set the Test parameter in all tests";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		return 1;
	}

	public int SetSkipRunSequence(string TestName, int newVal)
	{
		if (TestName != null)
		{
			lastCallInfo = "SetSkipRunSequence(" + TestName + ", " + newVal + ")";
			if (newVal < 0 || newVal > 5)
			{
				lastError = "SetSkipRunSequence failed. Invalid skip value specified. This argument must be an integer value in the range 0-5.";
				qsprInstance.SetErrorsExist(1);
				return 0;
			}
			if (!qsprInstance.SetSkipRunSequence(TestName, (SkipRunSequence)newVal))
			{
				lastError = "SetSkipRunSequence failed";
				qsprInstance.SetErrorsExist(1);
				return 0;
			}
			return 1;
		}
		lastCallInfo = "SetSkipRunSequence was called with one or more null arguments";
		lastError = "SetSkipRunSequence failed with one or more null arguments";
		qsprInstance.SetErrorsExist(1);
		return 0;
	}

	public int ShowWindow()
	{
		lastCallInfo = "ShowWindow()";
		if (!qsprInstance.ShowWindow())
		{
			lastError = "Failed to show window";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		return 1;
	}

	public int HideWindow()
	{
		lastCallInfo = "HideWindow()";
		if (!qsprInstance.HideWindow())
		{
			lastError = "Failed to hide window";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		return 1;
	}

	public int SetTestParameter(string TestName, string ParamName, object newVal)
	{
		if (TestName != null && ParamName != null && newVal != null)
		{
			lastCallInfo = string.Concat("SetTestParameter(", TestName, ", ", ParamName, ", ", newVal, ")");
			string text = newVal.GetType().Name.ToString();
			string text2 = "";
			if (text.CompareTo("String") == 0)
			{
				text2 = (string)newVal;
			}
			else if (text.CompareTo("Int32") == 0)
			{
				text2 = ((int)newVal).ToString();
			}
			else if (text.CompareTo("Int16") == 0)
			{
				text2 = ((short)newVal).ToString();
			}
			else if (text.CompareTo("Float") == 0)
			{
				text2 = ((float)newVal).ToString();
			}
			else
			{
				if (text.CompareTo("Double") != 0)
				{
					lastError = "Failed to set the Test parameter, invalid value";
					qsprInstance.SetErrorsExist(1);
					return 0;
				}
				text2 = ((double)newVal).ToString();
			}
			if (!qsprInstance.SetTestParameter(TestName, ParamName, text2, "", "", isTestIndex: false))
			{
				lastError = "Failed to set the Test parameter";
				qsprInstance.SetErrorsExist(1);
				return 0;
			}
			return 1;
		}
		lastCallInfo = "SetTestParameter was called with some null values";
		lastError = "SetTestParameter failed with one or more null arguments";
		qsprInstance.SetErrorsExist(1);
		return 0;
	}

	public int SetTestParametersInFolder(string paramName, string paramValue, string testTreeFolderName)
	{
		if (paramName != null && paramValue != null && testTreeFolderName != null)
		{
			lastCallInfo = "SetTestParametersInFolder(" + paramName + ", " + paramValue + ", " + testTreeFolderName + ")";
			if (!qsprInstance.SetTestParametersInFolder(paramName, paramValue, testTreeFolderName))
			{
				lastError = "Failed to set the test parameter";
				qsprInstance.SetErrorsExist(1);
				return 0;
			}
			return 1;
		}
		lastCallInfo = "SetTestParametersInFolder was called with one or more null values";
		lastError = "SetTestParametersInFolder failed with one or more null arguments";
		qsprInstance.SetErrorsExist(1);
		return 0;
	}

	public int RenameTest(string oldTestName, string newTestName)
	{
		if (oldTestName != null && newTestName != null)
		{
			lastCallInfo = "RenameTest(" + oldTestName + ", " + newTestName + ")";
			if (!qsprInstance.RenameTest(oldTestName, newTestName))
			{
				lastError = "Failed to rename the test";
				qsprInstance.SetErrorsExist(1);
				return 0;
			}
			return 1;
		}
		lastCallInfo = "RenameTest was called with one or more null arguments";
		lastError = "RenameTest failed with one or more null arguments";
		qsprInstance.SetErrorsExist(1);
		return 0;
	}

	public int ImportTests(string TestList, string treeFilePath, string DestinationFolder)
	{
		if (TestList != null && treeFilePath != null && DestinationFolder != null)
		{
			lastCallInfo = "ImportTests(" + TestList + ", " + treeFilePath + ", " + DestinationFolder + ")";
			if (!qsprInstance.ImportTests(TestList, treeFilePath, DestinationFolder, insertInFolder: true, importTestPosition))
			{
				lastError = "Failed to import the tests";
				qsprInstance.SetErrorsExist(1);
				return 0;
			}
			return 1;
		}
		lastCallInfo = "ImportTests was called with one or more null arguments";
		lastError = "ImportTests failed with one or more null arguments";
		qsprInstance.SetErrorsExist(1);
		return 0;
	}

	public int SetImportTestsPosition(int pos)
	{
		lastCallInfo = "SetImportTestsPosition(" + pos + ")";
		switch (pos)
		{
		case 0:
			importTestPosition = Position.First;
			return 1;
		case 1:
			importTestPosition = Position.Last;
			return 1;
		default:
			lastError = "Failed to set import test position, an invalid value was specified";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
	}

	public int RemoveRootFolder(int removeRoot)
	{
		lastCallInfo = "RemoveRootFolder(" + removeRoot + ")";
		if (!qsprInstance.RemoveRootFolder(removeRoot))
		{
			lastError = "Failed to Remove root folder";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		return 1;
	}

	public int DeleteTest(string TestName)
	{
		lastCallInfo = "DeleteTest(" + TestName + ")";
		if (!qsprInstance.DeleteTest(TestName))
		{
			lastError = "Failed to delete the test: " + TestName;
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		return 1;
	}

	public int SetLoopType(string folderName, int countdownValue, string parameterName, string loopValue, int loopType)
	{
		if (folderName == null || parameterName == null || loopValue == null)
		{
			lastCallInfo = "SetLoopType was called with one or more null arguments";
			lastError = "Failed to set loop type, one or more arguments are null";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		if (loopType < 0 || loopType > 4)
		{
			lastError = "Failed to set loop type, an invalid loop type was specified. This argument must be an integer value in the range 0-4.";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		string[] value = new string[5]
		{
			folderName,
			countdownValue.ToString(),
			parameterName,
			loopValue,
			loopType.ToString()
		};
		lastCallInfo = "SetLoopType(" + string.Join(",", value) + ")";
		if (!qsprInstance.SetLoopType(folderName, countdownValue, parameterName, loopValue, loopType, isTestIndex: false, "", ""))
		{
			lastError = "Failed to set loop type";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		return 1;
	}

	public int SetLoopTypeInFolder(string folderName, int countdownValue, string parameterName, string loopValue, int loopType, string inFolderName)
	{
		if (folderName == null || parameterName == null || loopValue == null || inFolderName == null)
		{
			lastCallInfo = "SetLoopTypeInFolder was called with one or more null arguments";
			lastError = "Failed to set loop type, one or more arguments are null";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		if (loopType < 0 || loopType > 3)
		{
			lastError = "Failed to set loop type, an invalid loop type was specified. This argument must be an integer value in the range 0-3.";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		string[] value = new string[6]
		{
			folderName,
			countdownValue.ToString(),
			parameterName,
			loopValue,
			loopType.ToString(),
			inFolderName
		};
		lastCallInfo = "SetLoopTypeInFolder(" + string.Join(",", value) + ")";
		if (!qsprInstance.SetLoopTypeInFolder(folderName, countdownValue, parameterName, loopValue, loopType, inFolderName))
		{
			lastError = "Failed to set loop type in a folder";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		return 1;
	}

	public int SetRootName(string RootFolderName)
	{
		if (string.IsNullOrEmpty(RootFolderName))
		{
			lastCallInfo = "SetRootName was called with a null or empty name";
			lastError = "SetRootName failed, the name was null or empty";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		lastCallInfo = "SetRootName(" + RootFolderName + ")";
		if (!qsprInstance.SetRootName(RootFolderName))
		{
			lastError = "Failed to set the Root name";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		return 1;
	}

	public TestNode CreateFolder(string subFolderName, string destinationFolderName)
	{
		if (subFolderName == null || destinationFolderName == null)
		{
			lastCallInfo = "CreateFolder was called with one or more null values";
			lastError = "CreateFolder failed, one or more values were null";
			qsprInstance.SetErrorsExist(1);
			return null;
		}
		lastCallInfo = "CreateFolder(" + subFolderName + "," + destinationFolderName + ")";
		string text = qsprInstance.CreateFolder(subFolderName, destinationFolderName, isTestIndex: false);
		if (text != null)
		{
			TestNode testNode = new TestNode();
			testNode.XttDocument = this;
			testNode.TestIndex = text;
			testNode.SetAutomationObject(qsprInstance);
			return testNode;
		}
		lastError = "Failed to create folder.";
		qsprInstance.SetErrorsExist(1);
		return null;
	}

	public int SetFolderPosition(int insertPosition)
	{
		lastCallInfo = "SetFolderPosition(" + insertPosition + ")";
		if (!qsprInstance.SetFolderInsertPosition(insertPosition))
		{
			lastError = "Failed to set folder position. Setting default position to First";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		return 1;
	}

	public int Save()
	{
		lastCallInfo = "Save()";
		if (qsprInstance.Save())
		{
			return 1;
		}
		lastError = "Save file failed";
		qsprInstance.SetErrorsExist(1);
		return 0;
	}

	public int SaveAs(string treeFilePath)
	{
		if (string.IsNullOrEmpty(treeFilePath))
		{
			lastCallInfo = "SaveAs called with invalid treeFilePath";
			lastError = "SaveAs failed, the treeFilePath is not valid";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		lastCallInfo = "SaveAs(" + treeFilePath + ")";
		if (qsprInstance.SaveAs(treeFilePath))
		{
			return 1;
		}
		lastError = "SaveAs failed";
		qsprInstance.SetErrorsExist(1);
		return 0;
	}

	public int XTTStop()
	{
		lastCallInfo = "XTTStop()";
		if (!qsprInstance.XTTStop())
		{
			lastError = "Failed to Stop";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		if (this.XTTStop_ != null)
		{
			this.XTTStop_();
		}
		return 1;
	}

	public int XTTAbort()
	{
		lastCallInfo = "XTTAbort()";
		if (!qsprInstance.XTTAbort())
		{
			lastError = "Failed to Abort";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		if (this.XTTStop_ != null)
		{
			this.XTTStop_();
		}
		return 1;
	}

	public string GetLastError()
	{
		return lastError;
	}

	public string LastCallInfo()
	{
		return lastCallInfo;
	}

	public int SyncTest(string testName)
	{
		testName = testName ?? string.Empty;
		lastCallInfo = "SyncTest(" + testName + ")";
		if (qsprInstance.SyncTest(testName))
		{
			return 1;
		}
		lastError = "SyncTest failed";
		qsprInstance.SetErrorsExist(1);
		return 0;
	}

	public int AddCTSDll(string dllPath)
	{
		if (string.IsNullOrEmpty(dllPath))
		{
			lastCallInfo = "AddCTSDll called with invalid dllPath";
			lastError = "AddCTSDll failed, dllPath is not valid";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		lastCallInfo = "AddCTSDll(" + dllPath + ")";
		if (qsprInstance.AddTestContainer(dllPath, QTMTestTypes.CTSTest))
		{
			return 1;
		}
		lastError = "AddCTSDll failed";
		qsprInstance.SetErrorsExist(1);
		return 0;
	}

	public int AddSUITEDll(string dllPath)
	{
		if (string.IsNullOrEmpty(dllPath))
		{
			lastCallInfo = "AddSUITEDll called with invalid dllPath";
			lastError = "AddSUITEDll failed, dllPath is not valid";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		lastCallInfo = "AddSUITEDll(" + dllPath + ")";
		if (qsprInstance.AddTestContainer(dllPath, QTMTestTypes.HiveTest))
		{
			return 1;
		}
		lastError = "AddSUITEDll failed";
		qsprInstance.SetErrorsExist(1);
		return 0;
	}

	public int AddScriptTest(string filePath)
	{
		if (string.IsNullOrEmpty(filePath))
		{
			lastCallInfo = "AddScriptTest called with invalid filePath";
			lastError = "AddScriptTest failed, filePath is not valid";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		lastCallInfo = "AddScriptTest(" + filePath + ")";
		if (qsprInstance.AddTestContainer(filePath, QTMTestTypes.ScriptTest))
		{
			return 1;
		}
		lastError = "AddScriptTest failed";
		qsprInstance.SetErrorsExist(1);
		return 0;
	}

	public int AddLabViewVI(string filePath)
	{
		if (string.IsNullOrEmpty(filePath))
		{
			lastCallInfo = "AddLabViewVI called with invalid filePath";
			lastError = "AddLabViewVI failed, filePath is not valid";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		lastCallInfo = "AddLabViewVI(" + filePath + ")";
		if (qsprInstance.AddTestContainer(filePath, QTMTestTypes.LabVIEWTest))
		{
			return 1;
		}
		lastError = "AddLabViewVI failed";
		qsprInstance.SetErrorsExist(1);
		return 0;
	}

	public int AddDotNetDll(string dllPath)
	{
		if (string.IsNullOrEmpty(dllPath))
		{
			lastCallInfo = "AddDotNetDll called with invalid dllPath";
			lastError = "AddDotNetDll failed, dllPath is not valid";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		lastCallInfo = "AddDotNetDll(" + dllPath + ")";
		if (qsprInstance.AddTestContainer(dllPath, QTMTestTypes.DotNetTest))
		{
			return 1;
		}
		lastError = "AddDotNetDll failed";
		qsprInstance.SetErrorsExist(1);
		return 0;
	}

	public TestNode GetTestNode(string testName)
	{
		if (testName == null)
		{
			lastCallInfo = "GetTestNode called with invalid TestName";
			lastError = "GetTestNode called with invalid first argument. TestName can't be null";
			qsprInstance.SetErrorsExist(1);
			return null;
		}
		lastCallInfo = "GetTestNode(" + testName + ")";
		string testIndex = qsprInstance.GetTestIndex(testName);
		if (testIndex != null)
		{
			TestNode testNode = new TestNode();
			testNode.XttDocument = this;
			testNode.TestIndex = testIndex;
			testNode.SetAutomationObject(qsprInstance);
			return testNode;
		}
		lastError = "GetTestNode failed";
		qsprInstance.SetErrorsExist(1);
		return null;
	}

	public int AddScriptParameter(string scriptTestName, string parameterName, int ioType, int dataType, string value, string unit, string description, string lowerLimit, string upperLimit)
	{
		if (string.IsNullOrEmpty(scriptTestName))
		{
			lastCallInfo = "AddScriptParameter called with invalid argument scriptTestName";
			lastError = "AddScriptParameter called with invalid argument scriptTestName. This argument can't be null or empty.";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		if (string.IsNullOrEmpty(parameterName))
		{
			lastCallInfo = "AddScriptParameter called with invalid argument parameterName";
			lastError = "AddScriptParameter called with invalid argument parameterName. This argument can't be null or empty.";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		if (ioType != 0 && ioType != 1)
		{
			lastCallInfo = "AddScriptParameter called with invalid argument ioType";
			lastError = "AddScriptParameter called with invalid argument ioType. This argument must be an integer value 0 or 1.";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		if (dataType != 0 && dataType != 1 && dataType != 2)
		{
			lastCallInfo = "AddScriptParameter called with invalid argument dataType";
			lastError = "AddScriptParameter called with invalid argument dataType. This argument must be an integer value 0, 1 or 2.";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		lastCallInfo = "AddScriptParameter(" + scriptTestName + ", " + parameterName + ", " + ioType + ", " + dataType + ", " + value + ", " + unit + ", " + description + ", " + lowerLimit + ", " + upperLimit + ")";
		if (qsprInstance.AddScriptParameter(scriptTestName, parameterName, ioType, dataType, value, unit, description, lowerLimit, upperLimit))
		{
			return 1;
		}
		lastError = "AddScriptParameter failed";
		qsprInstance.SetErrorsExist(1);
		return 0;
	}

	public int RegisterTree(string serverName, string databaseName, int regsiterWithoutDatabase)
	{
		if (serverName == null || databaseName == null)
		{
			lastCallInfo = "RegisterTree called with one or more null arguments";
			lastError = "RegisterTree failed, one or more arguments were null";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		lastCallInfo = "RegisterTree(" + serverName + ", " + databaseName + ", " + regsiterWithoutDatabase + ")";
		if (regsiterWithoutDatabase < 0 || regsiterWithoutDatabase > 1)
		{
			lastError = "RegisterTree failed, invalid argument: regsiterWithoutDatabase. This argument must be an integer value of 0 or 1.";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		if (qsprInstance.RegisterTree(serverName, databaseName, regsiterWithoutDatabase))
		{
			return 1;
		}
		lastError = "RegisterTree failed";
		qsprInstance.SetErrorsExist(1);
		return 0;
	}

	public int SetParameterLimits(string TestName, string ParamName, string LowerLimit, string UpperLimit)
	{
		if (TestName == null || ParamName == null || LowerLimit == null || UpperLimit == null)
		{
			lastCallInfo = "SetParameterLimits called with one or more null arguments";
			lastError = "SetParameterLimits failed, one or more arguments were null";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		lastCallInfo = "SetParameterLimits(" + TestName + ", " + ParamName + ", " + LowerLimit + ", " + UpperLimit + ")";
		if (!qsprInstance.SetParameterLimits(TestName, ParamName, LowerLimit, UpperLimit))
		{
			lastError = "Failed to set the Test parameter limits";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		return 1;
	}

	public int SaveAsQSPRFormat(string treeFilePath)
	{
		if (string.IsNullOrEmpty(treeFilePath))
		{
			lastCallInfo = "SaveAsQSPRFormat called with invalid treefilePath";
			lastError = "SaveAsQSPRFormat called with invalid treefilePath";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		lastCallInfo = "SaveAsQSPRFormat(" + treeFilePath + ")";
		if (!qsprInstance.SaveAsQSPRFormat(treeFilePath))
		{
			lastError = "Failed to save file in QSPR format";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		return 1;
	}

	public int SelectTestsByIndex(string guids)
	{
		if (string.IsNullOrEmpty(guids))
		{
			lastCallInfo = "SelectTestsByIndex called with invalid guids";
			lastError = "SelectTestsByIndex called with invalid guids";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		lastCallInfo = "SelectTestsByIndex(" + guids + ")";
		if (!qsprInstance.SelectTestsByIndex(guids))
		{
			lastError = "Failed to select tests by index";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		return 1;
	}

	public int SelectTestsByName(string names)
	{
		if (string.IsNullOrEmpty(names))
		{
			lastCallInfo = "SelectTestsByName called with invalid names";
			lastError = "SelectTestsByName called with invalid names";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		lastCallInfo = "SelectTestsByName(" + names + ")";
		if (!qsprInstance.SelectTestsByName(names))
		{
			lastError = "Failed to select tests by names";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		return 1;
	}

	public int EnableLoopValues(string TestName, string LoopName, string Values, int Enable)
	{
		if (string.IsNullOrEmpty(TestName))
		{
			lastCallInfo = "EnableLoopValues called with invalid param: TestName";
			lastError = "EnableLoopValues called with invalid param: TestName";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		if (string.IsNullOrEmpty(LoopName))
		{
			lastCallInfo = "EnableLoopValues called with invalid param: LoopName";
			lastError = "EnableLoopValues called with invalid param: LoopName";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		if (string.IsNullOrEmpty(Values))
		{
			lastCallInfo = "EnableLoopValues called with invalid param: Values";
			lastError = "EnableLoopValues called with invalid param: Values";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		if (Enable < 0 || Enable > 1)
		{
			lastCallInfo = "EnableLoopValues called with invalid param: Enable. Value should be 0 or 1.";
			lastError = "EnableLoopValues called with invalid param: Enable. Value should be 0 or 1.";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		lastCallInfo = "EnableLoopValues(" + TestName + "," + LoopName + "," + Values + "," + Enable + ")";
		if (!qsprInstance.EnableLoopValues(TestName, LoopName, Values, Enable == 1))
		{
			lastError = "Failed to enable loop values";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		return 1;
	}

	public string GetLoopValues(string TestName, string LoopName)
	{
		if (string.IsNullOrEmpty(TestName))
		{
			lastCallInfo = "GetLoopValues called with invalid param: TestName";
			lastError = "GetLoopValues called with invalid param: TestName";
			qsprInstance.SetErrorsExist(1);
			return "";
		}
		if (string.IsNullOrEmpty(LoopName))
		{
			lastCallInfo = "GetLoopValues called with one or more null arguments: LoopName";
			lastError = "GetLoopValues failed, one or more arguments were null: LoopName";
			qsprInstance.SetErrorsExist(1);
			return "";
		}
		lastCallInfo = "GetLoopValues(" + TestName + "," + LoopName + ")";
		return qsprInstance.GetLoopValues(TestName, LoopName);
	}

	internal void ProcessQSPRExited(string instanceIdentifier, int exitCode)
	{
		if (this.SendStatus != null)
		{
			this.SendStatus(instanceIdentifier + ": Exit Code ProcessQSPRExited " + exitCode);
		}
	}

	internal void ProcessStartedDelegate()
	{
		if (this.SendStatus != null)
		{
			this.SendStatus("QSPR Started!");
		}
	}

	internal void ProcessShownDelegate(bool shownOkay)
	{
		if (this.SendStatus != null)
		{
			this.SendStatus("QSPR Shown " + shownOkay);
		}
	}

	internal void ProcessHiddenDelegate(bool hiddenOkay)
	{
		if (this.SendStatus != null)
		{
			this.SendStatus("QSPR Hidden " + hiddenOkay);
		}
	}

	public int GetAllTestDoneMessages(int getAllTestDoneMessages)
	{
		lastCallInfo = "GetAllTestDoneMessages(" + getAllTestDoneMessages + ")";
		switch (getAllTestDoneMessages)
		{
		case 0:
			sendAllTestDoneMessages = false;
			return 1;
		case 1:
			sendAllTestDoneMessages = true;
			return 1;
		default:
			lastError = "GetAllTestDoneMessages failed, the value specified was not a 0 or 1.";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
	}

	public int GetAllTestStartMessages(int getAllTestStartMessages)
	{
		lastCallInfo = "GetAllTestStartMessages(" + getAllTestStartMessages + ")";
		switch (getAllTestStartMessages)
		{
		case 0:
			sendAllTestStartMessages = false;
			return 1;
		case 1:
			sendAllTestStartMessages = true;
			return 1;
		default:
			lastError = "GetAllTestStartMessages failed, the value specified was not a 0 or 1.";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
	}

	internal void ProcessMessageFromQSPR(string instanceIdentifier, MsgData msgData)
	{
		try
		{
			if (msgData.msgType == TestMsgType.OnUnitStart)
			{
				goNoGoResult = 0;
				qsprInstance.AppendToLocalLog(instanceIdentifier + " started");
				if (this.SendStatus != null)
				{
					this.SendStatus(instanceIdentifier + " started");
				}
				if (this.GoNoGoStart != null)
				{
					this.GoNoGoStart(msgData.time);
				}
				if (this.GoNoGoStartWithCount != null)
				{
					this.GoNoGoStartWithCount(msgData.time, (int)msgData.ParamResult);
				}
			}
			else if (msgData.msgType == TestMsgType.OnUnitEnd)
			{
				if (msgData.Result == TestResult.Exception || msgData.Result == TestResult.Aborted)
				{
					qsprInstance.AppendToLocalLog(instanceIdentifier + " bailed out");
					goNoGoResult = 0;
					if (this.SendStatus != null)
					{
						this.SendStatus(instanceIdentifier + " bailed out");
					}
				}
				if (msgData.Result == TestResult.Failed)
				{
					qsprInstance.AppendToLocalLog(instanceIdentifier + " failed");
					goNoGoResult = 0;
				}
				else if (msgData.Result == TestResult.Passed)
				{
					qsprInstance.AppendToLocalLog(instanceIdentifier + " passed");
					goNoGoResult = 1;
				}
			}
			else if (msgData.msgType == TestMsgType.OnTreeDone)
			{
				if (this.GoNoGoDone != null)
				{
					this.GoNoGoDone(goNoGoResult);
				}
			}
			else if (msgData.msgType == TestMsgType.TMSPluginInitialized)
			{
				if (qsprInstance.TestMessageReceived != null)
				{
					qsprInstance.TestMessageReceived(msgData);
				}
			}
			else if (msgData.msgType == TestMsgType.OnLoggingDone)
			{
				if (this.LoggingDone != null)
				{
					this.LoggingDone(msgData.TestFullPath);
				}
			}
			else if (msgData.msgType == TestMsgType.OnTestStart)
			{
				qsprInstance.AppendToLocalLog("Sending on test start message " + msgData.TestFullPath);
				if (sendAllTestStartMessages && this.TestStarted != null)
				{
					this.TestStarted(msgData.ErrorCode, msgData.TestFullPath);
				}
			}
			else if (msgData.msgType == TestMsgType.OnTestResult)
			{
				qsprInstance.AppendToLocalLog("Sending on test result message " + msgData.TestFullPath);
				if (msgData.Result == TestResult.Exception || msgData.Result == TestResult.Failed)
				{
					if (this.TestDone != null)
					{
						this.TestDone(msgData.TestFullPath, 0);
					}
				}
				else if (msgData.Result == TestResult.Passed && sendAllTestDoneMessages && this.TestDone != null)
				{
					this.TestDone(msgData.TestFullPath, 1);
				}
			}
			else if (msgData.msgType == TestMsgType.OnGoNoGoDidNotStart)
			{
				if (this.GoNoGoDidNotStart != null)
				{
					this.GoNoGoDidNotStart(msgData.ErrorCode);
				}
			}
			else if (msgData.msgType == TestMsgType.QsprHidden)
			{
				qsprInstance.HideWindow();
			}
			else if (msgData.msgType == TestMsgType.OnTreePause)
			{
				if (this.XTTPause != null)
				{
					this.XTTPause();
				}
			}
			else if (msgData.msgType == TestMsgType.OnTreeResume && this.XTTResume != null)
			{
				this.XTTResume();
			}
		}
		catch (Exception ex)
		{
			qsprInstance.AppendToLocalLog("Error processing message from QSPR " + ex.ToString() + ex.StackTrace);
			throw ex;
		}
	}

	internal void SetAutomationObject(QSPRInstance qsprinstance)
	{
		qsprInstance = qsprinstance;
		if (qsprinstance != null)
		{
			qsprinstance.processReceivedQSPRMessageDelegate = ProcessMessageFromQSPR;
		}
	}

	public int SetUUTId(int phIndex, string UUID)
	{
		if (UUID == null)
		{
			lastCallInfo = "SetUUTId called with null UUTId";
		}
		else
		{
			lastCallInfo = "SetUUTId(" + phIndex + ", " + UUID + ")";
		}
		if (!qsprInstance.SetUUTId(UUID, phIndex))
		{
			lastError = "Failed to Set UUTId";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		phoneIndex = phIndex;
		sUUID = UUID;
		return 1;
	}

	public int SetUserInput(string sCaption, string sLabel, string sUserInput)
	{
		if (sCaption == null || sLabel == null || sUserInput == null)
		{
			lastCallInfo = "SetUserInput was called with one or more null arguments";
		}
		else
		{
			lastCallInfo = "SetUserInput(" + sCaption + ", " + sLabel + ", " + sUserInput + ")";
		}
		if (!qsprInstance.SetUserInput(sCaption, sLabel, sUserInput))
		{
			lastError = "Failed to set user input";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		return 1;
	}

	public int SetNodeDescription(string nodeName, string description)
	{
		if (nodeName == null || description == null)
		{
			lastCallInfo = "SetNodeDescription called with one or more null arguments";
			lastError = "SetNodeDescription failed, one or more arguments were null";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		lastCallInfo = "SetNodeDescription(" + nodeName + ", " + description + ")";
		if (!qsprInstance.SetNodeDescription(nodeName, description))
		{
			lastError = "Failed to setup node description for node " + nodeName;
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		return 1;
	}

	public int SetNodeNotes(string nodeName, string nodeNotes)
	{
		if (nodeName == null || nodeNotes == null)
		{
			lastCallInfo = "SetNodeNotes called with one or more null arguments";
			lastError = "SetNodeNotes failed, one or more arguments were null";
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		lastCallInfo = "SetNodeNotes(" + nodeName + ", " + nodeNotes + ")";
		if (!qsprInstance.SetNodeNotes(nodeName, nodeNotes))
		{
			lastError = "Failed to setup node notes for node " + nodeName;
			qsprInstance.SetErrorsExist(1);
			return 0;
		}
		return 1;
	}

	public string GetNodeNotes(string nodeName)
	{
		if (nodeName == null)
		{
			lastCallInfo = "GetNodeNotes called with one or more null arguments";
			lastError = "GetNodeNotes failed, one or more arguments were null";
			qsprInstance.SetErrorsExist(1);
			return "";
		}
		lastCallInfo = "GetNodeNotes(" + nodeName + ")";
		return qsprInstance.GetNodeNotes(nodeName);
	}

	public string GetNodeDescription(string nodeName)
	{
		if (nodeName == null)
		{
			lastCallInfo = "GetNodeDescription called with one or more null arguments";
			lastError = "GetNodeDescription failed, one or more arguments were null";
			qsprInstance.SetErrorsExist(1);
			return "";
		}
		lastCallInfo = "GetNodeDescription(" + nodeName + ")";
		return qsprInstance.GetNodeDescription(nodeName);
	}
}
