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

namespace QC.SmartMatchApp;

public class EditKitValuesDialog : Form
{
	private List<string> refDesList = new List<string>();

	private Dictionary<string, KitValuesConverter> kitValues;

	private bool errorsExist;

	private List<string> removedItems = new List<string>();

	public List<string> RemovedItems = new List<string>();

	public List<string> AddedItems = new List<string>();

	private IContainer components;

	private TabControl tabCtrlRefDesKitVals;

	private TabPage tabPage1;

	private Button buttonOK;

	private Button buttonCancel;

	private Button btnAddCustomVals;

	private Button btnRemove;

	private KitValuesControl kitValuesControlDefault;

	public Dictionary<string, KitValuesConverter> KitValues => kitValues;

	public EditKitValuesDialog(List<string> refDesignators, Dictionary<string, KitValuesConverter> kitValuesDictionary, string selectedTab)
	{
		InitializeComponent();
		kitValues = kitValuesDictionary;
		refDesList = refDesignators;
		foreach (string key in kitValues.Keys)
		{
			if (string.IsNullOrWhiteSpace(key))
			{
				kitValuesControlDefault.ErrorsExistChanged += kitValuesCtrl_ErrorsExistChanged;
				kitValuesControlDefault.SetKitValues(kitValues[key]);
			}
			else
			{
				CreateNewTab(key);
			}
		}
		UpdateButtonStatus();
		tabCtrlRefDesKitVals.SelectTab(0);
		if (selectedTab != null && tabCtrlRefDesKitVals.TabPages.ContainsKey(selectedTab))
		{
			tabCtrlRefDesKitVals.SelectTab(selectedTab);
		}
	}

	private void kitValuesCtrl_ErrorsExistChanged(bool errorsExist)
	{
		this.errorsExist = errorsExist;
		UpdateButtonStatus();
	}

	private void dataGrid_DataError(object sender, DataGridViewDataErrorEventArgs e)
	{
	}

	private void UpdateButtonStatus()
	{
		if (refDesList.Count > 0)
		{
			List<string> currentKitValueNames = KitValues.Keys.ToList().ConvertAll((string t) => t.ToString());
			List<string> list = refDesList.Where((string t) => !currentKitValueNames.Contains(t)).ToList();
			btnAddCustomVals.Enabled = list.Count > 0;
		}
		else
		{
			btnAddCustomVals.Enabled = false;
		}
		btnRemove.Enabled = tabCtrlRefDesKitVals.SelectedTab != tabCtrlRefDesKitVals.TabPages[0];
		buttonOK.Enabled = !errorsExist;
	}

	private void CreateNewTab(string key)
	{
		tabCtrlRefDesKitVals.TabPages.Add(key, key);
		KitValuesControl kitValuesControl = new KitValuesControl(kitValues[key]);
		tabCtrlRefDesKitVals.TabPages[key].Controls.Add(kitValuesControl);
		kitValuesControl.Dock = DockStyle.Fill;
		tabCtrlRefDesKitVals.SelectedTab = tabCtrlRefDesKitVals.TabPages[key];
	}

	private void btnAddCustomVals_Click(object sender, EventArgs e)
	{
		List<string> currentKitValueNames = KitValues.Keys.ToList().ConvertAll((string t) => t.ToString());
		List<string> list = refDesList.Where((string t) => !currentKitValueNames.Contains(t)).ToList();
		AddKitValuesDialog addKitValuesDialog = new AddKitValuesDialog(list);
		if (addKitValuesDialog.ShowDialog() == DialogResult.OK)
		{
			string selectedItem = addKitValuesDialog.SelectedItem;
			kitValues.Add(selectedItem, new KitValuesConverter(selectedItem));
			CreateNewTab(selectedItem);
			AddedItems.Add(selectedItem);
		}
	}

	private void btnRemove_Click(object sender, EventArgs e)
	{
		string text = "Are you sure you want to delete the custom kit values for reference designator: " + tabCtrlRefDesKitVals.SelectedTab.Text + "?";
		if (MessageBox.Show(text, "Delete custom kit values?", MessageBoxButtons.YesNo) == DialogResult.Yes)
		{
			RemovedItems.Add(tabCtrlRefDesKitVals.SelectedTab.Text);
			kitValues.Remove(tabCtrlRefDesKitVals.SelectedTab.Text);
			tabCtrlRefDesKitVals.TabPages.Remove(tabCtrlRefDesKitVals.SelectedTab);
		}
	}

	private void tabCtrlRefDesKitVals_Selected(object sender, TabControlEventArgs e)
	{
		UpdateButtonStatus();
	}

	private void dialogResult_Click(object sender, EventArgs e)
	{
		kitValuesControlDefault.AllowErrors = true;
		foreach (TabPage tabPage in tabCtrlRefDesKitVals.TabPages)
		{
			KitValuesControl kitValuesControl = tabPage.Controls[0] as KitValuesControl;
			kitValuesControl.AllowErrors = true;
			kitValuesControl.DeleteEmptyRows();
		}
	}

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

