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

namespace QC.QTMApplication;

public class QSPRLoopConditionsConfigurator : Form
{
	public const string LOOP_CONDITION_SEPARATOR = ",";

	public const string LOOP_SEPARATOR = "_;_";

	private bool isLoopConditionsChanged;

	private string currentLoopConditions = string.Empty;

	private IContainer components;

	private Button btnCancel;

	private ListView lvLoopConditions;

	private TextBox txtNewLoopCondition;

	private Button btnAdd;

	private Button btnRemove;

	private Button btnSave;

	private ContextMenuStrip contextMenuQSPRLoopConditions;

	private ToolStripMenuItem selectAllToolStripMenuItem;

	private ToolStripMenuItem deselectAllToolStripMenuItem;

	private Panel panel1;

	private Panel panel2;

	private Panel panel3;

	private Panel panel4;

	public bool IsLoopConditionsChanged => isLoopConditionsChanged;

	public string ConfiguredLoopConditions
	{
		get
		{
			if (isLoopConditionsChanged)
			{
				currentLoopConditions = GetCurrentConfigureLoopConditions();
			}
			return currentLoopConditions;
		}
	}

	private string GetCurrentConfigureLoopConditions()
	{
		string[] array = new string[lvLoopConditions.Items.Count];
		for (int i = 0; i < lvLoopConditions.Items.Count; i++)
		{
			array[i] = lvLoopConditions.Items[i].Text + "_;_" + (lvLoopConditions.Items[i].Checked ? 1 : 0);
		}
		return string.Join(",", array);
	}

	public QSPRLoopConditionsConfigurator(string loopConditionsList)
	{
		InitializeComponent();
		currentLoopConditions = loopConditionsList;
		AddToListBox(loopConditionsList);
	}

	private void AddToListBox(string loopConditionsList)
	{
		lvLoopConditions.BeginUpdate();
		lvLoopConditions.Clear();
		string[] array = loopConditionsList.Split(new string[1] { "," }, StringSplitOptions.RemoveEmptyEntries);
		foreach (string text in array)
		{
			if (text.Contains("_;_"))
			{
				string[] array2 = text.Split(new string[1] { "_;_" }, StringSplitOptions.RemoveEmptyEntries);
				ListViewItem listViewItem = new ListViewItem(array2[0]);
				listViewItem.Name = array2[0];
				listViewItem.Checked = array2[1] == "1";
				lvLoopConditions.Items.Add(listViewItem);
			}
			else
			{
				ListViewItem listViewItem2 = new ListViewItem(text);
				listViewItem2.Name = text;
				listViewItem2.Checked = true;
				lvLoopConditions.Items.Add(listViewItem2);
			}
		}
		lvLoopConditions.EndUpdate();
	}

	private void btnAdd_Click(object sender, EventArgs e)
	{
		if (!lvLoopConditions.Items.ContainsKey(txtNewLoopCondition.Text))
		{
			ListViewItem listViewItem = new ListViewItem(txtNewLoopCondition.Text);
			listViewItem.Name = txtNewLoopCondition.Text;
			listViewItem.Checked = true;
			lvLoopConditions.Items.Insert(0, listViewItem);
		}
		else
		{
			MessageBox.Show(txtNewLoopCondition.Text + " already existed.");
		}
		txtNewLoopCondition.Text = string.Empty;
		isLoopConditionsChanged = true;
	}

	private void txtNewLoopCondition_TextChanged(object sender, EventArgs e)
	{
		btnAdd.Enabled = txtNewLoopCondition.Text.Length > 0;
	}

	private void lstLoopConditions_SelectedValueChanged(object sender, EventArgs e)
	{
		btnRemove.Enabled = lvLoopConditions.SelectedIndices.Count > 0;
	}

	private void btnRemove_Click(object sender, EventArgs e)
	{
		lvLoopConditions.BeginUpdate();
		object[] array = new object[lvLoopConditions.SelectedItems.Count];
		lvLoopConditions.SelectedItems.CopyTo(array, 0);
		for (int i = 0; i < array.Length; i++)
		{
			lvLoopConditions.Items[((ListViewItem)array[i]).Text].Remove();
		}
		lvLoopConditions.EndUpdate();
		isLoopConditionsChanged = true;
	}

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

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

	private void contextMenuQSPRLoopConditions_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
	{
		if (e.ClickedItem.Text == "Check All")
		{
			CheckUnCheckItems(selectFlag: true);
		}
		else if (e.ClickedItem.Text == "Uncheck All")
		{
			CheckUnCheckItems(selectFlag: false);
		}
	}

