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

namespace QC.GlobalVariableManager;

public class QTMGlobalVariablesDialog : Form
{
	public const string SELECTION = "Selection";

	public const string NAME = "Name";

	public const string VALUE = "Value";

	public const string STRING = "String";

	public const string OBJECT = "Object";

	public const string DEFAULT_VALUE = "";

	private DataTable gridTable = new DataTable();

	private bool invalidating;

	private string currentSelection = "";

	private IContainer components;

	private DataGridView gvGrid;

	private Button btnOK;

	private Button btnCancel;

	private Panel panelGrid;

	private Button btnAdd;

	private Button btnRemove;

	private Panel panelButtons;

	public string CurrentSelection
	{
		get
		{
			return currentSelection;
		}
		set
		{
			currentSelection = value;
		}
	}

	public string AssociatedObjectName
	{
		set
		{
			Text = "Global Variable Manager - " + value;
		}
	}

	public QTMGlobalVariablesDialog()
	{
		InitializeComponent();
		CreateTable();
	}

	private void CreateTable()
	{
		try
		{
			DataColumn dataColumn = new DataColumn("Selection");
			dataColumn.DataType = Type.GetType("System.Boolean");
			dataColumn.DefaultValue = false;
			DataColumn dataColumn2 = new DataColumn("Name");
			dataColumn2.DataType = Type.GetType("System.String");
			DataColumn dataColumn3 = new DataColumn("Value");
			dataColumn3.DataType = Type.GetType("System.String");
			dataColumn3.DefaultValue = "";
			gridTable.Columns.AddRange(new DataColumn[3] { dataColumn, dataColumn2, dataColumn3 });
		}
		catch
		{
		}
	}

	private void QTMGlobalVariables_Load(object sender, EventArgs e)
	{
		LoadGlobalVariables();
	}

	private void LoadGlobalVariables()
	{
		try
		{
			gridTable.Clear();
			foreach (string sortedGVName in QTMGlobalVariable.SortedGVNames)
			{
				switch (sortedGVName)
				{
				case "QSPR_STOP":
				case "QSPR_ABORT":
				case "QTM_STOP":
				case "QTM_ABORT":
					continue;
				}
				DataRow dataRow = gridTable.NewRow();
				dataRow["Name"] = sortedGVName;
				if (QTMGlobalVariable.GetGlobalVariable(sortedGVName, out var gvValue))
				{
					dataRow["Value"] = gvValue;
					gridTable.Rows.Add(dataRow);
				}
			}
		}
		catch
		{
		}
		gvGrid.DataSource = gridTable;
		gvGrid.Columns[0].Width = 60;
		SelectCurrent();
	}

	private void SelectCurrent()
	{
		try
		{
			if (!(currentSelection != ""))
			{
				return;
			}
			bool flag = false;
			foreach (DataGridViewRow item in (IEnumerable)gvGrid.Rows)
			{
				if (item.Cells["Name"].Value.ToString().Trim() == currentSelection)
				{
					item.Cells["Selection"].Value = true;
					flag = true;
					item.Selected = true;
					gvGrid.CurrentCell = item.Cells[0];
				}
			}
			if (!flag)
			{
				DataRow dataRow = gridTable.NewRow();
				dataRow["Name"] = currentSelection;
				dataRow["Selection"] = true;
				gridTable.Rows.Add(dataRow);
			}
		}
		catch
		{
		}
	}

	private void gvGrid_CurrentCellDirtyStateChanged(object sender, EventArgs e)
	{
		try
		{
			if (gvGrid.CurrentCell.OwningColumn.HeaderText == "Selection" && !invalidating)
			{
				if ((bool)gvGrid.CurrentCell.Value)
				{
					InvalidateCellSelection();
					gvGrid.CurrentCell.Value = true;
					currentSelection = gvGrid.CurrentCell.OwningRow.Cells["Name"].Value.ToString();
					invalidating = false;
					gvGrid.Update();
				}
				else
				{
					currentSelection = "";
				}
			}
			if (gvGrid.CurrentCell.OwningColumn.HeaderText == "Name" && (bool)gvGrid.CurrentCell.OwningRow.Cells["Selection"].Value)
			{
				currentSelection = gvGrid.CurrentCell.Value.ToString();
			}
		}
		catch
		{
		}
	}

	private void InvalidateCellSelection()
	{
		invalidating = true;
		try
		{
			foreach (DataGridViewRow item in (IEnumerable)gvGrid.Rows)
			{
				item.Cells["Selection"].Value = false;
			}
		}
		catch
		{
		}
	}