	private void InitializeComponent()
	{
		this.tabCtrlRefDesKitVals = new System.Windows.Forms.TabControl();
		this.tabPage1 = new System.Windows.Forms.TabPage();
		this.buttonOK = new System.Windows.Forms.Button();
		this.buttonCancel = new System.Windows.Forms.Button();
		this.btnAddCustomVals = new System.Windows.Forms.Button();
		this.btnRemove = new System.Windows.Forms.Button();
		this.kitValuesControlDefault = new QC.SmartMatchApp.KitValuesControl();
		this.tabCtrlRefDesKitVals.SuspendLayout();
		this.tabPage1.SuspendLayout();
		base.SuspendLayout();
		this.tabCtrlRefDesKitVals.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.tabCtrlRefDesKitVals.Controls.Add(this.tabPage1);
		this.tabCtrlRefDesKitVals.Location = new System.Drawing.Point(13, 41);
		this.tabCtrlRefDesKitVals.Name = "tabCtrlRefDesKitVals";
		this.tabCtrlRefDesKitVals.SelectedIndex = 0;
		this.tabCtrlRefDesKitVals.Size = new System.Drawing.Size(308, 315);
		this.tabCtrlRefDesKitVals.TabIndex = 0;
		this.tabCtrlRefDesKitVals.Selected += new System.Windows.Forms.TabControlEventHandler(tabCtrlRefDesKitVals_Selected);
		this.tabPage1.Controls.Add(this.kitValuesControlDefault);
		this.tabPage1.Location = new System.Drawing.Point(4, 22);
		this.tabPage1.Name = "tabPage1";
		this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
		this.tabPage1.Size = new System.Drawing.Size(300, 289);
		this.tabPage1.TabIndex = 0;
		this.tabPage1.Text = "Default Kit Values";
		this.tabPage1.UseVisualStyleBackColor = true;
		this.buttonOK.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.buttonOK.DialogResult = System.Windows.Forms.DialogResult.OK;
		this.buttonOK.Location = new System.Drawing.Point(165, 362);
		this.buttonOK.Name = "buttonOK";
		this.buttonOK.Size = new System.Drawing.Size(75, 23);
		this.buttonOK.TabIndex = 1;
		this.buttonOK.Text = "&OK";
		this.buttonOK.UseVisualStyleBackColor = true;
		this.buttonOK.Click += new System.EventHandler(dialogResult_Click);
		this.buttonCancel.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		this.buttonCancel.Location = new System.Drawing.Point(246, 362);
		this.buttonCancel.Name = "buttonCancel";
		this.buttonCancel.Size = new System.Drawing.Size(75, 23);
		this.buttonCancel.TabIndex = 2;
		this.buttonCancel.Text = "&Cancel";
		this.buttonCancel.UseVisualStyleBackColor = true;
		this.buttonCancel.Click += new System.EventHandler(dialogResult_Click);
		this.btnAddCustomVals.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right;
		this.btnAddCustomVals.Location = new System.Drawing.Point(78, 12);
		this.btnAddCustomVals.Name = "btnAddCustomVals";
		this.btnAddCustomVals.Size = new System.Drawing.Size(115, 23);
		this.btnAddCustomVals.TabIndex = 3;
		this.btnAddCustomVals.Text = "Add";
		this.btnAddCustomVals.UseVisualStyleBackColor = true;
		this.btnAddCustomVals.Click += new System.EventHandler(btnAddCustomVals_Click);
		this.btnRemove.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right;
		this.btnRemove.Location = new System.Drawing.Point(199, 12);
		this.btnRemove.Name = "btnRemove";
		this.btnRemove.Size = new System.Drawing.Size(115, 23);
		this.btnRemove.TabIndex = 4;
		this.btnRemove.Text = "Remove";
		this.btnRemove.UseVisualStyleBackColor = true;
		this.btnRemove.Click += new System.EventHandler(btnRemove_Click);
		this.kitValuesControlDefault.AllowErrors = false;
		this.kitValuesControlDefault.Dock = System.Windows.Forms.DockStyle.Fill;
		this.kitValuesControlDefault.ErrorsExist = false;
		this.kitValuesControlDefault.Location = new System.Drawing.Point(3, 3);
		this.kitValuesControlDefault.Name = "kitValuesControlDefault";
		this.kitValuesControlDefault.Size = new System.Drawing.Size(294, 283);
		this.kitValuesControlDefault.TabIndex = 0;
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(341, 397);
		base.Controls.Add(this.btnRemove);
		base.Controls.Add(this.btnAddCustomVals);
		base.Controls.Add(this.buttonCancel);
		base.Controls.Add(this.buttonOK);
		base.Controls.Add(this.tabCtrlRefDesKitVals);
		base.MaximizeBox = false;
		base.MinimizeBox = false;
		this.MinimumSize = new System.Drawing.Size(326, 340);
		base.Name = "EditKitValuesDialog";
		base.ShowIcon = false;
		base.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
		this.Text = "Edit Kit Values";
		this.tabCtrlRefDesKitVals.ResumeLayout(false);
		this.tabPage1.ResumeLayout(false);
		base.ResumeLayout(false);
	}
}
