using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace QC.MultiplexManager;

public class QTMDotNetInterfaceManager
{
	private static QTMDotNetInterfaceManager m_instance = new QTMDotNetInterfaceManager();

	private object m_gvClass;

	private MethodInfo setGlobalVariableFnc;

	private MethodInfo getGlobalVariableFnc;

	private object m_dmClass;

	private MethodInfo debugMessageWriteFnc;

	private MethodInfo debugMessageTPLStartContextFnc;

	private MethodInfo debugMessageTPLStopContextFnc;

	private object m_qtmInfoClass;

	private PropertyInfo qtmInfoCurrentTestName;

	private bool m_isValid;

	public static QTMDotNetInterfaceManager GetInstance()
	{
		return m_instance;
	}

	public void SetGlobalVariable(string name, string value)
	{
		object[] parameters = new object[2] { name, value };
		setGlobalVariableFnc.Invoke(m_gvClass, parameters);
	}

	public string GetGlobalVariable(string name)
	{
		object[] array = new object[2] { name, "" };
		getGlobalVariableFnc.Invoke(m_gvClass, array);
		return (string)array[1];
	}

	public void DebugMessageWrite(string name, string text)
	{
		object[] parameters = new object[2] { name, text };
		debugMessageWriteFnc.Invoke(m_dmClass, parameters);
	}

	public void DebugMessageTPLStartContext(string name, string testName, string text)
	{
		object[] parameters = new object[3] { name, testName, text };
		debugMessageTPLStartContextFnc.Invoke(m_dmClass, parameters);
	}

	public void DebugMessageTPLStopContext(string name)
	{
		object[] parameters = new object[1] { name };
		debugMessageTPLStopContextFnc.Invoke(m_dmClass, parameters);
	}

	public string QTMInfoCurrentTestName()
	{
		return qtmInfoCurrentTestName.GetValue(m_qtmInfoClass, null).ToString();
	}

	public bool isValid()
	{
		return m_isValid;
	}

	private QTMDotNetInterfaceManager()
	{
		string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
		if (!File.Exists(Path.Combine(directoryName, "BYPASS_QTM_DOT_NET_INTERFACE.TXT")))
		{
			try
			{
				string assemblyFile = Path.Combine(directoryName, "QTMDotNetKernelInterface.dll");
				Assembly assembly = Assembly.LoadFrom(assemblyFile);
				Type type = assembly.GetType("QC.QTMDotNetKernelInterface.GlobalVariable");
				m_gvClass = Activator.CreateInstance(type);
				MethodInfo[] methods = type.GetMethods();
				MethodInfo[] array = methods;
				foreach (MethodInfo methodInfo in array)
				{
					if (methodInfo.Name == "SetGlobalVariable" && methodInfo.GetParameters().Length == 2)
					{
						setGlobalVariableFnc = methodInfo;
					}
					if (methodInfo.Name == "GetGlobalVariable" && methodInfo.GetParameters().Length == 2)
					{
						getGlobalVariableFnc = methodInfo;
					}
				}
				if (setGlobalVariableFnc == null || getGlobalVariableFnc == null)
				{
					MessageBox.Show("setGlobalVariableFnc or getGlobalVariableFnc is null", "QC.MultiplexManager.DLL", MessageBoxButtons.OK, MessageBoxIcon.Hand);
					m_isValid = false;
				}
				else
				{
					Type type2 = assembly.GetType("QC.QTMDotNetKernelInterface.DebugMessage");
					m_dmClass = Activator.CreateInstance(type2);
					debugMessageWriteFnc = type2.GetMethod("Write", new Type[2]
					{
						typeof(string),
						typeof(string)
					});
					debugMessageTPLStartContextFnc = type2.GetMethod("TPLStartContext");
					debugMessageTPLStopContextFnc = type2.GetMethod("TPLStopContext");
					if (debugMessageWriteFnc == null || debugMessageTPLStartContextFnc == null || debugMessageTPLStopContextFnc == null)
					{
						MessageBox.Show("Failed to get User Interface (Debug/TPL) from QSPR", "QC.MultiplexManager.DLL", MessageBoxButtons.OK, MessageBoxIcon.Hand);
						m_isValid = false;
					}
					else
					{
						Type type3 = assembly.GetType("QC.QTMDotNetKernelInterface.QTMInfo");
						m_qtmInfoClass = Activator.CreateInstance(type3, nonPublic: true);
						qtmInfoCurrentTestName = type3.GetProperty("CurrentTestName");
						if (qtmInfoCurrentTestName == null)
						{
							MessageBox.Show("Failed to get property, CurrentTestName, from QSPR", "QC.MultiplexManager.DLL", MessageBoxButtons.OK, MessageBoxIcon.Hand);
							m_isValid = false;
						}
						else
						{
							m_isValid = true;
						}
					}
				}
				return;
			}
			catch
			{
				m_isValid = false;
				Console.WriteLine("QTMDotNetKernelInterface not foud. Use local Env.");
				return;
			}
		}
		m_isValid = false;
	}
}