	private void btnOK_Click(object sender, EventArgs e)
	{
		try
		{
			gvGrid.Update();
			bool flag = false;
			if (QTMGlobalVariable.GetGlobalVariable("QSPR_STOP", out var gvValue))
			{
				try
				{
					flag = Convert.ToBoolean(gvValue);
				}
				catch
				{
				}
			}
			bool flag2 = false;
			if (QTMGlobalVariable.GetGlobalVariable("QSPR_ABORT", out var gvValue2))
			{
				try
				{
					flag2 = Convert.ToBoolean(gvValue2);
				}
				catch
				{
				}
			}
			QTMGlobalVariable.GVariables.Clear();
			currentSelection = "";
			foreach (DataGridViewRow item in (IEnumerable)gvGrid.Rows)
			{
				try
				{
					string text = item.Cells["Name"].Value.ToString().Trim();
					string gvValue3 = item.Cells["Value"].Value.ToString().Trim();
					if (text.Trim().Length > 0)
					{
						QTMGlobalVariable.SetGlobalVariable(text, gvValue3);
					}
					if (item.Cells["Selection"].Value.ToString().ToUpper() == "TRUE")
					{
						currentSelection = text;
					}
				}
				catch
				{
				}
			}
			QTMGlobalVariable.SetGlobalVariable("QSPR_STOP", flag.ToString());
			QTMGlobalVariable.SetGlobalVariable("QSPR_ABORT", flag2.ToString());
			QTMGlobalVariable.SetGlobalVariable("QTM_STOP", flag.ToString());
			QTMGlobalVariable.SetGlobalVariable("QTM_ABORT", flag2.ToString());
			base.DialogResult = DialogResult.OK;
			Close();
		}
		catch
		{
		}
	}

	private void btnAdd_Click(object sender, EventArgs e)
	{
		try
		{
			DataRow row = gridTable.NewRow();
			gridTable.Rows.Add(row);
			gvGrid.Focus();
			foreach (DataGridViewRow item in (IEnumerable)gvGrid.Rows)
			{
				if (item.Selected)
				{
					item.Selected = false;
				}
			}
			gvGrid.Rows[gvGrid.Rows.Count - 1].Selected = true;
			gvGrid.CurrentCell = gvGrid.Rows[gvGrid.Rows.Count - 1].Cells[0];
		}
		catch
		{
		}
	}

