using System;
using System.Data.SqlClient;
using System.Windows.Forms;
using QC.QTMDotNetKernelInterface;

namespace QC.QTMToolKit.Tools.Databasetools;

[QTMTestClass]
public class GetParamValueFromDB
{
	private string testerID;

	private string paramName;

	private string dbname;

	private string servername;

	private string testName = "";

	private string serialID;

	private string paramValue;

	private string unitString;

	private bool isV2Schema = true;

	private bool isNumericValue = true;

	[QTMParameter(ParameterIOType.Input, TestList = "GetParamValue")]
	public string TesterID
	{
		get
		{
			return testerID;
		}
		set
		{
			testerID = value;
		}
	}

	[QTMParameter(ParameterIOType.Input, TestList = "GetParamValue")]
	public string ParamName
	{
		get
		{
			return paramName;
		}
		set
		{
			paramName = value;
		}
	}

	[QTMParameter(ParameterIOType.Input, TestList = "GetParamValue")]
	public string Database
	{
		get
		{
			return dbname;
		}
		set
		{
			dbname = value;
		}
	}

	[QTMParameter(ParameterIOType.Input, TestList = "GetParamValue")]
	public string Server
	{
		get
		{
			return servername;
		}
		set
		{
			servername = value;
		}
	}

	[QTMParameter(ParameterIOType.Input, TestList = "GetParamValue")]
	public string TestName
	{
		get
		{
			return testName;
		}
		set
		{
			testName = value;
		}
	}

	[QTMParameter(ParameterIOType.Input, TestList = "GetParamValue")]
	public string SerialID
	{
		get
		{
			return serialID;
		}
		set
		{
			serialID = value;
		}
	}

	[QTMParameter(ParameterIOType.Output, TestList = "GetParamValue")]
	public string ParamValue
	{
		get
		{
			return paramValue;
		}
		set
		{
			paramValue = value;
		}
	}

	[QTMParameter(ParameterIOType.Input, TestList = "GetParamValue")]
	public bool IsV2Schema
	{
		get
		{
			return isV2Schema;
		}
		set
		{
			isV2Schema = value;
		}
	}

	[QTMParameter(ParameterIOType.Input, TestList = "GetParamValue")]
	public bool IsNumericValue
	{
		get
		{
			return isNumericValue;
		}
		set
		{
			isNumericValue = value;
		}
	}

	[QTMTest(CanAddToTestPlan = true)]
	public bool GetParamValue()
	{
		bool result = true;
		try
		{
			string connectionString = "workstation id=" + Server + ";packet size=4096;user id=QDMAppUser;data source=" + Server + ";persist security info=True;initial catalog=" + Database + ";password=Qual1234$";
			if (string.IsNullOrEmpty(serialID))
			{
				if (SerialNumber.GetCurrentSerialNumber(out unitString))
				{
					return false;
				}
			}
			else
			{
				unitString = SerialID;
			}
			if (unitString == null)
			{
				MessageBox.Show("unitString is null");
			}
			using (SqlConnection sqlConnection = new SqlConnection(connectionString))
			{
				sqlConnection.Open();
				if (unitString.Contains("-DW"))
				{
					string[] array = unitString.Split(new string[1] { "-DW" }, StringSplitOptions.RemoveEmptyEntries);
					unitString = array[0];
				}
				string text = "";
				if (IsV2Schema)
				{
					text = ((!IsNumericValue) ? "Select CharValue from viewParameterResultV2 where LastVisit = 1 and " : "Select NumValue from viewParameterResultV2 where LastVisit = 1 and ");
					string text2 = text;
					text = text2 + "TesterID = '" + testerID + "' and ParameterName = '" + ParamName + "' ";
					text = text + " and UUT_SN = '" + unitString + "'";
					if (TestName.Length > 0)
					{
						text = text + " and TestName = '" + TestName + "'";
					}
				}
				else
				{
					text = ((!IsNumericValue) ? "Select ParamCharResult from viewParameterResult where LastVisit = 1 and " : "Select NumResult from viewParameterResult where LastVisit = 1 and ");
					string text3 = text;
					text = text3 + "TesterID = '" + testerID + "' and ParamName = '" + ParamName + "' ";
					text = text + " and SerialID = '" + unitString + "'";
					if (TestName.Length > 0)
					{
						text = text + " and NodeName = '" + TestName + "'";
					}
				}
				SqlCommand sqlCommand = new SqlCommand(text, sqlConnection);
				SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
				if (sqlDataReader.Read())
				{
					if (IsV2Schema)
					{
						if (IsNumericValue)
						{
							ParamValue = sqlDataReader["NumValue"].ToString();
						}
						else
						{
							ParamValue = sqlDataReader["CharValue"].ToString();
						}
					}
					else if (IsNumericValue)
					{
						ParamValue = sqlDataReader["NumResult"].ToString();
					}
					else
					{
						ParamValue = sqlDataReader["ParamCharResult"].ToString();
					}
				}
				else
				{
					ParamValue = "-9999";
					result = false;
				}
			}
			return result;
		}
		catch (Exception ex)
		{
			MessageBox.Show(ex.ToString());
			return false;
		}
	}
}