	private void CheckUnCheckItems(bool selectFlag)
	{
		for (int i = 0; i < lvLoopConditions.Items.Count; i++)
		{
			lvLoopConditions.Items[i].Checked = selectFlag;
		}
		isLoopConditionsChanged = true;
	}

	private void lvLoopConditions_AfterLabelEdit(object sender, LabelEditEventArgs e)
	{
		isLoopConditionsChanged = true;
	}

	private void lvLoopConditions_ItemChecked(object sender, ItemCheckedEventArgs e)
	{
		isLoopConditionsChanged = true;
	}

	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.btnCancel = new System.Windows.Forms.Button();
		this.lvLoopConditions = new System.Windows.Forms.ListView();
		this.contextMenuQSPRLoopConditions = new System.Windows.Forms.ContextMenuStrip(this.components);
		this.selectAllToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
		this.deselectAllToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
		this.txtNewLoopCondition = new System.Windows.Forms.TextBox();
		this.btnAdd = new System.Windows.Forms.Button();
		this.btnRemove = new System.Windows.Forms.Button();
		this.btnSave = new System.Windows.Forms.Button();
		this.panel1 = new System.Windows.Forms.Panel();
		this.panel2 = new System.Windows.Forms.Panel();
		this.panel4 = new System.Windows.Forms.Panel();
		this.panel3 = new System.Windows.Forms.Panel();
		this.contextMenuQSPRLoopConditions.SuspendLayout();
		this.panel1.SuspendLayout();
		this.panel2.SuspendLayout();
		this.panel4.SuspendLayout();
		this.panel3.SuspendLayout();
		base.SuspendLayout();
		this.btnCancel.Location = new System.Drawing.Point(169, 2);
		this.btnCancel.Margin = new System.Windows.Forms.Padding(2);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(62, 19);
		this.btnCancel.TabIndex = 0;
		this.btnCancel.Text = "Cancel";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
		this.lvLoopConditions.CheckBoxes = true;
		this.lvLoopConditions.ContextMenuStrip = this.contextMenuQSPRLoopConditions;
		this.lvLoopConditions.Dock = System.Windows.Forms.DockStyle.Fill;
		this.lvLoopConditions.FullRowSelect = true;
		this.lvLoopConditions.LabelEdit = true;
		this.lvLoopConditions.Location = new System.Drawing.Point(0, 0);
		this.lvLoopConditions.Margin = new System.Windows.Forms.Padding(2);
		this.lvLoopConditions.Name = "lvLoopConditions";
		this.lvLoopConditions.ShowGroups = false;
		this.lvLoopConditions.Size = new System.Drawing.Size(236, 214);
		this.lvLoopConditions.Sorting = System.Windows.Forms.SortOrder.Ascending;
		this.lvLoopConditions.TabIndex = 1;
		this.lvLoopConditions.UseCompatibleStateImageBehavior = false;
		this.lvLoopConditions.View = System.Windows.Forms.View.List;
		this.lvLoopConditions.ItemChecked += new System.Windows.Forms.ItemCheckedEventHandler(lvLoopConditions_ItemChecked);
		this.lvLoopConditions.AfterLabelEdit += new System.Windows.Forms.LabelEditEventHandler(lvLoopConditions_AfterLabelEdit);
		this.lvLoopConditions.SelectedIndexChanged += new System.EventHandler(lstLoopConditions_SelectedValueChanged);
		this.contextMenuQSPRLoopConditions.Items.AddRange(new System.Windows.Forms.ToolStripItem[2] { this.selectAllToolStripMenuItem, this.deselectAllToolStripMenuItem });
		this.contextMenuQSPRLoopConditions.Name = "contextMenuQSPRLoopConditions";
		this.contextMenuQSPRLoopConditions.Size = new System.Drawing.Size(129, 48);
		this.contextMenuQSPRLoopConditions.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(contextMenuQSPRLoopConditions_ItemClicked);
		this.selectAllToolStripMenuItem.Name = "selectAllToolStripMenuItem";
		this.selectAllToolStripMenuItem.Size = new System.Drawing.Size(128, 22);
		this.selectAllToolStripMenuItem.Text = "Check All";
		this.deselectAllToolStripMenuItem.Name = "deselectAllToolStripMenuItem";
		this.deselectAllToolStripMenuItem.Size = new System.Drawing.Size(128, 22);
		this.deselectAllToolStripMenuItem.Text = "Uncheck All";
		this.txtNewLoopCondition.Location = new System.Drawing.Point(5, 2);
		this.txtNewLoopCondition.Margin = new System.Windows.Forms.Padding(2);
		this.txtNewLoopCondition.Name = "txtNewLoopCondition";
		this.txtNewLoopCondition.Size = new System.Drawing.Size(143, 20);
		this.txtNewLoopCondition.TabIndex = 2;
		this.txtNewLoopCondition.TextChanged += new System.EventHandler(txtNewLoopCondition_TextChanged);
		this.btnAdd.Enabled = false;
		this.btnAdd.Location = new System.Drawing.Point(152, 2);
		this.btnAdd.Margin = new System.Windows.Forms.Padding(2);
		this.btnAdd.Name = "btnAdd";
		this.btnAdd.Size = new System.Drawing.Size(70, 19);
		this.btnAdd.TabIndex = 3;
		this.btnAdd.Text = "Add";
		this.btnAdd.UseVisualStyleBackColor = true;
		this.btnAdd.Click += new System.EventHandler(btnAdd_Click);
		this.btnRemove.Enabled = false;
		this.btnRemove.Location = new System.Drawing.Point(9, 2);
		this.btnRemove.Margin = new System.Windows.Forms.Padding(2);
		this.btnRemove.Name = "btnRemove";
		this.btnRemove.Size = new System.Drawing.Size(74, 19);
		this.btnRemove.TabIndex = 4;
		this.btnRemove.Text = "Remove";
		this.btnRemove.UseVisualStyleBackColor = true;
		this.btnRemove.Click += new System.EventHandler(btnRemove_Click);
		this.btnSave.Location = new System.Drawing.Point(89, 2);
		this.btnSave.Name = "btnSave";
		this.btnSave.Size = new System.Drawing.Size(74, 19);
		this.btnSave.TabIndex = 5;
		this.btnSave.Text = "Save";
		this.btnSave.UseVisualStyleBackColor = true;
		this.btnSave.Click += new System.EventHandler(btnSave_Click);
		this.panel1.Controls.Add(this.btnCancel);
		this.panel1.Controls.Add(this.btnRemove);
		this.panel1.Controls.Add(this.btnSave);
		this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
		this.panel1.Location = new System.Drawing.Point(0, 239);
		this.panel1.Name = "panel1";
		this.panel1.Size = new System.Drawing.Size(236, 25);
		this.panel1.TabIndex = 6;
		this.panel2.Controls.Add(this.panel4);
		this.panel2.Controls.Add(this.panel3);
		this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
		this.panel2.Location = new System.Drawing.Point(0, 0);
		this.panel2.Name = "panel2";
		this.panel2.Size = new System.Drawing.Size(236, 239);
		this.panel2.TabIndex = 7;
		this.panel4.Controls.Add(this.lvLoopConditions);
		this.panel4.Dock = System.Windows.Forms.DockStyle.Fill;
		this.panel4.Location = new System.Drawing.Point(0, 25);
		this.panel4.Name = "panel4";
		this.panel4.Size = new System.Drawing.Size(236, 214);
		this.panel4.TabIndex = 9;
		this.panel3.Controls.Add(this.btnAdd);
		this.panel3.Controls.Add(this.txtNewLoopCondition);
		this.panel3.Dock = System.Windows.Forms.DockStyle.Top;
		this.panel3.Location = new System.Drawing.Point(0, 0);
		this.panel3.Name = "panel3";
		this.panel3.Size = new System.Drawing.Size(236, 25);
		this.panel3.TabIndex = 8;
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(236, 264);
		base.Controls.Add(this.panel2);
		base.Controls.Add(this.panel1);
		base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
		base.Margin = new System.Windows.Forms.Padding(2);
		base.MinimizeBox = false;
		base.Name = "QSPRLoopConditionsConfigurator";
		base.ShowIcon = false;
		base.ShowInTaskbar = false;
		base.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
		this.Text = "Configure QSPR Loop Conditons";
		this.contextMenuQSPRLoopConditions.ResumeLayout(false);
		this.panel1.ResumeLayout(false);
		this.panel2.ResumeLayout(false);
		this.panel4.ResumeLayout(false);
		this.panel3.ResumeLayout(false);
		this.panel3.PerformLayout();
		base.ResumeLayout(false);
	}
}
