using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using QC.QsprShared.Dialogs;
using QC.QsprShared.Trace;
using QC.QTMInterface;

namespace QC.QTMApplication;

public class GetSerialNo : Form
{
	private const int SERIAL_NUMBER_LENGTH = 9;

	private const string COMMENTS = "Comments";

	private Button btnOK;

	private Button btnCancel;

	private ErrorProvider errorProvider1;

	private TextBox txtSerialNumber;

	private ArrayList list = new ArrayList();

	private Label lblSimulation;

	private Panel panelUnitID;

	private Panel panelConfig;

	private Label label2;

	private Label label1;

	private TextBox txtComments;

	private ToolTip toolTip;

	private IContainer components;

	private int numberOfEmbeddedUnits = 1;

	private bool partOfUnitIDFound;

	private bool naQuestionAsked;

	private TesterConfigurationInfo snConfigInfo;

	public string CONFIG_NAME_VALUE_SEP = "__";

	private Panel panelButtons;

	private Panel panelEmbeddedSerialNumbers;

	public string CONFIG_SEP = " ";

	public string SerialNo
	{
		get
		{
			return txtSerialNumber.Text;
		}
		set
		{
			txtSerialNumber.Text = value;
		}
	}

	public ArrayList List
	{
		set
		{
			list = value;
		}
	}

