using System;
using System.Collections;
using System.Collections.Generic;

namespace QC.QTMInterface;

public class GlobalInformation
{
	private static bool automationMode = false;

	private static RunTimeConfigInfo runTimeConfig = default(RunTimeConfigInfo);

	private static string defaultKey = null;

	private static Dictionary<string, Hashtable> gvById = new Dictionary<string, Hashtable>();

	private static Dictionary<string, bool> testRunningById = new Dictionary<string, bool>();

	public static bool TestRunning
	{
		get
		{
			return testRunningById[defaultKey];
		}
		set
		{
			testRunningById[defaultKey] = value;
		}
	}

	public static Dictionary<string, bool> TestRunningById
	{
		get
		{
			return testRunningById;
		}
		set
		{
			testRunningById = value;
		}
	}

	public static RunTimeConfigInfo RunTimeConfig
	{
		get
		{
			return runTimeConfig;
		}
		set
		{
			runTimeConfig = value;
		}
	}

	public static bool AutomationMode => automationMode;

	private GlobalInformation()
	{
	}

	private static Hashtable GetGlobals(string id)
	{
		if (id == null)
		{
			return null;
		}
		if (!gvById.ContainsKey(id))
		{
			gvById.Add(id, new Hashtable());
		}
		return gvById[id];
	}

	private static bool GetTestRunningFromId(string id)
	{
		if (id == null)
		{
			return false;
		}
		if (!testRunningById.ContainsKey(id))
		{
			testRunningById.Add(id, value: false);
		}
		return testRunningById[id];
	}

	public static void SetDefaultKey(string key)
	{
		if (defaultKey == null)
		{
			if (key == null)
			{
				throw new NullReferenceException("Cannot set the default key to null");
			}
			defaultKey = key;
		}
	}

	public static bool SetGlobalVariable(string vName, object vValue)
	{
		try
		{
			return SetGlobalVariable(vName, vValue, defaultKey);
		}
		catch
		{
			return false;
		}
	}

	public static bool SetGlobalVariable(string vName, object vValue, string id)
	{
		try
		{
			Hashtable globals = GetGlobals(id);
			if (globals == null)
			{
				return false;
			}
			if (globals.ContainsKey(vName.Trim()))
			{
				globals[vName.Trim()] = vValue;
			}
			else
			{
				globals.Add(vName.Trim(), vValue);
			}
			return true;
		}
		catch
		{
			return false;
		}
	}

	public static object GetGlobalVariable(string vName, string id)
	{
		try
		{
			Hashtable globals = GetGlobals(id);
			if (globals == null)
			{
				return null;
			}
			if (globals.ContainsKey(vName.Trim()))
			{
				return globals[vName.Trim()];
			}
		}
		catch
		{
		}
		return null;
	}

	public static object GetGlobalVariable(string vName)
	{
		try
		{
			return GetGlobalVariable(vName, defaultKey);
		}
		catch
		{
		}
		return null;
	}

	public static void SetAutomationModeFlag()
	{
		automationMode = true;
	}
}
