using System.Collections.Generic;

namespace QC.MCI;

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

	private Dictionary<string, string> m_globalVariables;

	public static MCI_EnvManager GetInstance()
	{
		return m_instance;
	}

	public string GetGlobalVariable(string name)
	{
		string value = "";
		if (QTMDotNetInterfaceManager.GetInstance().isValid())
		{
			value = QTMDotNetInterfaceManager.GetInstance().GetGlobalVariable(name);
		}
		else
		{
			m_globalVariables.TryGetValue(name, out value);
		}
		return value;
	}

	public void SetGlobalVariable(string name, string value)
	{
		if (QTMDotNetInterfaceManager.GetInstance().isValid())
		{
			QTMDotNetInterfaceManager.GetInstance().SetGlobalVariable(name, value);
		}
		else if (!m_globalVariables.ContainsKey(name))
		{
			m_globalVariables.Add(name, value);
		}
		else
		{
			m_globalVariables[name] = value;
		}
	}

	public void DebugMessageWrite(string name, string text)
	{
		if (QTMDotNetInterfaceManager.GetInstance().isValid())
		{
			QTMDotNetInterfaceManager.GetInstance().DebugMessageWrite(name, text);
		}
	}

	public void DebugMessageTPLStartContext(string name, string testName, string text)
	{
		if (QTMDotNetInterfaceManager.GetInstance().isValid())
		{
			QTMDotNetInterfaceManager.GetInstance().DebugMessageTPLStartContext(name, testName, text);
		}
	}

	public void DebugMessageTPLStopContext(string name)
	{
		if (QTMDotNetInterfaceManager.GetInstance().isValid())
		{
			QTMDotNetInterfaceManager.GetInstance().DebugMessageTPLStopContext(name);
		}
	}

	public void DebugMessageTPLPrintMessage(string name, string filter, string msg)
	{
		if (QTMDotNetInterfaceManager.GetInstance().isValid())
		{
			QTMDotNetInterfaceManager.GetInstance().DebugMessageTPLPrintMessage(name, filter, msg);
		}
	}

	public string QTMInfoGetTestName()
	{
		if (QTMDotNetInterfaceManager.GetInstance().isValid())
		{
			return QTMDotNetInterfaceManager.GetInstance().QTMInfoCurrentTestName();
		}
		return "Test Name not available when QTM is inactive";
	}

	private MCI_EnvManager()
	{
		m_globalVariables = new Dictionary<string, string>();
	}
}
