using System;
using System.Collections;
using System.Data;
using System.Globalization;
using System.Windows.Forms;
using QC.QTMDotNetKernel.Registry;

namespace QC.QTMDotNetKernel.Handlers;

public class ParameterHandler
{
	private enum SearchState
	{
		state1,
		state2,
		state3,
		state4,
		state5,
		state6,
		state7,
		state8
	}

	internal const string INPUT = "Input";

	internal const string OUTPUT = "Output";

	internal const string DOUBLE = "Double";

	internal const string SINGLE = "Single";

	internal const string DECIMAL = "Decimal";

	internal const string STRING = "String";

	internal const string CHAR = "Char";

	internal const string BYTE = "Byte";

	internal const string SBYTE = "SByte";

	internal const string BOOLEAN = "Boolean";

	internal const string VIRTUAL_PARAM_SUFFIX = "REF";

	internal const string INTEGER = "Int";

	internal const string UINT32 = "UInt32";

	internal const string INT16 = "Int16";

	internal const string UINT16 = "UInt16";

	internal const string UINT64 = "UInt64";

	internal const string INT64 = "Int64";

	internal const string DATATABLE = "DataTable";

	internal const string OBJECT = "Object";

	internal const string NULLABLE = "Nullable`1";

	private const string STRING_ARRAY_SEPARATOR = "_\"_";

	private ParameterHandler()
	{
	}

	public static Hashtable CreateParameters(object sParams)
	{
		Hashtable hashtable = new Hashtable();
		try
		{
			object[,] array = (object[,])sParams;
			if (array != null)
			{
				for (int i = 0; i < array.GetLength(0); i++)
				{
					try
					{
						TestParameter testParameter = new TestParameter();
						testParameter.Name = array[i, 0].ToString();
						testParameter.Value = array[i, 1].ToString();
						testParameter.LowerLimit = array[i, 2].ToString();
						testParameter.UpperLimit = array[i, 3].ToString();
						testParameter.NumberOfElements = int.Parse(array[i, 4].ToString());
						testParameter.IOType = array[i, 5].ToString();
						testParameter.UnitOfMeasure = array[i, 6].ToString();
						testParameter.Description = array[i, 7].ToString();
						testParameter.DataType = array[i, 8].ToString();
						if (testParameter.IsDataTable)
						{
							testParameter.DataTableValue = (DataTable)array[i, 1];
						}
						if ((bool)array[i, 9])
						{
							testParameter.CheckedState = CheckState.Checked;
						}
						try
						{
							if (array[i, 10] != null && !string.IsNullOrEmpty(array[i, 10].ToString()) && string.Compare(array[i, 10].ToString(), array[i, 0].ToString(), ignoreCase: true) != 0)
							{
								testParameter.Name = array[i, 10].ToString();
								testParameter.DisplayName = array[i, 0].ToString();
							}
						}
						catch
						{
						}
						hashtable.Add(testParameter.Name, testParameter);
					}
					catch
					{
					}
				}
			}
		}
		catch
		{
		}
		return hashtable;
	}

	public static object GetParameterObject(Hashtable parameters)
	{
		try
		{
			object[,] array = new object[19, parameters.Count];
			int num = 0;
			foreach (TestParameter value in parameters.Values)
			{
				array[0, num] = (string.IsNullOrEmpty(value.DisplayName) ? value.Name : value.DisplayName);
				array[1, num] = value.DataType;
				array[2, num] = value.NumberOfElements;
				if (value.IsDataTable)
				{
					array[3, num] = value.DataTableValue;
				}
				else
				{
					array[3, num] = value.Value;
				}
				array[4, num] = value.IOType;
				array[5, num] = value.UpperLimit;
				array[6, num] = value.LowerLimit;
				array[7, num] = value.UnitOfMeasure;
				array[8, num] = value.Description;
				array[9, num] = value.Constraints;
				array[10, num] = value.DisplayHint;
				array[11, num] = true;
				if (value.CheckedState == CheckState.Checked)
				{
					array[12, num] = true;
				}
				else
				{
					array[12, num] = false;
				}
				array[13, num] = value.IsNullable;
				array[14, num] = value.ParentName;
				array[15, num] = value.IsArray && value.NumberOfElements == -1;
				array[16, num] = value.IsEditableInTestPlan;
				array[17, num] = value.CanEditResultValue;
				array[18, num] = value.Name;
				num++;
			}
			return array;
		}
		catch
		{
			return null;
		}
	}

