using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Xml;

namespace QC.QMine.QMineInterface;

public class QueryParameters
{
	public const string WARN_ESTIMATED_RECORDS = "WarnEstimatedRecords";

	public const string SAMPLING_PERCENT = "SamplingPercent";

	public const string IS_SAMPLING_PERCENT = "IsSamplingPercent";

	public const string DVT_FLAG = "DvtFlag";

	public const string TESTERS = "Testers";

	public const string TESTER_TYPE = "TesterType";

	public const string TREEIDs = "TreeIDs";

	public const string TEST_LIST = "TestList";

	public const string PARAM_LIST = "ParamsList";

	public const string PRODUCT_TYPE = "ProductType";

	public const string PRODUCT_TYPE_AS_STRING = "ProductTypeAsString";

	public const string LOT = "Lot";

	public const string PLATES = "Plates";

	public const string SUBPLATES = "SubPlates";

	public const string PANELS = "Panels";

	public const string LOCATIONS = "Locations";

	public const string VISIT_NUMBER = "VisitNumber";

	public const string GLOBAL_RESULT = "GlobalResult";

	public const string NODE_RESULT_SELECTION = "NodeResultSelection";

	public const string PARAM_RESULT_SELECTION = "ParamResultSelection";

	public const string NODERIDs = "NodeRIDs";

	public const string START_TIME = "StartTime";

	public const string STOP_TIME = "StopTime";

	public const string DATE_OPTION = "DateOption";

	public const string LOOP_CONDITIONS = "LoopCondition";

	public const string RUN_TITLE_CONDITIONS = "RunTitleCondition";

	public const string USER = "User";

	public const string WORKSTATION = "WorkStation";

	public const string MCNs = "MCN";

	public const string SERIAL_NUMBERS = "SerialNumbers";

	public const string SERIAL_NUMBER_SELECTION = "SerialNumberSelection";

	public const string QMT_FILTER_ENABLED = "QMT_FilterEnabled";

	public const string DISABLE_LOOP_CONDITIONS_FILTER = "Disable_Loop_Conditions_Filter";

	public const string DISABLE_RUN_TITLE_CONDITIONS_FILTER = "Disable_Run_Title_Conditions_Filter";

	public const string LAST_SESSION_DATA_RID = "LastSessionDataRID";

	public const string LOAD_TEST_FROM_REGISTRATION = "LoadTestFromRegistration";

	public const string SESSION_DATA_RID = "SessionDataRID";

	public const string IGNORE_DUPLICATED_SN = "IgnoreDuplicatedSN";

	public const string XML_FOLDER = "XmlFolder";

	public const string TESTS_WITH_PARAM_ONLY = "TestsWithParamOnly";

	public const string TEST_RESULTS = "TestResults";

	public const string CONDITION_COLUMN_NAME = "Condition";

	public const string CONDITION_VALUE_COLUMN_NAME = "Value";

	public string WarnEstimatedRecords = "75000";

	public int SamplingPercentOrUnit = -1;

	public bool isSamplingPercent;

	public string DvtFlag = "";

	public string Testers = "";

	public string TesterType = "";

	public string TreeIDs = "";

	public string TestList = "";

	public string ParamsList = "";

	public string ProductType = "";

	public string ProductTypeAsString = "";

	public string Lot = "";

	public string Plates = "";

	public string SubPlates = "";

	public string Panels = "";

	public string Locations = "";

	public string VisitNumber = "";

	public string GlobalResult = "";

	public string NodeResultSelection = "";

	public string ParamResultSelection = "";

	public string NodeRIDs = "";

	public string StartTime = "";

	public string StopTime = "";

	public string DateOption = "";

	public string LoopCondition = "";

	public string RunTitleCondition = "";

	public string User = "";

	public string WorkStation = "";

	public string MCN = "";

	public string SerialNumbers = "";

	public string SerialNumberSelection = "";

	public string QMT_FilterEnabled = "false";

	public string DisableLoopConditionsFilter = "false";

