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

namespace QC.SmartMatchApp;

public class KitValuesControl : UserControl
{
	public delegate void ErrorsExistChangedDelegate(bool errorsExist);

	private bool errorsExist;

	private IContainer components;

	private SplitContainer splitContainer1;

	private DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;

	private DataGridViewTextBoxColumn ColumnCapacitor;

	public DataGridView dataGridCapacitorValues;

	public DataGridView dataGridInductorValues;

	private SplitContainer splitContainer2;

	private Button btnDeleteCapVal;

	private SplitContainer splitContainer3;

	private Button btnDeleteIndVal;

	public bool AllowErrors { get; set; }

	public bool ErrorsExist
	{
		get
		{
			return errorsExist;
		}
		set
		{
			errorsExist = value;
			if (this.ErrorsExistChanged != null)
			{
				this.ErrorsExistChanged(value);
			}
		}
	}

	public event ErrorsExistChangedDelegate ErrorsExistChanged;

	public KitValuesControl()
	{
		InitializeComponent();
		AllowErrors = false;
	}

	public KitValuesControl(KitValuesConverter kitValues)
	{
		InitializeComponent();
		AllowErrors = false;
		SetKitValues(kitValues);
	}

	public void SetKitValues(KitValuesConverter kitValues)
	{
		dataGridCapacitorValues.Columns.Clear();
		dataGridInductorValues.Columns.Clear();
		dataGridCapacitorValues.DataSource = kitValues.KitValCapDataTable;
		dataGridInductorValues.DataSource = kitValues.KitValIndDataTable;
		dataGridCapacitorValues.CellValidating += dataGridCapacitorValues_CellValidating;
		dataGridInductorValues.CellValidating += dataGridInductorValues_CellValidating;
		dataGridCapacitorValues.SelectionChanged += dataGridCapacitorValues_SelectionChanged;
		dataGridInductorValues.SelectionChanged += dataGridInductorValues_SelectionChanged;
		dataGridCapacitorValues.DataError += dataGridCapacitorValues_DataError;
		dataGridInductorValues.DataError += dataGridInductorValues_DataError;
	}

	public void DeleteEmptyRows()
	{
		for (int num = dataGridCapacitorValues.Rows.Count - 1; num >= 0; num--)
		{
			DataGridViewRow dataGridViewRow = dataGridCapacitorValues.Rows[num];
			if (dataGridViewRow.Cells[0].Value == DBNull.Value)
			{
				dataGridCapacitorValues.Rows.Remove(dataGridViewRow);
			}
		}
		for (int num2 = dataGridInductorValues.Rows.Count - 1; num2 >= 0; num2--)
		{
			DataGridViewRow dataGridViewRow2 = dataGridInductorValues.Rows[num2];
			if (dataGridViewRow2.Cells[0].Value == DBNull.Value)
			{
				dataGridInductorValues.Rows.Remove(dataGridViewRow2);
			}
		}
	}

	private void dataGridCapacitorValues_SelectionChanged(object sender, EventArgs e)
	{
		btnDeleteCapVal.Enabled = dataGridCapacitorValues.SelectedRows.Count > 0;
	}

	private void dataGridInductorValues_SelectionChanged(object sender, EventArgs e)
	{
		btnDeleteIndVal.Enabled = dataGridInductorValues.SelectedRows.Count > 0;
	}