	public static object GetParameterObject(ArrayList parameters)
	{
		try
		{
			object[,] array = new object[19, parameters.Count];
			int num = 0;
			foreach (TestParameter parameter in parameters)
			{
				array[0, num] = (string.IsNullOrEmpty(parameter.DisplayName) ? parameter.Name : parameter.DisplayName);
				array[1, num] = parameter.DataType;
				array[2, num] = parameter.NumberOfElements;
				array[3, num] = parameter.Value;
				if (parameter.IsDataTable)
				{
					array[3, num] = parameter.DataTableValue;
				}
				array[4, num] = parameter.IOType;
				array[5, num] = parameter.UpperLimit;
				array[6, num] = parameter.LowerLimit;
				array[7, num] = parameter.UnitOfMeasure;
				array[8, num] = parameter.Description;
				array[9, num] = parameter.Constraints;
				array[10, num] = parameter.DisplayHint;
				array[11, num] = true;
				if (parameter.CheckedState == CheckState.Checked)
				{
					array[12, num] = true;
				}
				else
				{
					array[12, num] = false;
				}
				array[13, num] = parameter.IsNullable;
				array[14, num] = parameter.ParentName;
				array[15, num] = parameter.IsArray && parameter.NumberOfElements == -1;
				array[16, num] = parameter.IsEditableInTestPlan;
				array[17, num] = parameter.CanEditResultValue;
				array[18, num] = parameter.Name;
				num++;
			}
			return array;
		}
		catch
		{
			return null;
		}
	}

	public static object CreateArguments(Hashtable argumentParameters, object objParameters)
	{
		try
		{
			Hashtable hashtable = CreateParameters(objParameters);
			foreach (TestParameter value in hashtable.Values)
			{
				if (value.IOType == "Input" && argumentParameters.Contains(value.Name))
				{
					TestParameter testParameter2 = (TestParameter)argumentParameters[value.Name];
					testParameter2.Value = value.Value;
					testParameter2.NumberOfElements = value.NumberOfElements;
					testParameter2.DataTableValue = value.DataTableValue;
				}
			}
			object[] array = new object[argumentParameters.Count];
			for (int i = 0; i < argumentParameters.Count; i++)
			{
				TestParameter testParameter3 = GetTestParameter(argumentParameters, i, "Input");
				if (testParameter3 != null)
				{
					SetParameterValue(testParameter3, ref array[i]);
				}
			}
			return array;
		}
		catch
		{
		}
		return null;
	}

	public static void SetOutputParameterLimits(Hashtable hashParameters, object objParameters)
	{
		try
		{
			Hashtable hashtable = CreateParameters(objParameters);
			foreach (TestParameter value in hashParameters.Values)
			{
				if (value.IOType == "Output" && hashtable.Contains(value.Name))
				{
					TestParameter testParameter2 = (TestParameter)hashtable[value.Name];
					value.LowerLimit = testParameter2.LowerLimit;
					value.UpperLimit = testParameter2.UpperLimit;
					value.Description = testParameter2.Description;
					value.UnitOfMeasure = testParameter2.UnitOfMeasure;
				}
			}
		}
		catch
		{
		}
	}

	public static Hashtable CreateInputProperties(Hashtable inputProperties, object objParameters)
	{
		try
		{
			Hashtable hashtable = new Hashtable();
			Hashtable hashtable2 = CreateParameters(objParameters);
			foreach (TestParameter value in inputProperties.Values)
			{
				if (hashtable2.Contains(value.Name))
				{
					TestParameter testParameter2 = (TestParameter)hashtable2[value.Name];
					value.Value = testParameter2.Value;
					value.DataTableValue = testParameter2.DataTableValue;
					value.NumberOfElements = testParameter2.NumberOfElements;
					object objArgument = "";
					SetParameterValue(value, ref objArgument);
					hashtable.Add(value.Name, objArgument);
				}
			}
			return hashtable;
		}
		catch
		{
		}
		return null;
	}