	private void btnRemove_Click(object sender, EventArgs e)
	{
		try
		{
			string text = "";
			if (gvGrid.SelectedRows.Count > 1)
			{
				text = "s";
			}
			DialogResult dialogResult = MessageBox.Show(this, "Do you want to delete the selected Global Variable" + text + " ?", "QSPR Message Center - Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
			if (dialogResult != DialogResult.Yes)
			{
				return;
			}
			ArrayList arrayList = new ArrayList();
			foreach (DataGridViewRow selectedRow in gvGrid.SelectedRows)
			{
				arrayList.Add(selectedRow);
			}
			foreach (DataGridViewRow item in arrayList)
			{
				gvGrid.Rows.Remove(item);
			}
			btnRemove.Enabled = false;
		}
		catch
		{
		}
	}

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

	private void gvGrid_SelectionChanged(object sender, EventArgs e)
	{
		try
		{
			if (gvGrid.SelectedRows.Count > 0)
			{
				btnRemove.Enabled = true;
			}
			else
			{
				btnRemove.Enabled = false;
			}
		}
		catch
		{
		}
	}

	private void gvGrid_CurrentCellChanged(object sender, EventArgs e)
	{
		try
		{
			gvGrid.Update();
		}
		catch
		{
		}
	}

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

	private void InitializeComponent()
	{
		System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(QC.GlobalVariableManager.QTMGlobalVariablesDialog));
		this.gvGrid = new System.Windows.Forms.DataGridView();
		this.btnOK = new System.Windows.Forms.Button();
		this.btnCancel = new System.Windows.Forms.Button();
		this.panelGrid = new System.Windows.Forms.Panel();
		this.btnAdd = new System.Windows.Forms.Button();
		this.btnRemove = new System.Windows.Forms.Button();
		this.panelButtons = new System.Windows.Forms.Panel();
		((System.ComponentModel.ISupportInitialize)this.gvGrid).BeginInit();
		this.panelGrid.SuspendLayout();
		this.panelButtons.SuspendLayout();
		base.SuspendLayout();
		this.gvGrid.AllowUserToAddRows = false;
		this.gvGrid.AllowUserToDeleteRows = false;
		this.gvGrid.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
		this.gvGrid.Dock = System.Windows.Forms.DockStyle.Fill;
		this.gvGrid.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		this.gvGrid.Location = new System.Drawing.Point(0, 0);
		this.gvGrid.Name = "gvGrid";
		this.gvGrid.Size = new System.Drawing.Size(533, 358);
		this.gvGrid.TabIndex = 0;
		this.gvGrid.CurrentCellChanged += new System.EventHandler(gvGrid_CurrentCellChanged);
		this.gvGrid.CurrentCellDirtyStateChanged += new System.EventHandler(gvGrid_CurrentCellDirtyStateChanged);
		this.gvGrid.SelectionChanged += new System.EventHandler(gvGrid_SelectionChanged);
		this.btnOK.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right;
		this.btnOK.Location = new System.Drawing.Point(452, 8);
		this.btnOK.Name = "btnOK";
		this.btnOK.Size = new System.Drawing.Size(75, 23);
		this.btnOK.TabIndex = 1;
		this.btnOK.Text = "Ok";
		this.btnOK.UseVisualStyleBackColor = true;
		this.btnOK.Click += new System.EventHandler(btnOK_Click);
		this.btnCancel.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right;
		this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		this.btnCancel.Location = new System.Drawing.Point(371, 8);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(75, 23);
		this.btnCancel.TabIndex = 2;
		this.btnCancel.Text = "Cancel";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
		this.panelGrid.Controls.Add(this.gvGrid);
		this.panelGrid.Dock = System.Windows.Forms.DockStyle.Fill;
		this.panelGrid.Location = new System.Drawing.Point(0, 36);
		this.panelGrid.Name = "panelGrid";
		this.panelGrid.Size = new System.Drawing.Size(533, 358);
		this.panelGrid.TabIndex = 3;
		this.btnAdd.Image = (System.Drawing.Image)resources.GetObject("btnAdd.Image");
		this.btnAdd.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
		this.btnAdd.Location = new System.Drawing.Point(5, 6);
		this.btnAdd.Name = "btnAdd";
		this.btnAdd.Size = new System.Drawing.Size(75, 23);
		this.btnAdd.TabIndex = 4;
		this.btnAdd.Text = "Add";
		this.btnAdd.UseVisualStyleBackColor = true;
		this.btnAdd.Click += new System.EventHandler(btnAdd_Click);
		this.btnRemove.Enabled = false;
		this.btnRemove.Image = (System.Drawing.Image)resources.GetObject("btnRemove.Image");
		this.btnRemove.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
		this.btnRemove.Location = new System.Drawing.Point(86, 6);
		this.btnRemove.Name = "btnRemove";
		this.btnRemove.Size = new System.Drawing.Size(87, 23);
		this.btnRemove.TabIndex = 5;
		this.btnRemove.Text = "Remove";
		this.btnRemove.UseVisualStyleBackColor = true;
		this.btnRemove.Click += new System.EventHandler(btnRemove_Click);
		this.panelButtons.Controls.Add(this.btnAdd);
		this.panelButtons.Controls.Add(this.btnRemove);
		this.panelButtons.Controls.Add(this.btnCancel);
		this.panelButtons.Controls.Add(this.btnOK);
		this.panelButtons.Dock = System.Windows.Forms.DockStyle.Top;
		this.panelButtons.Location = new System.Drawing.Point(0, 0);
		this.panelButtons.Name = "panelButtons";
		this.panelButtons.Size = new System.Drawing.Size(533, 36);
		this.panelButtons.TabIndex = 6;
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		this.BackColor = System.Drawing.Color.White;
		base.ClientSize = new System.Drawing.Size(533, 394);
		base.Controls.Add(this.panelGrid);
		base.Controls.Add(this.panelButtons);
		base.Icon = (System.Drawing.Icon)resources.GetObject("$this.Icon");
		base.Name = "QTMGlobalVariablesDialog";
		base.ShowIcon = false;
		base.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
		this.Text = "Global Variable Manager";
		base.Load += new System.EventHandler(QTMGlobalVariables_Load);
		((System.ComponentModel.ISupportInitialize)this.gvGrid).EndInit();
		this.panelGrid.ResumeLayout(false);
		this.panelButtons.ResumeLayout(false);
		base.ResumeLayout(false);
	}
}
