using System.Collections.Generic;
using QC.QTMDotNetKernelInterface.Environment;
using QC.QTMInterface;

namespace QC.QTMDotNetKernelInterface;

internal class StaticMapper
{
	private static string defaultKey = null;

	private static Dictionary<string, QC.QTMDotNetKernelInterface.Environment.DebugMessage> DebugMessageMap = new Dictionary<string, QC.QTMDotNetKernelInterface.Environment.DebugMessage>();

	private static Dictionary<string, QC.QTMDotNetKernelInterface.Environment.GlobalVariable> GlobalVariableMap = new Dictionary<string, QC.QTMDotNetKernelInterface.Environment.GlobalVariable>();

	private static Dictionary<string, QC.QTMDotNetKernelInterface.Environment.QTMInfo> QTMInfoMap = new Dictionary<string, QC.QTMDotNetKernelInterface.Environment.QTMInfo>();

	internal static void SetupStaticEnv(QSPREnvironment qsprEnv)
	{
		if (defaultKey == null)
		{
			defaultKey = qsprEnv.SchedulerID;
			GlobalInformation.SetDefaultKey(defaultKey);
			AddDebugMessageObject(defaultKey, qsprEnv.DebugMessage);
			DebugMessage.SetupEvents();
			AddGlobalVariableObject(defaultKey, qsprEnv.GlobalVariable);
			GlobalVariable.SetupGvEvents();
			AddQTMInfoObject(defaultKey, qsprEnv.QTMInfo);
			QTMInfo.SetupEvents();
		}
		else
		{
			AddDebugMessageObject(qsprEnv.SchedulerID, qsprEnv.DebugMessage);
			AddGlobalVariableObject(qsprEnv.SchedulerID, qsprEnv.GlobalVariable);
			AddQTMInfoObject(qsprEnv.SchedulerID, qsprEnv.QTMInfo);
		}
	}

	private static void AddDebugMessageObject(string key, QC.QTMDotNetKernelInterface.Environment.DebugMessage debugMsgObj)
	{
		if (!DebugMessageMap.ContainsKey(key))
		{
			DebugMessageMap.Add(key, debugMsgObj);
		}
	}

	internal static QC.QTMDotNetKernelInterface.Environment.DebugMessage GetDebugMessageObject(string key = null)
	{
		QC.QTMDotNetKernelInterface.Environment.DebugMessage result = null;
		if (key == null)
		{
			key = defaultKey;
		}
		if (DebugMessageMap.ContainsKey(key))
		{
			result = DebugMessageMap[key];
		}
		return result;
	}

	private static void AddGlobalVariableObject(string key, QC.QTMDotNetKernelInterface.Environment.GlobalVariable gvObj)
	{
		if (!GlobalVariableMap.ContainsKey(key))
		{
			GlobalVariableMap.Add(key, gvObj);
		}
	}

	internal static QC.QTMDotNetKernelInterface.Environment.GlobalVariable GetGlobalVariableObject(string key = null)
	{
		QC.QTMDotNetKernelInterface.Environment.GlobalVariable result = null;
		if (key == null)
		{
			key = defaultKey;
		}
		if (GlobalVariableMap.ContainsKey(key))
		{
			result = GlobalVariableMap[key];
		}
		return result;
	}

	private static void AddQTMInfoObject(string key, QC.QTMDotNetKernelInterface.Environment.QTMInfo qtmInfoObj)
	{
		if (!QTMInfoMap.ContainsKey(key))
		{
			QTMInfoMap.Add(key, qtmInfoObj);
		}
	}

	internal static QC.QTMDotNetKernelInterface.Environment.QTMInfo GetQTMInfoObject(string key = null)
	{
		QC.QTMDotNetKernelInterface.Environment.QTMInfo result = null;
		if (key == null)
		{
			key = defaultKey;
		}
		if (QTMInfoMap.ContainsKey(key))
		{
			result = QTMInfoMap[key];
		}
		return result;
	}
}
