using System;
using System.Collections;
using QC.QTMDotNetKernelInterface;

namespace QC.QTMToolKit.Tools;

[QTMTestClass]
public class SerialNumber
{
	private static string UUT_SN_SEP = "%^&";

	[QTMTest(CanAddToTestPlan = true)]
	public static bool ParseSerialNumber(out Hashtable hashUUT_SN)
	{
		hashUUT_SN = new Hashtable();
		try
		{
			string gvValue = "";
			if (GlobalVariable.GetGlobalVariable("SN", out gvValue))
			{
				if (gvValue.Contains(UUT_SN_SEP))
				{
					string[] array = gvValue.Split(new string[1] { UUT_SN_SEP }, StringSplitOptions.RemoveEmptyEntries);
					for (int i = 1; i <= array.Length; i++)
					{
						hashUUT_SN.Add(i, array[i - 1]);
					}
				}
				else
				{
					hashUUT_SN.Add(1, gvValue);
				}
				return true;
			}
		}
		catch
		{
		}
		return false;
	}

	[QTMTest(CanAddToTestPlan = true)]
	public static bool IsCurrentSerialNumberValid(out bool IsValid, out string ErrorString)
	{
		ErrorString = "";
		IsValid = false;
		try
		{
			GlobalVariable.GetGlobalVariable("CURRENT_UNIT_INDEX", out var gvValue);
			if (gvValue == "")
			{
				gvValue = "1";
				GlobalVariable.SetGlobalVariable("CURRENT_UNIT_INDEX", "1");
			}
			int num = int.Parse(gvValue);
			if (GlobalVariable.GetGlobalVariable("SN", out var gvValue2))
			{
				ParseSerialNumber(out var hashUUT_SN);
				if (!hashUUT_SN.ContainsKey(num))
				{
					IsValid = false;
					ErrorString = "Index not found";
					return false;
				}
				gvValue2 = (string)hashUUT_SN[num];
				if (!(gvValue2.Trim().ToUpper() == "NA") && !(gvValue2.Trim() == ""))
				{
					IsValid = true;
				}
				else
				{
					IsValid = false;
					ErrorString = "Serial number is : NA";
				}
				return true;
			}
			return false;
		}
		catch (Exception ex)
		{
			ErrorString = ex.Message;
			return false;
		}
	}

	[QTMTest(CanAddToTestPlan = true)]
	public static bool SetCurrentSerialNumber(string SerialNumber)
	{
		try
		{
			GlobalVariable.GetGlobalVariable("CURRENT_UNIT_INDEX", out var gvValue);
			if (gvValue == "")
			{
				gvValue = "1";
				GlobalVariable.SetGlobalVariable("CURRENT_UNIT_INDEX", "1");
			}
			int index = int.Parse(gvValue);
			return SetSerialNumberAtIndex(index, SerialNumber);
		}
		catch
		{
			return false;
		}
	}

	[QTMTest(CanAddToTestPlan = true)]
	public static bool SetSerialNumberAtIndex(int Index, string SerialNumber)
	{
		try
		{
			if (Index > 0)
			{
				ParseSerialNumber(out var hashUUT_SN);
				if (!hashUUT_SN.ContainsKey(Index))
				{
					return false;
				}
				hashUUT_SN[Index] = SerialNumber;
				string text = "";
				for (int i = 1; i <= hashUUT_SN.Count; i++)
				{
					text = ((i != 1) ? (text + UUT_SN_SEP + (string)hashUUT_SN[i]) : ((string)hashUUT_SN[i]));
				}
				GlobalVariable.SetGlobalVariable("SN", text);
				return true;
			}
			if (Index == 0)
			{
				GlobalVariable.SetGlobalVariable("SN", SerialNumber);
				return true;
			}
			return false;
		}
		catch
		{
			return false;
		}
	}

	[QTMTest(CanAddToTestPlan = true)]
	public static bool SetCurrentUnitIndex(int Index)
	{
		try
		{
			GlobalVariable.SetGlobalVariable("CURRENT_UNIT_INDEX", Index.ToString());
			return true;
		}
		catch
		{
		}
		return false;
	}

	[QTMTest(CanAddToTestPlan = true)]
	public static bool SetCurrentUnitIndexToAllUnits()
	{
		try
		{
			GlobalVariable.SetGlobalVariable("CURRENT_UNIT_INDEX", "0");
			return true;
		}
		catch
		{
		}
		return false;
	}

	[QTMTest(CanAddToTestPlan = true)]
	public static bool GetCurrentSerialNumber(out string SerialNumber)
	{
		SerialNumber = "";
		try
		{
			GlobalVariable.GetGlobalVariable("CURRENT_UNIT_INDEX", out var gvValue);
			if (gvValue == "")
			{
				gvValue = "1";
				GlobalVariable.SetGlobalVariable("CURRENT_UNIT_INDEX", "1");
			}
			int num = int.Parse(gvValue);
			if (num > 0)
			{
				ParseSerialNumber(out var hashUUT_SN);
				if (!hashUUT_SN.ContainsKey(num))
				{
					return false;
				}
				SerialNumber = (string)hashUUT_SN[num];
				if (SerialNumber.Trim().ToUpper() == "NA" || SerialNumber.Trim() == "")
				{
					return false;
				}
				return true;
			}
			if (num == 0)
			{
				GlobalVariable.GetGlobalVariable("SN", out SerialNumber);
				if (SerialNumber.Trim().ToUpper() == "NA" || SerialNumber.Trim() == "")
				{
					return false;
				}
				return true;
			}
			return false;
		}
		catch
		{
			return false;
		}
	}