	public GetSerialNo(ArrayList configList, string lastSerialNumber, bool simulatorMode)
	{
		InitializeComponent();
		list = configList;
		SerialNo = lastSerialNumber;
		if (simulatorMode)
		{
			lblSimulation.Visible = true;
		}
		numberOfEmbeddedUnits = 1;
		if (list.Count <= 0)
		{
			return;
		}
		int num = 0;
		foreach (TesterConfigurationInfo item in list)
		{
			if (item.DisplayAtGoNoGo)
			{
				num++;
			}
			if (item.Name == "NUM_OF_UNITS")
			{
				try
				{
					numberOfEmbeddedUnits = int.Parse(item.Value);
				}
				catch
				{
				}
			}
		}
		int num2 = (int)((double)num / 4.0);
		if (num % 4 == 0 && num > 3)
		{
			num2--;
		}
		int num3 = 0;
		if (num > 0)
		{
			num3 = 54 + num2 * 40;
		}
		int num4 = 0;
		if (numberOfEmbeddedUnits > 1)
		{
			num4 = 65;
			if (numberOfEmbeddedUnits > 12)
			{
				num4 = 140;
			}
		}
		base.Size = new Size(base.Size.Width, base.Size.Height + num3 + num4);
		if (num4 > 0)
		{
			panelEmbeddedSerialNumbers.Size = new Size(panelEmbeddedSerialNumbers.Size.Width, panelEmbeddedSerialNumbers.Size.Height + num4);
		}
		int num5 = 10;
		int num6 = 5;
		int num7 = 1;
		partOfUnitIDFound = false;
		ArrayList arrayList = new ArrayList();
		foreach (TesterConfigurationInfo item2 in list)
		{
			if (item2.PartOfUnitID)
			{
				arrayList.Add(item2);
			}
		}
		foreach (TesterConfigurationInfo item3 in list)
		{
			if (!item3.PartOfUnitID && item3.Editable)
			{
				arrayList.Add(item3);
			}
		}
		foreach (TesterConfigurationInfo item4 in list)
		{
			if (!item4.Editable)
			{
				arrayList.Add(item4);
			}
		}
		foreach (TesterConfigurationInfo item5 in arrayList)
		{
			try
			{
				if (item5.Name == "SN_REG")
				{
					snConfigInfo = item5;
				}
				else
				{
					if (!item5.DisplayAtGoNoGo)
					{
						continue;
					}
					if (item5.Name.Trim() == "Comments")
					{
						txtComments.Text = item5.Value;
						txtComments.Tag = item5;
						continue;
					}
					Label label = new Label
					{
						Text = item5.Name,
						AutoEllipsis = true,
						Location = new Point(num5, num6),
						Size = new Size(100, 16),
						TextAlign = ContentAlignment.BottomLeft
					};
					toolTip.SetToolTip(label, item5.Name + "\n" + item5.Description);
					if (!string.IsNullOrWhiteSpace(item5.DropdownOptions))
					{
						ComboBox comboBox = new ComboBox();
						string[] items = item5.DropdownOptions.Split(',');
						comboBox.Text = item5.Value;
						comboBox.Location = new Point(num5, num6 + 20);
						comboBox.Size = new Size(100, 20);
						comboBox.Tag = item5;
						comboBox.BackColor = Color.White;
						comboBox.Items.AddRange(items);
						comboBox.TextChanged += box_TextChanged;
						panelConfig.Controls.AddRange(new Control[2] { label, comboBox });
					}
					else
					{
						TextBox textBox = new TextBox();
						textBox.Text = item5.Value;
						textBox.Location = new Point(num5, num6 + 20);
						textBox.Size = new Size(100, 20);
						textBox.Tag = item5;
						textBox.BackColor = Color.White;
						textBox.TextChanged += txt_TextChanged;
						panelConfig.Controls.AddRange(new Control[2] { label, textBox });
						if (!item5.Editable)
						{
							textBox.Enabled = false;
						}
					}
					if (item5.PartOfUnitID)
					{
						partOfUnitIDFound = true;
						if (numberOfEmbeddedUnits == 1)
						{
							Panel panel = new Panel
							{
								Size = new Size(102, 22),
								BackColor = Color.LightGreen,
								Location = new Point(num5 - 1, num6 + 19)
							};
							panelConfig.Controls.Add(panel);
							panel.SendToBack();
						}
					}
					num5 += 120;
					if (num7 % 4 == 0)
					{
						num5 = 10;
						num6 += 40;
					}
					num7++;
					continue;
				}
			}
			catch (Exception ex)
			{
				TraceMessage.Write(ToString(), "GetSerialNo_Load " + ex.Message, TraceMessageType.Exception);
			}
		}
		if (partOfUnitIDFound && numberOfEmbeddedUnits == 1)
		{
			txtSerialNumber.Enabled = false;
			FormUnitID();
		}
		else if (numberOfEmbeddedUnits > 1)
		{
			string[] array = null;
			if (lastSerialNumber.Trim() != "")
			{
				array = lastSerialNumber.Split(new string[1] { "%^&" }, StringSplitOptions.RemoveEmptyEntries);
			}
			num5 = 0;
			num6 = 0;
			for (int i = 1; i <= numberOfEmbeddedUnits; i++)
			{
				Label label2 = new Label
				{
					Text = i.ToString(),
					Location = new Point(num5, num6),
					AutoSize = false,
					Size = new Size(25, 20),
					TextAlign = ContentAlignment.MiddleRight,
					ForeColor = Color.RoyalBlue
				};
				TextBox textBox2 = new TextBox
				{
					Text = "",
					CharacterCasing = CharacterCasing.Upper,
					Location = new Point(num5 + 30, num6),
					Size = new Size(80, 20),
					Tag = snConfigInfo
				};
				if (array != null && array.Length >= i)
				{
					textBox2.Text = array[i - 1];
				}
				textBox2.TextChanged += txt_TextChanged;
				panelEmbeddedSerialNumbers.Controls.AddRange(new Control[2] { label2, textBox2 });
				num5 += 115;
				if (i % 4 == 0)
				{
					num5 = 0;
					num6 += 25;
				}
			}
			txtSerialNumber.Enabled = false;
			txtSerialNumber.Text = "NA";
			panelEmbeddedSerialNumbers.TabIndex = 0;
			panelUnitID.TabIndex = 1;
			panelButtons.TabIndex = 2;
			panelConfig.TabIndex = 3;
		}
		else
		{
			txtSerialNumber.Tag = snConfigInfo;
			panelUnitID.TabIndex = 0;
			panelButtons.TabIndex = 1;
			panelConfig.TabIndex = 2;
		}
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.components = new System.ComponentModel.Container();
		this.btnOK = new System.Windows.Forms.Button();
		this.btnCancel = new System.Windows.Forms.Button();
		this.txtSerialNumber = new System.Windows.Forms.TextBox();
		this.errorProvider1 = new System.Windows.Forms.ErrorProvider(this.components);
		this.lblSimulation = new System.Windows.Forms.Label();
		this.panelUnitID = new System.Windows.Forms.Panel();
		this.label2 = new System.Windows.Forms.Label();
		this.label1 = new System.Windows.Forms.Label();
		this.txtComments = new System.Windows.Forms.TextBox();
		this.panelConfig = new System.Windows.Forms.Panel();
		this.toolTip = new System.Windows.Forms.ToolTip(this.components);
		this.panelButtons = new System.Windows.Forms.Panel();
		this.panelEmbeddedSerialNumbers = new System.Windows.Forms.Panel();
		((System.ComponentModel.ISupportInitialize)this.errorProvider1).BeginInit();
		this.panelUnitID.SuspendLayout();
		this.panelButtons.SuspendLayout();
		base.SuspendLayout();
		this.btnOK.BackColor = System.Drawing.SystemColors.Control;
		this.btnOK.Location = new System.Drawing.Point(404, 61);
		this.btnOK.Name = "btnOK";
		this.btnOK.Size = new System.Drawing.Size(75, 23);
		this.btnOK.TabIndex = 2;
		this.btnOK.Text = "Ok";
		this.btnOK.UseVisualStyleBackColor = false;
		this.btnOK.Click += new System.EventHandler(ultraButtonOK_Click);
		this.btnCancel.BackColor = System.Drawing.SystemColors.Control;
		this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		this.btnCancel.Location = new System.Drawing.Point(323, 61);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(75, 23);
		this.btnCancel.TabIndex = 3;
		this.btnCancel.Text = "Cancel";
		this.btnCancel.UseVisualStyleBackColor = false;
		this.btnCancel.Click += new System.EventHandler(ultraButtonCancel_Click);
		this.txtSerialNumber.BackColor = System.Drawing.Color.White;
		this.txtSerialNumber.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
		this.txtSerialNumber.Location = new System.Drawing.Point(98, 6);
		this.txtSerialNumber.Name = "txtSerialNumber";
		this.txtSerialNumber.Size = new System.Drawing.Size(300, 20);
		this.txtSerialNumber.TabIndex = 1;
		this.txtSerialNumber.TextChanged += new System.EventHandler(txt_TextChanged);
		this.errorProvider1.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.AlwaysBlink;
		this.errorProvider1.ContainerControl = this;
		this.lblSimulation.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
		this.lblSimulation.ForeColor = System.Drawing.Color.Red;
		this.lblSimulation.Location = new System.Drawing.Point(70, 63);
		this.lblSimulation.Name = "lblSimulation";
		this.lblSimulation.Size = new System.Drawing.Size(251, 15);
		this.lblSimulation.TabIndex = 4;
		this.lblSimulation.Text = "SIMULATION MODE";
		this.lblSimulation.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
		this.lblSimulation.Visible = false;
		this.panelUnitID.Controls.Add(this.label2);
		this.panelUnitID.Controls.Add(this.txtSerialNumber);
		this.panelUnitID.Dock = System.Windows.Forms.DockStyle.Bottom;
		this.panelUnitID.Location = new System.Drawing.Point(0, 50);
		this.panelUnitID.Name = "panelUnitID";
		this.panelUnitID.Size = new System.Drawing.Size(484, 28);
		this.panelUnitID.TabIndex = 2;
		this.label2.AutoSize = true;
		this.label2.Location = new System.Drawing.Point(36, 6);
		this.label2.Name = "label2";
		this.label2.Size = new System.Drawing.Size(40, 13);
		this.label2.TabIndex = 0;
		this.label2.Text = "Unit ID";
		this.label1.AutoSize = true;
		this.label1.Location = new System.Drawing.Point(36, 5);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(56, 13);
		this.label1.TabIndex = 0;
		this.label1.Text = "Comments";
		this.txtComments.Location = new System.Drawing.Point(98, 5);
		this.txtComments.Multiline = true;
		this.txtComments.Name = "txtComments";
		this.txtComments.Size = new System.Drawing.Size(300, 50);
		this.txtComments.TabIndex = 1;
		this.panelConfig.Dock = System.Windows.Forms.DockStyle.Fill;
		this.panelConfig.Location = new System.Drawing.Point(0, 0);
		this.panelConfig.Name = "panelConfig";
		this.panelConfig.Size = new System.Drawing.Size(484, 40);
		this.panelConfig.TabIndex = 0;
		this.panelButtons.Controls.Add(this.lblSimulation);
		this.panelButtons.Controls.Add(this.label1);
		this.panelButtons.Controls.Add(this.btnOK);
		this.panelButtons.Controls.Add(this.txtComments);
		this.panelButtons.Controls.Add(this.btnCancel);
		this.panelButtons.Dock = System.Windows.Forms.DockStyle.Bottom;
		this.panelButtons.Location = new System.Drawing.Point(0, 78);
		this.panelButtons.Name = "panelButtons";
		this.panelButtons.Size = new System.Drawing.Size(484, 89);
		this.panelButtons.TabIndex = 3;
		this.panelEmbeddedSerialNumbers.AutoScroll = true;
		this.panelEmbeddedSerialNumbers.Dock = System.Windows.Forms.DockStyle.Bottom;
		this.panelEmbeddedSerialNumbers.Location = new System.Drawing.Point(0, 40);
		this.panelEmbeddedSerialNumbers.Name = "panelEmbeddedSerialNumbers";
		this.panelEmbeddedSerialNumbers.Size = new System.Drawing.Size(484, 10);
		this.panelEmbeddedSerialNumbers.TabIndex = 1;
		base.AcceptButton = this.btnOK;
		this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
		this.BackColor = System.Drawing.Color.White;
		base.CancelButton = this.btnCancel;
		base.ClientSize = new System.Drawing.Size(484, 167);
		base.Controls.Add(this.panelConfig);
		base.Controls.Add(this.panelEmbeddedSerialNumbers);
		base.Controls.Add(this.panelUnitID);
		base.Controls.Add(this.panelButtons);
		base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
		base.MaximizeBox = false;
		base.MinimizeBox = false;
		base.Name = "GetSerialNo";
		base.ShowIcon = false;
		base.ShowInTaskbar = false;
		base.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
		this.Text = "QSPR - Unit Serial Number";
		base.Load += new System.EventHandler(GetSerialNo_Load);
		((System.ComponentModel.ISupportInitialize)this.errorProvider1).EndInit();
		this.panelUnitID.ResumeLayout(false);
		this.panelUnitID.PerformLayout();
		this.panelButtons.ResumeLayout(false);
		this.panelButtons.PerformLayout();
		base.ResumeLayout(false);
	}

