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

namespace QC.SmartMatchApp;

public class EditBandChannels : Form
{
	public Dictionary<string, string[]> UserSelectedChannels = new Dictionary<string, string[]>();

	private Dictionary<string, string[]> DefaultChannels = new Dictionary<string, string[]>();

	private Dictionary<string, string> MandatoryChannels = new Dictionary<string, string>();

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

	private bool ignoreChanges;

	private IContainer components;

	private ListView lvBandChannels;

	private ComboBox cbBandSelect;

	private Label label1;

	private Button btnOK;

	private Button btnCancel;

	private Button btnSelectAll;

	private Button btnDeselectAll;

	public EditBandChannels(List<string> bands, Dictionary<string, string[]> userSelectedChannels, Dictionary<string, string[]> defaultChannels, Dictionary<string, string> mandatoryChannels)
	{
		InitializeComponent();
		DefaultChannels = defaultChannels;
		MandatoryChannels = mandatoryChannels;
		foreach (string band in bands)
		{
			if (!userSelectedChannels.ContainsKey(band))
			{
				UserSelectedChannels.Add(band, defaultChannels[band].Clone() as string[]);
			}
			else
			{
				UserSelectedChannels.Add(band, userSelectedChannels[band].Clone() as string[]);
			}
		}
		foreach (string band2 in bands)
		{
			cbBandSelect.Items.Add(band2);
		}
		cbBandSelect.SelectedIndex = 0;
		base.Load += EditBandChannels_Load;
	}

	private void EditBandChannels_Load(object sender, EventArgs e)
	{
		lvBandChannels.ItemChecked += lvBandChannels_ItemChecked;
	}

	private void lvBandChannels_ItemChecked(object sender, ItemCheckedEventArgs e)
	{
		if (ignoreChanges)
		{
			return;
		}
		string text = cbBandSelect.SelectedItem.ToString();
		List<string> list = new List<string>();
		foreach (ListViewItem checkedItem in lvBandChannels.CheckedItems)
		{
			list.Add(checkedItem.Text);
		}
		UserSelectedChannels[text] = list.ToArray();
		if (!BandsChanged.Contains(text))
		{
			BandsChanged.Add(text);
		}
	}

	private void cbBandSelect_SelectedValueChanged(object sender, EventArgs e)
	{
		ignoreChanges = true;
		string key = cbBandSelect.SelectedItem.ToString();
		string[] array = DefaultChannels[key];
		lvBandChannels.Clear();
		string[] array2 = array;
		foreach (string value in array2)
		{
			ListViewItem listViewItem = lvBandChannels.Items.Add(value);
			if (UserSelectedChannels[key].Contains(value))
			{
				listViewItem.Checked = true;
			}
		}
		ignoreChanges = false;
	}

	private void btnSelectAll_Click(object sender, EventArgs e)
	{
		foreach (ListViewItem item in lvBandChannels.Items)
		{
			item.Checked = true;
		}
	}

	private void btnDeselectAll_Click(object sender, EventArgs e)
	{
		foreach (ListViewItem item in lvBandChannels.Items)
		{
			item.Checked = false;
		}
	}

	private void EditBandChannels_FormClosing(object sender, FormClosingEventArgs e)
	{
		if (base.DialogResult != DialogResult.OK)
		{
			return;
		}
		List<string> list = new List<string>();
		foreach (object item in cbBandSelect.Items)
		{
			string text = item.ToString();
			string text2 = MandatoryChannels[text];
			if (!UserSelectedChannels[text].Contains(text2))
			{
				List<string> list2 = UserSelectedChannels[text].ToList();
				list2.Add(text2);
				UserSelectedChannels[text] = list2.ToArray();
				list.Add(text2);
				if (!BandsChanged.Contains(text))
				{
					BandsChanged.Add(text);
				}
			}
		}
		if (list.Count > 0)
		{
			string text3 = string.Join(", ", list);
			MessageBox.Show("The following reference channel(s) were unselected in their respective bands: " + text3 + "." + Environment.NewLine + "These channels will be automatically selected.");
		}
	}

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