	public string DisableRunTitleConditionsFilter = "false";

	public string LastRanTime = "";

	public string LastSessionDataRID = "";

	public string SessionDataRIDs = "";

	public string IgnoreDuplicatedSN = "";

	public string XmlFolder = "";

	public string TestsWithParamOnly = "";

	public string TestResults = "";

	public QueryParameters Clone()
	{
		QueryParameters queryParameters = new QueryParameters();
		queryParameters.DvtFlag = DvtFlag;
		queryParameters.TesterType = TesterType;
		queryParameters.Testers = Testers;
		queryParameters.TreeIDs = TreeIDs;
		queryParameters.TestList = TestList;
		queryParameters.ParamsList = ParamsList;
		queryParameters.ProductType = ProductType;
		queryParameters.ProductTypeAsString = ProductTypeAsString;
		queryParameters.Lot = Lot;
		queryParameters.Plates = Plates;
		queryParameters.SubPlates = SubPlates;
		queryParameters.Panels = Panels;
		queryParameters.Locations = Locations;
		queryParameters.VisitNumber = VisitNumber;
		queryParameters.GlobalResult = GlobalResult;
		queryParameters.NodeRIDs = NodeRIDs;
		queryParameters.StartTime = StartTime;
		queryParameters.StopTime = StopTime;
		queryParameters.DateOption = DateOption;
		queryParameters.LoopCondition = LoopCondition;
		queryParameters.RunTitleCondition = RunTitleCondition;
		queryParameters.User = User;
		queryParameters.NodeResultSelection = NodeResultSelection;
		queryParameters.WorkStation = WorkStation;
		queryParameters.MCN = MCN;
		queryParameters.SerialNumbers = SerialNumbers;
		queryParameters.SerialNumberSelection = SerialNumberSelection;
		queryParameters.QMT_FilterEnabled = QMT_FilterEnabled;
		queryParameters.DisableLoopConditionsFilter = DisableLoopConditionsFilter;
		queryParameters.DisableRunTitleConditionsFilter = DisableRunTitleConditionsFilter;
		return queryParameters;
	}