	private bool IsListValid()
	{
		bool result = true;
		try
		{
			foreach (Control control4 in panelConfig.Controls)
			{
				if (!(control4 is TextBox))
				{
					continue;
				}
				TextBox textBox = (TextBox)control4;
				TesterConfigurationInfo testerConfigurationInfo = (TesterConfigurationInfo)textBox.Tag;
				if (testerConfigurationInfo.PartOfUnitID && textBox.Text.Trim().Length == 0)
				{
					errorProvider1.SetError(textBox, testerConfigurationInfo.Name + " cannot be empty.");
					textBox.Focus();
					return false;
				}
				if (testerConfigurationInfo.RegEx.Trim() != "" && !ValidateString(textBox.Text, testerConfigurationInfo.RegEx))
				{
					if (testerConfigurationInfo.ErrorMsg != string.Empty)
					{
						errorProvider1.SetError(textBox, "RegEx: " + testerConfigurationInfo.RegEx + " " + testerConfigurationInfo.ErrorMsg);
					}
					else
					{
						errorProvider1.SetError(textBox, "Value does not match regular expression. RegEx: " + testerConfigurationInfo.RegEx + " " + testerConfigurationInfo.ErrorMsg);
					}
					textBox.Focus();
					return false;
				}
			}
			if (numberOfEmbeddedUnits > 1)
			{
				Hashtable hashtable = new Hashtable();
				foreach (Control control5 in panelEmbeddedSerialNumbers.Controls)
				{
					if (!(control5 is TextBox))
					{
						continue;
					}
					TextBox textBox2 = (TextBox)control5;
					if (textBox2.Text.Trim().Length == 0)
					{
						if (!naQuestionAsked)
						{
							DialogResult dialogResult = AppMessageDialog.Show(this, "\tSerial number cannot be empty.\nDo you want to put NA for the remaining Serial numbers?", "QSPR - Message Center", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, DialogResult.Yes, MessageDialogType.Warning);
							naQuestionAsked = true;
							if (dialogResult == DialogResult.Yes)
							{
								foreach (Control control6 in panelEmbeddedSerialNumbers.Controls)
								{
									if (control6 is TextBox)
									{
										TextBox textBox3 = (TextBox)control6;
										if (textBox3.Text.Trim().Length == 0)
										{
											textBox3.Text = "NA";
										}
									}
								}
								textBox2.Focus();
								return false;
							}
						}
						errorProvider1.SetError(textBox2, "Serial number cannot be empty.");
						textBox2.Focus();
						return false;
					}
					if (!(textBox2.Text.Trim().ToUpper() != "NA"))
					{
						continue;
					}
					if (hashtable.ContainsKey(textBox2.Text.Trim()))
					{
						errorProvider1.SetError(textBox2, "Serial number already found.");
						textBox2.Focus();
						return false;
					}
					if (snConfigInfo != null && snConfigInfo.RegEx.Trim() != "" && !ValidateString(textBox2.Text.Trim(), snConfigInfo.RegEx))
					{
						if (snConfigInfo.ErrorMsg != string.Empty)
						{
							errorProvider1.SetError(textBox2, snConfigInfo.ErrorMsg);
						}
						else
						{
							errorProvider1.SetError(textBox2, " Serail Number does not match the Regualr Expression  " + snConfigInfo.RegEx);
						}
						textBox2.Focus();
						return false;
					}
					hashtable.Add(textBox2.Text.Trim(), textBox2);
				}
			}
			else if (txtSerialNumber.Tag != null)
			{
				TesterConfigurationInfo testerConfigurationInfo2 = (TesterConfigurationInfo)txtSerialNumber.Tag;
				if (testerConfigurationInfo2 != null && testerConfigurationInfo2.RegEx.Trim() != "" && !ValidateString(txtSerialNumber.Text, testerConfigurationInfo2.RegEx))
				{
					if (testerConfigurationInfo2.ErrorMsg != string.Empty)
					{
						errorProvider1.SetError(txtSerialNumber, testerConfigurationInfo2.ErrorMsg);
					}
					else
					{
						errorProvider1.SetError(txtSerialNumber, "Value does not match regular expression " + testerConfigurationInfo2.ErrorMsg);
					}
					txtSerialNumber.Focus();
					return false;
				}
			}
			if (txtComments.Tag != null)
			{
				TesterConfigurationInfo testerConfigurationInfo3 = (TesterConfigurationInfo)txtComments.Tag;
				if (testerConfigurationInfo3.Value.Trim() != txtComments.Text.Trim())
				{
					testerConfigurationInfo3.Value = txtComments.Text.Trim();
					testerConfigurationInfo3.IncludeInTestResults = true;
				}
			}
			else
			{
				TesterConfigurationInfo testerConfigurationInfo4 = new TesterConfigurationInfo();
				testerConfigurationInfo4.Name = "Comments";
				testerConfigurationInfo4.Value = txtComments.Text;
				testerConfigurationInfo4.IncludeInTestResults = true;
				list.Add(testerConfigurationInfo4);
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "UpdateList:" + ex.Message, TraceMessageType.Exception);
			AppMessageDialog.Show(ex.Message, "Exception ", DialogResult.OK, MessageDialogType.Exception);
			return false;
		}
		return result;
	}