	[QTMTest(CanAddToTestPlan = true)]
	public static bool GetCurrentSerialNumberCustom(bool ReturnFalseIfNotValid, out int Index, out string SerialNumber, out bool IsValid, out string ErrorMessage)
	{
		SerialNumber = "";
		Index = 0;
		IsValid = false;
		ErrorMessage = "";
		try
		{
			GlobalVariable.GetGlobalVariable("CURRENT_UNIT_INDEX", out var gvValue);
			if (gvValue == "")
			{
				gvValue = "1";
				GlobalVariable.SetGlobalVariable("CURRENT_UNIT_INDEX", "1");
			}
			Index = int.Parse(gvValue);
			GlobalVariable.GetGlobalVariable("SN", out SerialNumber);
			if (Index > 0)
			{
				ParseSerialNumber(out var hashUUT_SN);
				if (!hashUUT_SN.ContainsKey(Index))
				{
					SerialNumber = "";
					ErrorMessage = "Index not found";
					IsValid = false;
					return false;
				}
				SerialNumber = (string)hashUUT_SN[Index];
				if (SerialNumber.Trim().ToUpper() == "NA" || SerialNumber.Trim() == "")
				{
					ErrorMessage = "Serial number is : NA";
					IsValid = false;
					if (ReturnFalseIfNotValid)
					{
						return false;
					}
					return true;
				}
				IsValid = true;
				return true;
			}
			if (Index == 0)
			{
				if (SerialNumber.Trim().ToUpper() == "NA" || SerialNumber.Trim() == "")
				{
					ErrorMessage = "Serial number is : NA";
					IsValid = false;
					if (ReturnFalseIfNotValid)
					{
						return false;
					}
					return true;
				}
				IsValid = true;
				return true;
			}
			ErrorMessage = "Index cannot be less than ZERO";
			IsValid = false;
			return false;
		}
		catch (Exception ex)
		{
			ErrorMessage = ex.Message;
			IsValid = false;
			SerialNumber = "";
			return false;
		}
	}

	[QTMTest(CanAddToTestPlan = true)]
	public static bool GetSerialNumberAtIndex(int Index, out string SerialNumber)
	{
		SerialNumber = "";
		try
		{
			if (Index > 0)
			{
				ParseSerialNumber(out var hashUUT_SN);
				if (hashUUT_SN.ContainsKey(Index))
				{
					SerialNumber = (string)hashUUT_SN[Index];
					if (!(SerialNumber.Trim().ToUpper() == "NA") && !(SerialNumber.Trim() == ""))
					{
						return true;
					}
				}
			}
			else if (Index == 0)
			{
				GlobalVariable.GetGlobalVariable("SN", out SerialNumber);
				if (!(SerialNumber.Trim().ToUpper() == "NA") && !(SerialNumber.Trim() == ""))
				{
					return true;
				}
			}
		}
		catch
		{
		}
		return false;
	}

	[QTMTest(CanAddToTestPlan = true)]
	public static bool GetSerialNumberAtIndexCustom(int Index, bool ReturnFalseIfNotValid, out string SerialNumber, out bool IsValid, out string ErrorMessage)
	{
		SerialNumber = "";
		IsValid = false;
		ErrorMessage = "";
		try
		{
			if (GlobalVariable.GetGlobalVariable("SN", out var _))
			{
				if (Index > 0)
				{
					ParseSerialNumber(out var hashUUT_SN);
					if (hashUUT_SN.ContainsKey(Index))
					{
						SerialNumber = (string)hashUUT_SN[Index];
						if (SerialNumber.Trim().ToUpper() == "NA" || SerialNumber.Trim() == "")
						{
							IsValid = false;
							ErrorMessage = "Serial number is : NA";
							if (ReturnFalseIfNotValid)
							{
								return false;
							}
							return true;
						}
						IsValid = true;
						return true;
					}
					IsValid = false;
					ErrorMessage = "Index not found";
				}
				else
				{
					if (Index == 0)
					{
						if (SerialNumber.Trim().ToUpper() == "NA" || SerialNumber.Trim() == "")
						{
							IsValid = false;
							ErrorMessage = "Serial number is : NA";
							if (ReturnFalseIfNotValid)
							{
								return false;
							}
							return true;
						}
						IsValid = true;
						return true;
					}
					IsValid = false;
					ErrorMessage = "Index cannot be less than ZERO";
					SerialNumber = "";
				}
			}
			else
			{
				IsValid = false;
				ErrorMessage = "Serial Number not found";
				SerialNumber = "";
			}
		}
		catch
		{
		}
		return false;
	}

	[QTMTest(CanAddToTestPlan = true)]
	public static bool SelectNextValidUnit(out bool ValidUnitSelected)
	{
		ValidUnitSelected = false;
		if (GetCurrentUnitIndex(out var Index))
		{
			bool IsValid = false;
			ValidUnitSelected = true;
			while (!IsValid)
			{
				Index++;
				SetCurrentUnitIndex(Index);
				if (!IsCurrentSerialNumberValid(out IsValid, out var _))
				{
					ValidUnitSelected = false;
					break;
				}
			}
		}
		return true;
	}

	[QTMTest(CanAddToTestPlan = true)]
	public static bool GetCurrentUnitIndex(out int Index)
	{
		Index = -1;
		try
		{
			GlobalVariable.GetGlobalVariable("CURRENT_UNIT_INDEX", out var gvValue);
			if (gvValue == "")
			{
				gvValue = "1";
				GlobalVariable.SetGlobalVariable("CURRENT_UNIT_INDEX", "1");
			}
			Index = int.Parse(gvValue);
			return true;
		}
		catch (Exception)
		{
			return false;
		}
	}
}