	public void LoadXML(string InputXML, bool isQmtDatabase)
	{
		try
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.LoadXml(InputXML);
			User = GetTagValue(xmlDocument, "User");
			DateOption = GetTagValue(xmlDocument, "DateOption");
			StartTime = GetTagValue(xmlDocument, "StartTime");
			StopTime = GetTagValue(xmlDocument, "StopTime");
			DvtFlag = GetTagValue(xmlDocument, "DvtFlag");
			TesterType = GetTagValue(xmlDocument, "TesterType");
			Testers = GetTagValue(xmlDocument, "Testers");
			WorkStation = GetTagValue(xmlDocument, "WorkStation");
			MCN = GetTagValue(xmlDocument, "MCN");
			DisableRunTitleConditionsFilter = GetTagValue(xmlDocument, "Disable_Run_Title_Conditions_Filter");
			RunTitleCondition = GetTagValue(xmlDocument, "RunTitleCondition");
			TreeIDs = GetTagValue(xmlDocument, "TreeIDs");
			DisableLoopConditionsFilter = GetTagValue(xmlDocument, "Disable_Loop_Conditions_Filter");
			LoopCondition = GetTagValue(xmlDocument, "LoopCondition");
			TestList = GetTagValue(xmlDocument, "TestList");
			NodeResultSelection = GetTagValue(xmlDocument, "NodeResultSelection");
			NodeRIDs = GetTagValue(xmlDocument, "NodeRIDs");
			ParamsList = GetTagValue(xmlDocument, "ParamsList");
			WarnEstimatedRecords = GetTagValue(xmlDocument, "WarnEstimatedRecords");
			ParamResultSelection = GetTagValue(xmlDocument, "ParamResultSelection");
			SamplingPercentOrUnit = Convert.ToInt32(GetTagValue(xmlDocument, "SamplingPercent"));
			isSamplingPercent = Convert.ToBoolean(GetTagValue(xmlDocument, "IsSamplingPercent"));
			if (isQmtDatabase)
			{
				QMT_FilterEnabled = GetTagValue(xmlDocument, "QMT_FilterEnabled");
				ProductType = GetTagValue(xmlDocument, "ProductType");
				ProductTypeAsString = GetTagValue(xmlDocument, "ProductTypeAsString");
				Lot = GetTagValue(xmlDocument, "Lot");
				Plates = GetTagValue(xmlDocument, "Plates");
				SubPlates = GetTagValue(xmlDocument, "SubPlates");
				Panels = GetTagValue(xmlDocument, "Panels");
				Locations = GetTagValue(xmlDocument, "Locations");
			}
			SerialNumbers = GetTagValue(xmlDocument, "SerialNumbers");
			SerialNumberSelection = GetTagValue(xmlDocument, "SerialNumberSelection");
			VisitNumber = GetTagValue(xmlDocument, "VisitNumber");
			GlobalResult = GetTagValue(xmlDocument, "GlobalResult");
			LastSessionDataRID = GetTagValue(xmlDocument, "LastSessionDataRID");
			SessionDataRIDs = GetTagValue(xmlDocument, "SessionDataRID");
			xmlDocument = null;
		}
		catch
		{
		}
	}

	private string GetTagValue(XmlDocument xmlRoot, string TagName)
	{
		try
		{
			return xmlRoot.GetElementsByTagName(TagName)[0].InnerText;
		}
		catch
		{
		}
		return "";
	}

	public string ToXml()
	{
		return ToXml(includeDeclaration: true, isQmtDatabase: true);
	}

	public string ToXml(bool isQmtDatabase)
	{
		return ToXml(includeDeclaration: true, isQmtDatabase);
	}

	public string ToXml(bool includeDeclaration, bool isQmtDatabase)
	{
		try
		{
			XmlDocument xmlDocument = new XmlDocument();
			if (includeDeclaration)
			{
				XmlNode newChild = xmlDocument.CreateNode(XmlNodeType.XmlDeclaration, "", "");
				xmlDocument.AppendChild(newChild);
			}
			XmlNode xmlNode = xmlDocument.CreateNode(XmlNodeType.Element, "Root", "");
			xmlDocument.AppendChild(xmlNode);
			xmlNode.AppendChild(CreateElement(xmlDocument, "User", User));
			xmlNode.AppendChild(CreateElement(xmlDocument, "DateOption", DateOption));
			xmlNode.AppendChild(CreateElement(xmlDocument, "StartTime", StartTime));
			xmlNode.AppendChild(CreateElement(xmlDocument, "StopTime", StopTime));
			xmlNode.AppendChild(CreateElement(xmlDocument, "LastSessionDataRID", LastSessionDataRID));
			xmlNode.AppendChild(CreateElement(xmlDocument, "SessionDataRID", SessionDataRIDs));
			xmlNode.AppendChild(CreateElement(xmlDocument, "DvtFlag", DvtFlag));
			xmlNode.AppendChild(CreateElement(xmlDocument, "TesterType", TesterType));
			xmlNode.AppendChild(CreateElement(xmlDocument, "Testers", Testers));
			xmlNode.AppendChild(CreateElement(xmlDocument, "WorkStation", WorkStation));
			xmlNode.AppendChild(CreateElement(xmlDocument, "MCN", MCN));
			xmlNode.AppendChild(CreateElement(xmlDocument, "Disable_Run_Title_Conditions_Filter", DisableRunTitleConditionsFilter));
			xmlNode.AppendChild(CreateElement(xmlDocument, "RunTitleCondition", RunTitleCondition));
			xmlNode.AppendChild(CreateElement(xmlDocument, "TreeIDs", TreeIDs));
			xmlNode.AppendChild(CreateElement(xmlDocument, "Disable_Loop_Conditions_Filter", DisableLoopConditionsFilter));
			xmlNode.AppendChild(CreateElement(xmlDocument, "LoopCondition", LoopCondition));
			xmlNode.AppendChild(CreateElement(xmlDocument, "TestList", TestList));
			xmlNode.AppendChild(CreateElement(xmlDocument, "NodeResultSelection", NodeResultSelection));
			xmlNode.AppendChild(CreateElement(xmlDocument, "NodeRIDs", NodeRIDs));
			xmlNode.AppendChild(CreateElement(xmlDocument, "ParamsList", ParamsList));
			xmlNode.AppendChild(CreateElement(xmlDocument, "SamplingPercent", SamplingPercentOrUnit.ToString()));
			xmlNode.AppendChild(CreateElement(xmlDocument, "IsSamplingPercent", isSamplingPercent.ToString()));
			xmlNode.AppendChild(CreateElement(xmlDocument, "ParamResultSelection", ParamResultSelection));
			xmlNode.AppendChild(CreateElement(xmlDocument, "WarnEstimatedRecords", WarnEstimatedRecords));
			if (isQmtDatabase)
			{
				xmlNode.AppendChild(CreateElement(xmlDocument, "QMT_FilterEnabled", QMT_FilterEnabled));
				xmlNode.AppendChild(CreateElement(xmlDocument, "ProductType", ProductType));
				xmlNode.AppendChild(CreateElement(xmlDocument, "ProductTypeAsString", ProductTypeAsString));
				xmlNode.AppendChild(CreateElement(xmlDocument, "Lot", Lot));
				xmlNode.AppendChild(CreateElement(xmlDocument, "Plates", Plates));
				xmlNode.AppendChild(CreateElement(xmlDocument, "SubPlates", SubPlates));
				xmlNode.AppendChild(CreateElement(xmlDocument, "Panels", Panels));
				xmlNode.AppendChild(CreateElement(xmlDocument, "Locations", Locations));
			}
			xmlNode.AppendChild(CreateElement(xmlDocument, "SerialNumberSelection", SerialNumberSelection));
			xmlNode.AppendChild(CreateElement(xmlDocument, "SerialNumbers", SerialNumbers));
			xmlNode.AppendChild(CreateElement(xmlDocument, "VisitNumber", VisitNumber));
			xmlNode.AppendChild(CreateElement(xmlDocument, "GlobalResult", GlobalResult));
			return xmlNode.OuterXml;
		}
		catch
		{
		}
		return "<Root></Root>";
	}

	public virtual DataTable ToTable(bool ignoreEmptyFields)
	{
		return ToTable(ignoreEmptyFields, (string[])null);
	}

	public virtual DataTable ToTable(bool ignoreEmptyFields, params string[] selectedFields)
	{
		List<string> list = new List<string>();
		if (selectedFields != null)
		{
			foreach (string text in selectedFields)
			{
				list.Add(text.ToLower());
			}
		}
		DataTable dataTable = new DataTable("QueryParameterTable");
		DataColumn column = new DataColumn("Condition", typeof(string));
		dataTable.Columns.Add(column);
		DataColumn column2 = new DataColumn("Value", typeof(string));
		dataTable.Columns.Add(column2);
		FieldInfo[] fields = GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		FieldInfo[] array = fields;
		foreach (FieldInfo fieldInfo in array)
		{
			string name = fieldInfo.Name;
			if (selectedFields == null || list.Contains(name.ToLower()))
			{
				string text2 = Convert.ToString(fieldInfo.GetValue(this));
				if (!ignoreEmptyFields || text2.Length > 0)
				{
					DataRow dataRow = dataTable.NewRow();
					dataRow["Condition"] = name;
					dataRow["Value"] = text2;
					dataTable.Rows.Add(dataRow);
				}
			}
		}
		return dataTable;
	}

	public static XmlElement CreateElement(XmlDocument xmlDoc, string ElementName, string Text)
	{
		XmlElement xmlElement = xmlDoc.CreateElement("", ElementName, "");
		XmlText newChild = xmlDoc.CreateTextNode(Text.Trim());
		xmlElement.AppendChild(newChild);
		return xmlElement;
	}
}