	public static void SetOutputProperties(Hashtable outputProperties, object objParameters)
	{
		try
		{
			Hashtable hashtable = CreateParameters(objParameters);
			foreach (TestParameter value in outputProperties.Values)
			{
				if (hashtable.Contains(value.Name))
				{
					TestParameter testParameter2 = (TestParameter)hashtable[value.Name];
					value.LowerLimit = testParameter2.LowerLimit;
					value.UpperLimit = testParameter2.UpperLimit;
					value.Description = testParameter2.Description;
					value.UnitOfMeasure = testParameter2.UnitOfMeasure;
					value.NumberOfElements = testParameter2.NumberOfElements;
				}
			}
		}
		catch
		{
		}
	}

	public static TestParameter GetTestParameter(Hashtable parameters, int position, string ioType)
	{
		try
		{
			foreach (TestParameter value in parameters.Values)
			{
				if (value.Position == position && value.IOType == ioType)
				{
					return value;
				}
			}
		}
		catch
		{
		}
		return null;
	}

	private static object GetActualValue(TestParameter param, string paramValue)
	{
		object result = null;
		try
		{
			switch (param.DataType)
			{
			case "Int":
				if (!param.Type.IsEnum)
				{
					if (paramValue.ToUpper().StartsWith("0X"))
					{
						string text = paramValue.Remove(0, 2);
						if (text.Length > 0)
						{
							result = int.Parse(text, NumberStyles.HexNumber);
						}
					}
					else
					{
						result = ((!param.IsSingle) ? ((!(paramValue.ToLower() == "yes") && !(paramValue.ToLower() == "on") && !(paramValue.ToLower() == "true")) ? ((!(paramValue.ToLower() == "no") && !(paramValue.ToLower() == "off") && !(paramValue.ToLower() == "false")) ? ((object)int.Parse(paramValue)) : ((object)0)) : ((object)1)) : ((object)short.Parse(paramValue)));
					}
				}
				else
				{
					result = Enum.Parse(param.Type, paramValue);
				}
				break;
			case "Double":
				result = ((!param.IsSingle) ? ((object)double.Parse(paramValue, CultureInfo.InvariantCulture)) : ((object)float.Parse(paramValue)));
				break;
			case "Byte":
				result = byte.Parse(paramValue);
				break;
			case "UInt32":
				result = uint.Parse(paramValue);
				break;
			case "UInt16":
				result = ushort.Parse(paramValue);
				break;
			case "UInt64":
				result = ulong.Parse(paramValue);
				break;
			case "Int16":
				result = short.Parse(paramValue);
				break;
			case "Int64":
				result = long.Parse(paramValue);
				break;
			case "Decimal":
				result = decimal.Parse(paramValue);
				break;
			case "Single":
				result = float.Parse(paramValue);
				break;
			case "SByte":
				result = sbyte.Parse(paramValue);
				break;
			case "Char":
				result = char.Parse(paramValue.Trim());
				break;
			case "String":
				result = paramValue;
				break;
			case "Boolean":
				result = false;
				result = ((!(paramValue == "1") && !(paramValue.ToLower() == "true") && !(paramValue.ToLower() == "yes") && !(paramValue.ToLower() == "on")) ? ((object)Convert.ToBoolean(paramValue)) : ((object)true));
				break;
			}
		}
		catch
		{
		}
		return result;
	}

	private static void SetParameterValue(TestParameter param, ref object objArgument)
	{
		try
		{
			if (param.IsDataTable)
			{
				objArgument = param.DataTableValue;
			}
			else if (!param.IsArray && !param.Type.Name.EndsWith("[]&"))
			{
				if (param.IsNullable)
				{
					objArgument = (string.IsNullOrEmpty(param.Value) ? null : GetActualValue(param, param.Value));
				}
				else
				{
					objArgument = GetActualValue(param, param.Value);
				}
			}
			else
			{
				objArgument = SetActualValueForArrayType(param, objArgument);
			}
		}
		catch
		{
		}
	}