	private void GetSerialNo_Load(object sender, EventArgs e)
	{
	}

	private void ultraButtonOK_Click(object sender, EventArgs e)
	{
		if (txtSerialNumber.Text.Trim().Length != 0)
		{
			if (!IsListValid())
			{
				return;
			}
			if (numberOfEmbeddedUnits > 1 && numberOfEmbeddedUnits > 1)
			{
				string text = "";
				foreach (Control control in panelEmbeddedSerialNumbers.Controls)
				{
					if (control is TextBox)
					{
						TextBox textBox = (TextBox)control;
						text = ((!(text == "")) ? (text + "%^&" + textBox.Text.Trim()) : textBox.Text.Trim());
					}
				}
				SerialNo = text;
			}
			base.DialogResult = DialogResult.OK;
			Close();
		}
		else if (txtSerialNumber.Text.Trim().Length == 0)
		{
			errorProvider1.SetError(txtSerialNumber, "Serial Number cannot be Empty");
			txtSerialNumber.Focus();
		}
	}

	private void ultraButtonCancel_Click(object sender, EventArgs e)
	{
		base.DialogResult = DialogResult.Cancel;
		Close();
	}

	private void box_TextChanged(object sender, EventArgs e)
	{
		ComboBox comboBox = (ComboBox)sender;
		if (comboBox.Text.Trim().Length != 0)
		{
			errorProvider1.SetError(comboBox, "");
		}
		if (numberOfEmbeddedUnits == 1)
		{
			TesterConfigurationInfo testerConfigurationInfo = (TesterConfigurationInfo)comboBox.Tag;
			testerConfigurationInfo.Value = comboBox.Text;
			if (testerConfigurationInfo.PartOfUnitID)
			{
				FormUnitID();
			}
		}
	}