	private void InitializeComponent()
	{
		this.lvBandChannels = new System.Windows.Forms.ListView();
		this.cbBandSelect = new System.Windows.Forms.ComboBox();
		this.label1 = new System.Windows.Forms.Label();
		this.btnOK = new System.Windows.Forms.Button();
		this.btnCancel = new System.Windows.Forms.Button();
		this.btnSelectAll = new System.Windows.Forms.Button();
		this.btnDeselectAll = new System.Windows.Forms.Button();
		base.SuspendLayout();
		this.lvBandChannels.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.lvBandChannels.CheckBoxes = true;
		this.lvBandChannels.Location = new System.Drawing.Point(12, 79);
		this.lvBandChannels.Name = "lvBandChannels";
		this.lvBandChannels.Size = new System.Drawing.Size(214, 146);
		this.lvBandChannels.TabIndex = 0;
		this.lvBandChannels.UseCompatibleStateImageBehavior = false;
		this.lvBandChannels.View = System.Windows.Forms.View.List;
		this.cbBandSelect.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.cbBandSelect.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.cbBandSelect.FormattingEnabled = true;
		this.cbBandSelect.Location = new System.Drawing.Point(56, 10);
		this.cbBandSelect.Name = "cbBandSelect";
		this.cbBandSelect.Size = new System.Drawing.Size(170, 21);
		this.cbBandSelect.TabIndex = 1;
		this.cbBandSelect.SelectedValueChanged += new System.EventHandler(cbBandSelect_SelectedValueChanged);
		this.label1.AutoSize = true;
		this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		this.label1.Location = new System.Drawing.Point(12, 11);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(40, 16);
		this.label1.TabIndex = 2;
		this.label1.Text = "Band";
		this.btnOK.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
		this.btnOK.Location = new System.Drawing.Point(56, 231);
		this.btnOK.Name = "btnOK";
		this.btnOK.Size = new System.Drawing.Size(87, 32);
		this.btnOK.TabIndex = 3;
		this.btnOK.Text = "OK";
		this.btnOK.UseVisualStyleBackColor = true;
		this.btnCancel.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		this.btnCancel.Location = new System.Drawing.Point(152, 231);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(87, 32);
		this.btnCancel.TabIndex = 4;
		this.btnCancel.Text = "Cancel";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.btnSelectAll.Location = new System.Drawing.Point(15, 50);
		this.btnSelectAll.Name = "btnSelectAll";
		this.btnSelectAll.Size = new System.Drawing.Size(75, 23);
		this.btnSelectAll.TabIndex = 5;
		this.btnSelectAll.Text = "Select All";
		this.btnSelectAll.UseVisualStyleBackColor = true;
		this.btnSelectAll.Click += new System.EventHandler(btnSelectAll_Click);
		this.btnDeselectAll.Location = new System.Drawing.Point(96, 50);
		this.btnDeselectAll.Name = "btnDeselectAll";
		this.btnDeselectAll.Size = new System.Drawing.Size(75, 23);
		this.btnDeselectAll.TabIndex = 6;
		this.btnDeselectAll.Text = "Deselect All";
		this.btnDeselectAll.UseVisualStyleBackColor = true;
		this.btnDeselectAll.Click += new System.EventHandler(btnDeselectAll_Click);
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(251, 275);
		base.ControlBox = false;
		base.Controls.Add(this.btnDeselectAll);
		base.Controls.Add(this.btnSelectAll);
		base.Controls.Add(this.btnCancel);
		base.Controls.Add(this.btnOK);
		base.Controls.Add(this.label1);
		base.Controls.Add(this.cbBandSelect);
		base.Controls.Add(this.lvBandChannels);
		base.Name = "EditBandChannels";
		base.ShowIcon = false;
		base.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
		this.Text = "Edit Band Channels";
		base.FormClosing += new System.Windows.Forms.FormClosingEventHandler(EditBandChannels_FormClosing);
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
