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

namespace QC.QTMDotNetKernelInterface.Environment;

public class GlobalVariable : MarshalByRefObject
{
	private object lockObject = new object();

	internal string CurrentSN;

	internal int CurrentIndex;

	internal Hashtable AllSerialNos = new Hashtable();

	internal bool resetSubError;

	internal Hashtable gVariables = new Hashtable();

	internal Hashtable gVariablesForSn = new Hashtable();

	internal Hashtable gVariableObject = new Hashtable();

	private QSPREnvironment env;

	public Hashtable GVariables
	{
		get
		{
			return gVariables;
		}
		set
		{
			gVariables = value;
			OnGVChange(new GlobalVariableEventArgs());
		}
	}

	public ArrayList SortedGVNames
	{
		get
		{
			ArrayList arrayList = new ArrayList();
			try
			{
				ArrayList arrayList2 = new ArrayList();
				foreach (string key2 in gVariables.Keys)
				{
					arrayList2.Add(key2);
				}
				arrayList2.Sort();
				foreach (string item in arrayList2)
				{
					arrayList.Add(((GVData)gVariables[item]).Name);
				}
			}
			catch
			{
			}
			return arrayList;
		}
	}

	public event GlobalVariablesChangedDelegate OnGlobalVariableChange;

	public event TesterConfigChangedDelegate OnTesterConfigChange;

	public void valueSetup(Hashtable GVariables, Hashtable GVariablesForSn, Hashtable GVariableObject)
	{
		gVariables = GVariables;
		gVariablesForSn = GVariablesForSn;
		gVariableObject = GVariableObject;
	}

	public void Clear()
	{
		gVariables = new Hashtable();
	}

	internal GlobalVariable(QSPREnvironment env)
	{
		this.env = env;
	}

	public bool SetGlobalVariableObject(string gvName, object gvValue)
	{
		gVariableObject[gvName.Trim()] = gvValue;
		return true;
	}

	public bool GetGlobalVariableObject(string gvName, out object gvValue)
	{
		gvValue = null;
		try
		{
			if (gVariableObject.Contains(gvName.Trim()))
			{
				gvValue = gVariableObject[gvName.Trim()];
				return true;
			}
		}
		catch (Exception)
		{
		}
		return false;
	}

	public bool SetTesterConfigValue(string name, string value, bool addToSessionData)
	{
		TesterConfigurationInfo testerConfigurationInfo = new TesterConfigurationInfo();
		testerConfigurationInfo.Name = name;
		testerConfigurationInfo.Value = value;
		testerConfigurationInfo.AddToSessionData = addToSessionData;
		testerConfigurationInfo.IncludeInTestResults = addToSessionData;
		return SetTesterConfigValue(testerConfigurationInfo);
	}

	public bool SetTesterConfigValue(TesterConfigurationInfo configValue)
	{
		return SetTesterConfigValues(new List<TesterConfigurationInfo> { configValue });
	}

	public bool SetTesterConfigValues(List<TesterConfigurationInfo> configValues)
	{
		if (configValues != null && configValues.Count > 0)
		{
			TesterConfigEventArgs testerConfigEventArgs = new TesterConfigEventArgs();
			testerConfigEventArgs.ChangedItems = configValues;
			OnConfigChange(testerConfigEventArgs);
			return true;
		}
		return false;
	}

	public bool SetGlobalVariable(string gvName, string gvValue)
	{
		try
		{
			GVData gVData = default(GVData);
			gVData.Name = gvName.Trim();
			gVData.Value = gvValue;
			if (gvName.ToUpper() == "SUBERRORCODE")
			{
				GetGlobalVariable("SUBERRORCODE", out var gvValue2);
				if (IsSubErrorSet(gvValue2) && !resetSubError)
				{
					return false;
				}
			}
			if (gvName.ToUpper() == "SUBERRORSTRING")
			{
				GetGlobalVariable("SUBERRORSTRING", out var gvValue3);
				if (IsSubErrorStrSet(gvValue3) && !resetSubError)
				{
					return false;
				}
			}
			gVariables[gvName.Trim().ToUpper()] = gVData;
			GlobalInformation.SetGlobalVariable(gvName, gvValue, env.SchedulerID);
			switch (gvName)
			{
			default:
				OnGVChange(new GlobalVariableEventArgs(gvName, gvValue));
				if (gvName.Trim() != "SN" && gvName.Trim() != "CURRENT_UNIT_INDEX")
				{
					if (CurrentSN != null && CurrentSN.Length > 0)
					{
						if (!gVariablesForSn.ContainsKey(CurrentSN))
						{
							gVariablesForSn[CurrentSN] = new Hashtable();
						}
						Hashtable hashtable = (Hashtable)gVariablesForSn[CurrentSN];
						hashtable[gvName.Trim().ToUpper()] = gVData;
					}
				}
				else if (gvName.Trim() == "SN")
				{
					CurrentSN = gvValue.TrimEnd();
					ParseSerialNumber(CurrentSN, out AllSerialNos);
				}
				else
				{
					int.TryParse(gvValue, out CurrentIndex);
					if (AllSerialNos.ContainsKey(CurrentIndex))
					{
						CurrentSN = (string)AllSerialNos[CurrentIndex];
					}
				}
				break;
			case "QSPR_ABORT":
			case "QSPR_STOP":
			case "QTM_ABORT":
			case "QTM_STOP":
			case "DATALOGGING_XMLFILE":
			case "DATALOGGING_CSVFILE":
				break;
			}
			return true;
		}
		catch (Exception)
		{
			return false;
		}
	}