	private void txt_TextChanged(object sender, EventArgs e)
	{
		try
		{
			TextBox textBox = (TextBox)sender;
			if (textBox.Text.Trim().Length != 0)
			{
				errorProvider1.SetError(textBox, "");
			}
			if (textBox != txtSerialNumber && numberOfEmbeddedUnits == 1)
			{
				TesterConfigurationInfo testerConfigurationInfo = (TesterConfigurationInfo)textBox.Tag;
				testerConfigurationInfo.Value = textBox.Text;
				if (testerConfigurationInfo.PartOfUnitID)
				{
					FormUnitID();
				}
			}
		}
		catch
		{
		}
	}

	private void FormUnitID()
	{
		try
		{
			string text = "";
			foreach (TesterConfigurationInfo item in list)
			{
				if (item.PartOfUnitID)
				{
					if (text != "")
					{
						text += CONFIG_SEP;
					}
					text += item.Value;
				}
			}
			txtSerialNumber.Text = text;
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "FormUnitID " + ex.Message, TraceMessageType.Exception);
		}
	}

	private bool ValidateString(string InputText, string regExp)
	{
		Regex regex = new Regex(regExp);
		MatchCollection matchCollection = regex.Matches(InputText);
		for (int i = 0; i < matchCollection.Count; i++)
		{
			Match match = matchCollection[i];
			if (match.Value == InputText)
			{
				return true;
			}
		}
		return false;
	}
}