	internal static string[] SplitStringArrayParameter(string paramString)
	{
		ArrayList arrayList = new ArrayList();
		if (paramString != null && paramString.Length > 0)
		{
			bool flag = false;
			bool flag2 = true;
			string text = paramString;
			SearchState searchState = SearchState.state1;
			int num = 0;
			while (!flag)
			{
				if (num > text.Length - 1)
				{
					searchState = SearchState.state8;
					flag = true;
				}
				switch (searchState)
				{
				case SearchState.state1:
					if (text[num] == '_')
					{
						searchState = SearchState.state2;
					}
					else if (text[num] == ',')
					{
						searchState = SearchState.state8;
					}
					break;
				case SearchState.state2:
					searchState = ((text[num] != '"') ? ((text[num] == ',') ? SearchState.state8 : SearchState.state1) : SearchState.state3);
					break;
				case SearchState.state3:
					if (text[num] == '_')
					{
						searchState = SearchState.state4;
						flag2 = false;
					}
					else
					{
						searchState = ((text[num] == ',') ? SearchState.state8 : SearchState.state1);
					}
					break;
				case SearchState.state4:
					if (text[num] == '_')
					{
						searchState = SearchState.state5;
					}
					break;
				case SearchState.state5:
					searchState = ((text[num] != '"') ? SearchState.state4 : SearchState.state6);
					break;
				case SearchState.state6:
					if (text[num] == '_')
					{
						searchState = SearchState.state7;
						flag2 = true;
					}
					else
					{
						searchState = SearchState.state4;
					}
					break;
				case SearchState.state7:
					if (text[num] == ',')
					{
						searchState = SearchState.state8;
					}
					break;
				case SearchState.state8:
					if (!flag && num <= text.Length - 1)
					{
						string value = text.Substring(0, num).Trim(',').Replace("_\"_", "");
						arrayList.Add(value);
						text = text.Substring(num);
						num = -1;
						searchState = SearchState.state1;
					}
					else
					{
						flag = true;
					}
					if (flag)
					{
						if (text.Length == 0)
						{
							arrayList.Add("");
						}
						else if (text.EndsWith(",") && flag2)
						{
							arrayList.Add(text.Trim(',').Replace("_\"_", ""));
							arrayList.Add("");
						}
						else
						{
							arrayList.Add(text.Replace("_\"_", ""));
						}
					}
					break;
				}
				num++;
			}
		}
		if (arrayList.Count == 0)
		{
			arrayList.Add("");
		}
		string[] array = new string[arrayList.Count];
		for (int i = 0; i < arrayList.Count; i++)
		{
			array[i] = arrayList[i].ToString();
		}
		return array;
	}

	private static object SetActualValueForArrayType(TestParameter param, object objArgument)
	{
		string[] array = ((!(param.DataType != "String")) ? SplitStringArrayParameter(param.Value) : param.Value.Split(','));
		int num = array.Length;
		switch (param.DataType)
		{
		case "Int":
			if (param.IsSingle)
			{
				short[] array16 = new short[num];
				for (int num10 = 0; num10 < num; num10++)
				{
					array16[num10] = (short)GetActualValue(param, array[num10]);
				}
				objArgument = array16;
			}
			else
			{
				int[] array17 = new int[num];
				for (int num11 = 0; num11 < num; num11++)
				{
					array17[num11] = (int)GetActualValue(param, array[num11]);
				}
				objArgument = array17;
			}
			break;
		case "Double":
			if (param.IsSingle)
			{
				float[] array10 = new float[num];
				for (int num4 = 0; num4 < num; num4++)
				{
					array10[num4] = (float)GetActualValue(param, array[num4]);
				}
				objArgument = array10;
			}
			else
			{
				double[] array11 = new double[num];
				for (int num5 = 0; num5 < num; num5++)
				{
					array11[num5] = (double)GetActualValue(param, array[num5]);
				}
				objArgument = array11;
			}
			break;
		case "Boolean":
		{
			bool[] array3 = new bool[num];
			for (int j = 0; j < num; j++)
			{
				array3[j] = (bool)GetActualValue(param, array[j]);
			}
			objArgument = array3;
			break;
		}
		case "Byte":
		{
			byte[] array7 = new byte[num];
			for (int n = 0; n < num; n++)
			{
				array7[n] = (byte)GetActualValue(param, array[n]);
			}
			objArgument = array7;
			break;
		}
		case "String":
		{
			string[] array13 = new string[num];
			for (int num7 = 0; num7 < num; num7++)
			{
				array13[num7] = (string)GetActualValue(param, array[num7]);
			}
			objArgument = array13;
			break;
		}
		case "UInt32":
		{
			uint[] array6 = new uint[num];
			for (int m = 0; m < num; m++)
			{
				array6[m] = (uint)GetActualValue(param, array[m]);
			}
			objArgument = array6;
			break;
		}
		case "UInt16":
		{
			ushort[] array14 = new ushort[num];
			for (int num8 = 0; num8 < num; num8++)
			{
				array14[num8] = (ushort)GetActualValue(param, array[num8]);
			}
			objArgument = array14;
			break;
		}
		case "UInt64":
		{
			ulong[] array9 = new ulong[num];
			for (int num3 = 0; num3 < num; num3++)
			{
				array9[num3] = (ulong)GetActualValue(param, array[num3]);
			}
			objArgument = array9;
			break;
		}
		case "Int16":
		{
			short[] array4 = new short[num];
			for (int k = 0; k < num; k++)
			{
				array4[k] = (short)GetActualValue(param, array[k]);
			}
			objArgument = array4;
			break;
		}
		case "Int64":
		{
			long[] array15 = new long[num];
			for (int num9 = 0; num9 < num; num9++)
			{
				array15[num9] = (long)GetActualValue(param, array[num9]);
			}
			objArgument = array15;
			break;
		}
		case "Decimal":
		{
			decimal[] array12 = new decimal[num];
			for (int num6 = 0; num6 < num; num6++)
			{
				ref decimal reference = ref array12[num6];
				reference = (decimal)GetActualValue(param, array[num6]);
			}
			objArgument = array12;
			break;
		}
		case "Single":
		{
			float[] array8 = new float[num];
			for (int num2 = 0; num2 < num; num2++)
			{
				array8[num2] = (float)GetActualValue(param, array[num2]);
			}
			objArgument = array8;
			break;
		}
		case "SByte":
		{
			sbyte[] array5 = new sbyte[num];
			for (int l = 0; l < num; l++)
			{
				array5[l] = (sbyte)GetActualValue(param, array[l]);
			}
			objArgument = array5;
			break;
		}
		case "Char":
		{
			char[] array2 = new char[num];
			for (int i = 0; i < num; i++)
			{
				array2[i] = (char)GetActualValue(param, array[i]);
			}
			objArgument = array2;
			break;
		}
		}
		return objArgument;
	}