	private void dataGridCapacitorValues_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
	{
		if (AllowErrors)
		{
			return;
		}
		dataGridCapacitorValues.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange);
		if (dataGridCapacitorValues[e.ColumnIndex, e.RowIndex].Value != DBNull.Value)
		{
			if (!(dataGridCapacitorValues[e.ColumnIndex, e.RowIndex].Value is double))
			{
				DisplayInvalidDataMessage(isCapTable: true, e.RowIndex);
				e.Cancel = true;
				ErrorsExist = true;
			}
			else
			{
				ErrorsExist = false;
			}
		}
	}

	private void dataGridInductorValues_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
	{
		if (!AllowErrors && dataGridInductorValues[e.ColumnIndex, e.RowIndex].Value != DBNull.Value)
		{
			dataGridInductorValues.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange);
			if (!(dataGridInductorValues[e.ColumnIndex, e.RowIndex].Value is double))
			{
				DisplayInvalidDataMessage(isCapTable: true, e.RowIndex);
				e.Cancel = true;
				ErrorsExist = true;
			}
			else
			{
				ErrorsExist = false;
			}
		}
	}

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

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

	private void DisplayInvalidDataMessage(bool isCapTable, int row)
	{
		string format = "Invalid {0} Value entered at Row {1}";
		string arg = "Capacitor";
		if (!isCapTable)
		{
			arg = "Inductor";
		}
		MessageBox.Show(string.Format(format, arg, row + 1));
	}

	private void btnDeleteCapVal_Click(object sender, EventArgs e)
	{
		List<DataGridViewRow> source = dataGridCapacitorValues.SelectedRows.Cast<DataGridViewRow>().ToList();
		source = source.OrderByDescending((DataGridViewRow t) => t.Index).ToList();
		foreach (DataGridViewRow item in source)
		{
			if (dataGridCapacitorValues.Rows.IndexOf(item) <= dataGridCapacitorValues.Rows.Count - 1)
			{
				dataGridCapacitorValues.Rows.Remove(item);
			}
		}
	}

	private void btnDeleteIndVal_Click(object sender, EventArgs e)
	{
		List<DataGridViewRow> source = dataGridInductorValues.SelectedRows.Cast<DataGridViewRow>().ToList();
		source = source.OrderByDescending((DataGridViewRow t) => t.Index).ToList();
		foreach (DataGridViewRow item in source)
		{
			if (dataGridInductorValues.Rows.IndexOf(item) <= dataGridInductorValues.Rows.Count - 1)
			{
				dataGridInductorValues.Rows.Remove(item);
			}
		}
	}

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

	private void InitializeComponent()
	{
		System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle = new System.Windows.Forms.DataGridViewCellStyle();
		System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
		System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle();
		System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle4 = new System.Windows.Forms.DataGridViewCellStyle();
		this.splitContainer1 = new System.Windows.Forms.SplitContainer();
		this.dataGridCapacitorValues = new System.Windows.Forms.DataGridView();
		this.dataGridViewTextBoxColumn1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.dataGridInductorValues = new System.Windows.Forms.DataGridView();
		this.ColumnCapacitor = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.splitContainer2 = new System.Windows.Forms.SplitContainer();
		this.btnDeleteCapVal = new System.Windows.Forms.Button();
		this.splitContainer3 = new System.Windows.Forms.SplitContainer();
		this.btnDeleteIndVal = new System.Windows.Forms.Button();
		((System.ComponentModel.ISupportInitialize)this.splitContainer1).BeginInit();
		this.splitContainer1.Panel1.SuspendLayout();
		this.splitContainer1.Panel2.SuspendLayout();
		this.splitContainer1.SuspendLayout();
		((System.ComponentModel.ISupportInitialize)this.dataGridCapacitorValues).BeginInit();
		((System.ComponentModel.ISupportInitialize)this.dataGridInductorValues).BeginInit();
		((System.ComponentModel.ISupportInitialize)this.splitContainer2).BeginInit();
		this.splitContainer2.Panel1.SuspendLayout();
		this.splitContainer2.Panel2.SuspendLayout();
		this.splitContainer2.SuspendLayout();
		((System.ComponentModel.ISupportInitialize)this.splitContainer3).BeginInit();
		this.splitContainer3.Panel1.SuspendLayout();
		this.splitContainer3.Panel2.SuspendLayout();
		this.splitContainer3.SuspendLayout();
		base.SuspendLayout();
		this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
		this.splitContainer1.Location = new System.Drawing.Point(0, 0);
		this.splitContainer1.Name = "splitContainer1";
		this.splitContainer1.Panel1.Controls.Add(this.splitContainer2);
		this.splitContainer1.Panel2.Controls.Add(this.splitContainer3);
		this.splitContainer1.Size = new System.Drawing.Size(226, 240);
		this.splitContainer1.SplitterDistance = 111;
		this.splitContainer1.TabIndex = 0;
		this.dataGridCapacitorValues.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
		dataGridViewCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
		dataGridViewCellStyle.BackColor = System.Drawing.SystemColors.Control;
		dataGridViewCellStyle.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		dataGridViewCellStyle.ForeColor = System.Drawing.SystemColors.WindowText;
		dataGridViewCellStyle.SelectionBackColor = System.Drawing.SystemColors.Highlight;
		dataGridViewCellStyle.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
		dataGridViewCellStyle.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
		this.dataGridCapacitorValues.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle;
		this.dataGridCapacitorValues.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
		this.dataGridCapacitorValues.Columns.AddRange(this.dataGridViewTextBoxColumn1);
		dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
		dataGridViewCellStyle2.BackColor = System.Drawing.SystemColors.Window;
		dataGridViewCellStyle2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		dataGridViewCellStyle2.ForeColor = System.Drawing.SystemColors.ControlText;
		dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
		dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
		dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
		this.dataGridCapacitorValues.DefaultCellStyle = dataGridViewCellStyle2;
		this.dataGridCapacitorValues.Dock = System.Windows.Forms.DockStyle.Fill;
		this.dataGridCapacitorValues.Location = new System.Drawing.Point(0, 0);
		this.dataGridCapacitorValues.Name = "dataGridCapacitorValues";
		this.dataGridCapacitorValues.Size = new System.Drawing.Size(111, 205);
		this.dataGridCapacitorValues.TabIndex = 2;
		this.dataGridViewTextBoxColumn1.HeaderText = "Capacitor Values";
		this.dataGridViewTextBoxColumn1.Name = "dataGridViewTextBoxColumn1";
		this.dataGridInductorValues.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
		dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
		dataGridViewCellStyle3.BackColor = System.Drawing.SystemColors.Control;
		dataGridViewCellStyle3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		dataGridViewCellStyle3.ForeColor = System.Drawing.SystemColors.WindowText;
		dataGridViewCellStyle3.SelectionBackColor = System.Drawing.SystemColors.Highlight;
		dataGridViewCellStyle3.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
		dataGridViewCellStyle3.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
		this.dataGridInductorValues.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle3;
		this.dataGridInductorValues.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
		this.dataGridInductorValues.Columns.AddRange(this.ColumnCapacitor);
		dataGridViewCellStyle4.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
		dataGridViewCellStyle4.BackColor = System.Drawing.SystemColors.Window;
		dataGridViewCellStyle4.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		dataGridViewCellStyle4.ForeColor = System.Drawing.SystemColors.ControlText;
		dataGridViewCellStyle4.SelectionBackColor = System.Drawing.SystemColors.Highlight;
		dataGridViewCellStyle4.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
		dataGridViewCellStyle4.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
		this.dataGridInductorValues.DefaultCellStyle = dataGridViewCellStyle4;
		this.dataGridInductorValues.Dock = System.Windows.Forms.DockStyle.Fill;
		this.dataGridInductorValues.Location = new System.Drawing.Point(0, 0);
		this.dataGridInductorValues.Name = "dataGridInductorValues";
		this.dataGridInductorValues.Size = new System.Drawing.Size(111, 205);
		this.dataGridInductorValues.TabIndex = 1;
		this.ColumnCapacitor.HeaderText = "Inductor Values";
		this.ColumnCapacitor.Name = "ColumnCapacitor";
		this.splitContainer2.Dock = System.Windows.Forms.DockStyle.Fill;
		this.splitContainer2.FixedPanel = System.Windows.Forms.FixedPanel.Panel2;
		this.splitContainer2.IsSplitterFixed = true;
		this.splitContainer2.Location = new System.Drawing.Point(0, 0);
		this.splitContainer2.Name = "splitContainer2";
		this.splitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal;
		this.splitContainer2.Panel1.Controls.Add(this.dataGridCapacitorValues);
		this.splitContainer2.Panel2.Controls.Add(this.btnDeleteCapVal);
		this.splitContainer2.Size = new System.Drawing.Size(111, 240);
		this.splitContainer2.SplitterDistance = 205;
		this.splitContainer2.TabIndex = 3;
		this.btnDeleteCapVal.Dock = System.Windows.Forms.DockStyle.Fill;
		this.btnDeleteCapVal.Location = new System.Drawing.Point(0, 0);
		this.btnDeleteCapVal.Name = "btnDeleteCapVal";
		this.btnDeleteCapVal.Size = new System.Drawing.Size(111, 31);
		this.btnDeleteCapVal.TabIndex = 0;
		this.btnDeleteCapVal.Text = "Delete Value";
		this.btnDeleteCapVal.UseVisualStyleBackColor = true;
		this.btnDeleteCapVal.Click += new System.EventHandler(btnDeleteCapVal_Click);
		this.splitContainer3.Dock = System.Windows.Forms.DockStyle.Fill;
		this.splitContainer3.FixedPanel = System.Windows.Forms.FixedPanel.Panel2;
		this.splitContainer3.IsSplitterFixed = true;
		this.splitContainer3.Location = new System.Drawing.Point(0, 0);
		this.splitContainer3.Name = "splitContainer3";
		this.splitContainer3.Orientation = System.Windows.Forms.Orientation.Horizontal;
		this.splitContainer3.Panel1.Controls.Add(this.dataGridInductorValues);
		this.splitContainer3.Panel2.Controls.Add(this.btnDeleteIndVal);
		this.splitContainer3.Size = new System.Drawing.Size(111, 240);
		this.splitContainer3.SplitterDistance = 205;
		this.splitContainer3.TabIndex = 0;
		this.btnDeleteIndVal.Dock = System.Windows.Forms.DockStyle.Fill;
		this.btnDeleteIndVal.Location = new System.Drawing.Point(0, 0);
		this.btnDeleteIndVal.Name = "btnDeleteIndVal";
		this.btnDeleteIndVal.Size = new System.Drawing.Size(111, 31);
		this.btnDeleteIndVal.TabIndex = 1;
		this.btnDeleteIndVal.Text = "Delete Value";
		this.btnDeleteIndVal.UseVisualStyleBackColor = true;
		this.btnDeleteIndVal.Click += new System.EventHandler(btnDeleteIndVal_Click);
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.Controls.Add(this.splitContainer1);
		base.Name = "KitValuesControl";
		base.Size = new System.Drawing.Size(226, 240);
		this.splitContainer1.Panel1.ResumeLayout(false);
		this.splitContainer1.Panel2.ResumeLayout(false);
		((System.ComponentModel.ISupportInitialize)this.splitContainer1).EndInit();
		this.splitContainer1.ResumeLayout(false);
		((System.ComponentModel.ISupportInitialize)this.dataGridCapacitorValues).EndInit();
		((System.ComponentModel.ISupportInitialize)this.dataGridInductorValues).EndInit();
		this.splitContainer2.Panel1.ResumeLayout(false);
		this.splitContainer2.Panel2.ResumeLayout(false);
		((System.ComponentModel.ISupportInitialize)this.splitContainer2).EndInit();
		this.splitContainer2.ResumeLayout(false);
		this.splitContainer3.Panel1.ResumeLayout(false);
		this.splitContainer3.Panel2.ResumeLayout(false);
		((System.ComponentModel.ISupportInitialize)this.splitContainer3).EndInit();
		this.splitContainer3.ResumeLayout(false);
		base.ResumeLayout(false);
	}
}
