using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace QDART_MFG_CfgEditor;

public class MultiplexerAddForm : Form
{
	private SortedList<int, int> slRes = new SortedList<int, int>();

	private SortedList<int, SortedList<int, int>> slResDutGroupMap = new SortedList<int, SortedList<int, int>>();

	private SortedList<int, int> slGroupMap = new SortedList<int, int>();

	private Common.EditTypes editType;

	private IContainer components;

	private Label lbtxt;

	private Button btnCancel;

	private Button btnOK;

	private ComboBox cbId;

	private Label lbErr;

	public MultiplexerAddForm(Common.EditTypes et, SortedList<int, int> sl)
	{
		InitializeComponent();
		slRes = sl;
		editType = et;
		InitControls(et);
	}

	public MultiplexerAddForm(Common.EditTypes et, SortedList<int, int> sl, SortedList<int, SortedList<int, int>> slDutGroupMap, SortedList<int, int> slGroup)
	{
		InitializeComponent();
		slRes = sl;
		slResDutGroupMap = slDutGroupMap;
		slGroupMap = slGroup;
		editType = et;
		InitControls(et);
	}

	private void InitControls(Common.EditTypes et)
	{
		switch (et)
		{
		case Common.EditTypes.GROUP:
			lbtxt.Text = "Group ID:";
			Text = "Add new GROUP";
			break;
		case Common.EditTypes.DUT:
			lbtxt.Text = "DUT ID:";
			Text = "Add new DUT";
			break;
		case Common.EditTypes.MULTIPLEXER:
			lbtxt.Text = "MULTIPLEXER ID:";
			Text = "Add new MULTIPLEXER";
			break;
		}
	}

	private void MultiplexerAddForm_Load(object sender, EventArgs e)
	{
		for (int i = 1; i < 17; i++)
		{
			cbId.Items.Add(i.ToString());
		}
	}

	private void btnOK_Click(object sender, EventArgs e)
	{
		if (cbId.Text != "")
		{
			int key = int.Parse(cbId.Text);
			if (!slRes.ContainsKey(key))
			{
				base.Tag = key.ToString();
				base.DialogResult = DialogResult.OK;
				return;
			}
			switch (editType)
			{
			case Common.EditTypes.GROUP:
				lbErr.Visible = true;
				lbErr.Text = "GROUP ID: " + cbId.Text + " already exists";
				break;
			case Common.EditTypes.DUT:
				foreach (int key2 in slResDutGroupMap.Keys)
				{
					foreach (int key3 in slResDutGroupMap[key2].Keys)
					{
						if (!(key3.ToString() == cbId.Text))
						{
							continue;
						}
						foreach (int key4 in slGroupMap.Keys)
						{
							if (slGroupMap[key4] == key2)
							{
								lbErr.Text = "DUT: " + cbId.Text + " in GROUP: " + key4 + " already exists";
								break;
							}
						}
					}
				}
				lbErr.Visible = true;
				break;
			case Common.EditTypes.MULTIPLEXER:
				lbErr.Visible = true;
				lbErr.Text = "MULTIPLEXER ID: " + cbId.Text + " already exists";
				break;
			}
		}
		else
		{
			MessageBox.Show("Select ID first");
		}
	}

	private void btnCancel_Click(object sender, EventArgs e)
	{
		Close();
	}

	private void cbId_SelectedIndexChanged(object sender, EventArgs e)
	{
		lbErr.Visible = false;
	}

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

	private void InitializeComponent()
	{
		this.lbtxt = new System.Windows.Forms.Label();
		this.btnCancel = new System.Windows.Forms.Button();
		this.btnOK = new System.Windows.Forms.Button();
		this.cbId = new System.Windows.Forms.ComboBox();
		this.lbErr = new System.Windows.Forms.Label();
		base.SuspendLayout();
		this.lbtxt.AutoSize = true;
		this.lbtxt.Font = new System.Drawing.Font("Microsoft Sans Serif", 12f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		this.lbtxt.Location = new System.Drawing.Point(38, 37);
		this.lbtxt.Name = "lbtxt";
		this.lbtxt.Size = new System.Drawing.Size(84, 20);
		this.lbtxt.TabIndex = 13;
		this.lbtxt.Text = "To Group: ";
		this.btnCancel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		this.btnCancel.Location = new System.Drawing.Point(186, 117);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(80, 37);
		this.btnCancel.TabIndex = 10;
		this.btnCancel.Text = "Cancel";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
		this.btnOK.Font = new System.Drawing.Font("Microsoft Sans Serif", 12f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		this.btnOK.Location = new System.Drawing.Point(69, 117);
		this.btnOK.Name = "btnOK";
		this.btnOK.Size = new System.Drawing.Size(80, 37);
		this.btnOK.TabIndex = 11;
		this.btnOK.Text = "OK";
		this.btnOK.UseVisualStyleBackColor = true;
		this.btnOK.Click += new System.EventHandler(btnOK_Click);
		this.cbId.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.cbId.FormattingEnabled = true;
		this.cbId.Location = new System.Drawing.Point(186, 39);
		this.cbId.Name = "cbId";
		this.cbId.Size = new System.Drawing.Size(93, 21);
		this.cbId.TabIndex = 14;
		this.cbId.SelectedIndexChanged += new System.EventHandler(cbId_SelectedIndexChanged);
		this.lbErr.AutoSize = true;
		this.lbErr.Font = new System.Drawing.Font("Microsoft Sans Serif", 12f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		this.lbErr.ForeColor = System.Drawing.Color.Red;
		this.lbErr.Location = new System.Drawing.Point(39, 77);
		this.lbErr.Name = "lbErr";
		this.lbErr.Size = new System.Drawing.Size(117, 20);
		this.lbErr.TabIndex = 15;
		this.lbErr.Text = "ID already exist";
		this.lbErr.Visible = false;
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(331, 173);
		base.Controls.Add(this.lbErr);
		base.Controls.Add(this.cbId);
		base.Controls.Add(this.lbtxt);
		base.Controls.Add(this.btnCancel);
		base.Controls.Add(this.btnOK);
		base.Name = "MultiplexerAddForm";
		base.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
		this.Text = "MultiplexerAddForm";
		base.Load += new System.EventHandler(MultiplexerAddForm_Load);
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