	public bool GetGlobalVariable(string gvName, out string gvValue)
	{
		gvValue = "";
		try
		{
			if (gVariables.Contains(gvName.ToUpper().Trim()))
			{
				gvValue = ((GVData)gVariables[gvName.ToUpper().Trim()]).Value;
				return true;
			}
		}
		catch
		{
		}
		return false;
	}

	public bool GetGlobalVariableForSN(string SN, string gvName, out string gvValue)
	{
		gvValue = "";
		try
		{
			if (SN != null && CurrentSN.Length > 0)
			{
				if (!gVariablesForSn.ContainsKey(SN))
				{
					gVariablesForSn[SN] = new Hashtable();
				}
				Hashtable hashtable = (Hashtable)gVariablesForSn[SN];
				string key = gvName.Trim().ToUpper();
				if (!hashtable.ContainsKey(key))
				{
					return false;
				}
				gvValue = ((GVData)hashtable[key]).Value;
			}
		}
		catch
		{
		}
		return false;
	}

	public bool Contains(string gvName)
	{
		try
		{
			if (gVariables.Contains(gvName.ToUpper().Trim()))
			{
				return true;
			}
		}
		catch
		{
		}
		return false;
	}

	internal void OnGVChange(GlobalVariableEventArgs e)
	{
		try
		{
			if (this.OnGlobalVariableChange != null)
			{
				this.OnGlobalVariableChange(null, e);
			}
		}
		catch
		{
		}
	}

	internal void OnConfigChange(TesterConfigEventArgs e)
	{
		if (this.OnTesterConfigChange != null)
		{
			this.OnTesterConfigChange(null, e);
		}
	}

	internal bool ParseSerialNumber(string strSerialNumber, out Hashtable hashUUT_SN)
	{
		hashUUT_SN = new Hashtable();
		try
		{
			if (strSerialNumber.Contains("%^&"))
			{
				string[] array = strSerialNumber.Split(new string[1] { "%^&" }, StringSplitOptions.RemoveEmptyEntries);
				for (int i = 1; i <= array.Length; i++)
				{
					hashUUT_SN.Add(i, array[i - 1]);
				}
			}
			else
			{
				hashUUT_SN.Add(1, strSerialNumber);
			}
			return true;
		}
		catch
		{
		}
		return false;
	}

	public void ResetErrorCode()
	{
		SetGlobalVariable("ERRORCODE", "");
		resetSubError = true;
		SetGlobalVariable("SUBERRORCODE", "");
		SetGlobalVariable("SUBERRORSTRING", "");
		resetSubError = false;
	}

	public bool SetErrorCode(string errorCode)
	{
		GetGlobalVariable("ERRORCODE", out var gvValue);
		if (gvValue.ToString().Length == 0)
		{
			SetGlobalVariable("ERRORCODE", errorCode);
			return true;
		}
		return false;
	}

	internal bool IsSubErrorSet(string errorCode)
	{
		int result = 0;
		if (string.IsNullOrWhiteSpace(errorCode))
		{
			return false;
		}
		if (int.TryParse(errorCode, out result) && result == 0)
		{
			return false;
		}
		return true;
	}

	internal bool IsSubErrorStrSet(string errorString)
	{
		if (string.IsNullOrWhiteSpace(errorString))
		{
			return false;
		}
		if (errorString == "SYSTEM__COMMON__NO_ERROR")
		{
			return false;
		}
		return true;
	}

	public string GetErrorCode()
	{
		GetGlobalVariable("ERRORCODE", out var gvValue);
		return gvValue.ToString();
	}

	public bool RegisterTechnology(char Tech)
	{
		if (Tech == '\0')
		{
			return false;
		}
		GetGlobalVariable("TECHNOLOGIES", out var gvValue);
		if (string.IsNullOrWhiteSpace(gvValue))
		{
			gvValue = "";
		}
		if (!gvValue.Contains(Tech.ToString()))
		{
			SetGlobalVariable("TECHNOLOGIES", gvValue + Tech);
		}
		return true;
	}

	public string GetTechnologies()
	{
		GetGlobalVariable("TECHNOLOGIES", out var gvValue);
		return gvValue;
	}

	public bool SetDutIndex(uint IndexValue)
	{
		if (IndexValue < 10)
		{
			SetGlobalVariable("DUT_INDEX", "0" + IndexValue);
		}
		else
		{
			SetGlobalVariable("DUT_INDEX", IndexValue.ToString());
		}
		return true;
	}

	public void ResetTechnologies()
	{
		SetGlobalVariable("TECHNOLOGIES", "");
	}

	public void ResetDutIndex()
	{
		SetGlobalVariable("DUT_INDEX", "");
	}
}