	public static bool SetOutputProperty(TestParameter param, Array arrayOutput)
	{
		try
		{
			if (arrayOutput != null)
			{
				param.NumberOfElements = arrayOutput.Length;
				for (int i = 0; i < arrayOutput.Length; i++)
				{
					try
					{
						if (i == 0)
						{
							param.Value = arrayOutput.GetValue(i).ToString();
						}
						else
						{
							param.Value = param.Value + "," + arrayOutput.GetValue(i).ToString();
						}
					}
					catch
					{
					}
				}
			}
		}
		catch
		{
		}
		return false;
	}

	public static bool SetOutputArguments(Hashtable parameters, object[] objResultArguments)
	{
		try
		{
			for (int i = 0; i < objResultArguments.GetLength(0); i++)
			{
				try
				{
					TestParameter testParameter = GetTestParameter(parameters, i, "Output");
					if (testParameter == null || !(testParameter.IOType == "Output"))
					{
						continue;
					}
					if (!testParameter.IsArray && !testParameter.Type.Name.EndsWith("[]&"))
					{
						if (testParameter.IsNullable && objResultArguments[i] == null)
						{
							testParameter.Value = "";
						}
						else
						{
							testParameter.Value = objResultArguments[i].ToString();
						}
						continue;
					}
					Array array = (Array)objResultArguments[i];
					testParameter.NumberOfElements = array.Length;
					testParameter.Value = "";
					for (int j = 0; j < testParameter.NumberOfElements; j++)
					{
						try
						{
							if (j == 0)
							{
								if (array.GetValue(j).ToString().Contains(","))
								{
									testParameter.Value = "_\"_" + array.GetValue(j).ToString() + "_\"_";
								}
								else
								{
									testParameter.Value = array.GetValue(j).ToString();
								}
							}
							else if (array.GetValue(j).ToString().Contains(","))
							{
								testParameter.Value = testParameter.Value + ",_\"_" + array.GetValue(j).ToString() + "_\"_";
							}
							else
							{
								testParameter.Value = testParameter.Value + "," + array.GetValue(j).ToString();
							}
						}
						catch
						{
						}
					}
				}
				catch
				{
				}
			}
			return true;
		}
		catch
		{
		}
		return false;
	}

	public static bool SetReturnValue(Hashtable parameters, object objReturnValue)
	{
		try
		{
			foreach (TestParameter value in parameters.Values)
			{
				if (!value.Argument)
				{
					value.Value = objReturnValue.ToString();
					return true;
				}
			}
		}
		catch (Exception)
		{
			return false;
		}
		return false;
	}
}
