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

namespace QC.QTMScriptKernel;

public class ScriptOptionsDialog : Form
{
	private const string NAME = "Name";

	private const string VALUE = "Value";

	private const string LOWER_LIMIT = "Lower Limit";

	private const string UPPER_LIMIT = "Upper Limit";

	private const string DATA_TYPE = "Data Type";

	private const string IO_TYPE = "IO Type";

	private const string UNIT = "Unit";

	private const string DESCRIPTION = "Description";

	public const string DEFAULT_OUTPUT_VALUE = "-99999";

	private const string INPUT = "Input";

	private const string OUTPUT = "Output";

	private const string INT = "int";

	private const string DOUBLE = "double";

	private const string STRING = "string";

	private const string ROW_SEPARATOR = "^";

	private const string ROW_ITEM_SEPARATOR = "|";

	private object parameters;

	private string[] IOTypes = new string[2] { "Input", "Output" };

	private string[] DataTypes = new string[3] { "int", "double", "string" };

	private Panel panelBottom;

	private Button btnOk;

	private Button btnCancel;

	private DataGridView paramGrid;

	private Button btnDeleteRow;

	private Button btnInsertRow;

	private Button btnPasteOptions;

	private Button btnCopyOptions;

	public object Parameters => parameters;

	public ScriptOptionsDialog(object parameters)
	{
		this.parameters = parameters;
		InitializeComponent();
		InitializeGrid();
	}

	protected override void Dispose(bool disposing)
	{
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.panelBottom = new System.Windows.Forms.Panel();
		this.btnPasteOptions = new System.Windows.Forms.Button();
		this.btnCopyOptions = new System.Windows.Forms.Button();
		this.btnDeleteRow = new System.Windows.Forms.Button();
		this.btnInsertRow = new System.Windows.Forms.Button();
		this.btnCancel = new System.Windows.Forms.Button();
		this.btnOk = new System.Windows.Forms.Button();
		this.paramGrid = new System.Windows.Forms.DataGridView();
		this.panelBottom.SuspendLayout();
		((System.ComponentModel.ISupportInitialize)this.paramGrid).BeginInit();
		base.SuspendLayout();
		this.panelBottom.Controls.Add(this.btnPasteOptions);
		this.panelBottom.Controls.Add(this.btnCopyOptions);
		this.panelBottom.Controls.Add(this.btnDeleteRow);
		this.panelBottom.Controls.Add(this.btnInsertRow);
		this.panelBottom.Controls.Add(this.btnCancel);
		this.panelBottom.Controls.Add(this.btnOk);
		this.panelBottom.Dock = System.Windows.Forms.DockStyle.Bottom;
		this.panelBottom.Location = new System.Drawing.Point(0, 438);
		this.panelBottom.Name = "panelBottom";
		this.panelBottom.Size = new System.Drawing.Size(864, 40);
		this.panelBottom.TabIndex = 0;
		this.btnPasteOptions.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnPasteOptions.Location = new System.Drawing.Point(320, 8);
		this.btnPasteOptions.Name = "btnPasteOptions";
		this.btnPasteOptions.Size = new System.Drawing.Size(86, 23);
		this.btnPasteOptions.TabIndex = 5;
		this.btnPasteOptions.Text = "Paste Options";
		this.btnPasteOptions.Click += new System.EventHandler(btnPasteOptions_Click);
		this.btnCopyOptions.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnCopyOptions.Location = new System.Drawing.Point(210, 8);
		this.btnCopyOptions.Name = "btnCopyOptions";
		this.btnCopyOptions.Size = new System.Drawing.Size(86, 23);
		this.btnCopyOptions.TabIndex = 4;
		this.btnCopyOptions.Text = "Copy Options";
		this.btnCopyOptions.Click += new System.EventHandler(btnCopyOptions_Click);
		this.btnDeleteRow.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnDeleteRow.Location = new System.Drawing.Point(111, 8);
		this.btnDeleteRow.Name = "btnDeleteRow";
		this.btnDeleteRow.Size = new System.Drawing.Size(75, 23);
		this.btnDeleteRow.TabIndex = 3;
		this.btnDeleteRow.Text = "Delete Row";
		this.btnDeleteRow.Click += new System.EventHandler(btnDeleteRow_Click);
		this.btnInsertRow.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnInsertRow.Location = new System.Drawing.Point(12, 8);
		this.btnInsertRow.Name = "btnInsertRow";
		this.btnInsertRow.Size = new System.Drawing.Size(75, 23);
		this.btnInsertRow.TabIndex = 2;
		this.btnInsertRow.Text = "Insert Row";
		this.btnInsertRow.Click += new System.EventHandler(btnInsertRow_Click);
		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(680, 8);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(75, 23);
		this.btnCancel.TabIndex = 1;
		this.btnCancel.Text = "Cancel";
		this.btnOk.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnOk.Location = new System.Drawing.Point(768, 8);
		this.btnOk.Name = "btnOk";
		this.btnOk.Size = new System.Drawing.Size(75, 23);
		this.btnOk.TabIndex = 0;
		this.btnOk.Text = "Ok";
		this.btnOk.Click += new System.EventHandler(btnOk_Click);
		this.paramGrid.Cursor = System.Windows.Forms.Cursors.Default;
		this.paramGrid.Dock = System.Windows.Forms.DockStyle.Fill;
		this.paramGrid.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		this.paramGrid.Location = new System.Drawing.Point(0, 0);
		this.paramGrid.Name = "paramGrid";
		this.paramGrid.Size = new System.Drawing.Size(864, 438);
		this.paramGrid.TabIndex = 2;
		this.paramGrid.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(paramGrid_CellClick);
		this.paramGrid.CurrentCellChanged += new System.EventHandler(paramGrid_CurrentCellChanged);
		this.paramGrid.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(paramGrid_DataError);
		this.paramGrid.RowsAdded += new System.Windows.Forms.DataGridViewRowsAddedEventHandler(paramGrid_RowsAdded);
		this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
		base.ClientSize = new System.Drawing.Size(864, 478);
		base.Controls.Add(this.paramGrid);
		base.Controls.Add(this.panelBottom);
		base.Name = "ScriptOptionsDialog";
		base.ShowIcon = false;
		base.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
		this.Text = "ScriptOptionsDialog";
		base.Load += new System.EventHandler(ScriptOptionsDialog_Load);
		this.panelBottom.ResumeLayout(false);
		((System.ComponentModel.ISupportInitialize)this.paramGrid).EndInit();
		base.ResumeLayout(false);
	}

	private void paramGrid_CellClick(object sender, DataGridViewCellEventArgs e)
	{
		paramGrid.BeginEdit(selectAll: true);
	}

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

	private void paramGrid_CurrentCellChanged(object sender, EventArgs e)
	{
		paramGrid.Update();
	}

	private void paramGrid_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
	{
		paramGrid.Rows[e.RowIndex].Cells["IO Type"].Value = IOTypes[0];
		paramGrid.Rows[e.RowIndex].Cells["Data Type"].Value = DataTypes[0];
		paramGrid.Rows[e.RowIndex].Cells["Name"].Value = "";
		paramGrid.Rows[e.RowIndex].Cells["Lower Limit"].Value = "";
		paramGrid.Rows[e.RowIndex].Cells["Upper Limit"].Value = "";
		paramGrid.Rows[e.RowIndex].Cells["Value"].Value = "";
		paramGrid.Rows[e.RowIndex].Cells["Unit"].Value = "";
		paramGrid.Rows[e.RowIndex].Cells["Description"].Value = "";
	}

	private void InitializeGrid()
	{
		try
		{
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn.Name = "Name";
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn2 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn2.Name = "Lower Limit";
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn3 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn3.Name = "Value";
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn4 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn4.Name = "Upper Limit";
			DataGridViewComboBoxColumn dataGridViewComboBoxColumn = new DataGridViewComboBoxColumn();
			dataGridViewComboBoxColumn.Name = "IO Type";
			dataGridViewComboBoxColumn.DataSource = IOTypes;
			DataGridViewComboBoxColumn dataGridViewComboBoxColumn2 = new DataGridViewComboBoxColumn();
			dataGridViewComboBoxColumn2.Name = "Data Type";
			dataGridViewComboBoxColumn2.DataSource = DataTypes;
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn5 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn5.Name = "Unit";
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn6 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn6.Name = "Description";
			paramGrid.Columns.AddRange(dataGridViewTextBoxColumn, dataGridViewTextBoxColumn2, dataGridViewTextBoxColumn3, dataGridViewTextBoxColumn4, dataGridViewComboBoxColumn, dataGridViewComboBoxColumn2, dataGridViewTextBoxColumn5, dataGridViewTextBoxColumn6);
		}
		catch
		{
		}
	}

	private void ScriptOptionsDialog_Load(object sender, EventArgs e)
	{
		try
		{
			LoadParameters();
		}
		catch
		{
		}
	}

	private void LoadParameters()
	{
		ArrayList arrayList = new ArrayList();
		ParameterHandler.CreateParameters(parameters, out arrayList);
		foreach (Parameter item in arrayList)
		{
			int index = paramGrid.Rows.Add();
			DataGridViewRow dataGridViewRow = paramGrid.Rows[index];
			dataGridViewRow.Cells["Name"].Value = item.Name;
			dataGridViewRow.Cells["Lower Limit"].Value = item.LowerLimit;
			dataGridViewRow.Cells["Value"].Value = item.Value;
			dataGridViewRow.Cells["Upper Limit"].Value = item.UpperLimit;
			dataGridViewRow.Cells["IO Type"].Value = item.IOType;
			dataGridViewRow.Cells["Data Type"].Value = item.DataType.ToLower();
			dataGridViewRow.Cells["Unit"].Value = item.UnitOfMeasure;
			dataGridViewRow.Cells["Description"].Value = item.Description;
		}
	}

	private void btnOk_Click(object sender, EventArgs e)
	{
		try
		{
			ArrayList arrayList = new ArrayList();
			foreach (DataGridViewRow item in (IEnumerable)paramGrid.Rows)
			{
				try
				{
					if (!item.IsNewRow)
					{
						switch (CheckRowResult(item))
						{
						case RowResult.Valid:
						{
							Parameter parameter = new Parameter();
							parameter.Name = item.Cells["Name"].Value.ToString().Trim();
							parameter.LowerLimit = item.Cells["Lower Limit"].Value.ToString().Trim();
							parameter.Value = item.Cells["Value"].Value.ToString();
							parameter.UpperLimit = item.Cells["Upper Limit"].Value.ToString().Trim();
							parameter.IOType = item.Cells["IO Type"].Value.ToString();
							parameter.DataType = item.Cells["Data Type"].Value.ToString();
							parameter.UnitOfMeasure = item.Cells["Unit"].Value.ToString().Trim();
							parameter.Description = item.Cells["Description"].Value.ToString().Trim();
							parameter.DisplayHint = 0;
							parameter.NumberOfElements = 1;
							arrayList.Add(parameter);
							break;
						}
						case RowResult.Cancel:
							return;
						}
					}
				}
				catch
				{
				}
			}
			parameters = ParameterHandler.GetParameterObject(arrayList);
			base.DialogResult = DialogResult.OK;
		}
		catch
		{
		}
	}

	private RowResult CheckRowResult(DataGridViewRow row)
	{
		try
		{
			string text = row.Cells["Name"].Value.ToString().Trim();
			if (text.Length == 0)
			{
				DialogResult dialogResult = MessageBox.Show(this, "Parameter name cannot be empty.\nDo you want to ignore the parameter?", "QTM - Message Center", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
				if (dialogResult == DialogResult.Yes)
				{
					return RowResult.Ignore;
				}
				return RowResult.Cancel;
			}
			string text2 = row.Cells["Lower Limit"].Value.ToString().Trim();
			string text3 = row.Cells["Value"].Value.ToString();
			string text4 = row.Cells["Upper Limit"].Value.ToString().Trim();
			string text5 = row.Cells["IO Type"].Value.ToString();
			string text6 = row.Cells["Data Type"].Value.ToString();
			row.Cells["Unit"].Value.ToString().Trim();
			row.Cells["Description"].Value.ToString().Trim();
			switch (text5)
			{
			case "Input":
				if (text3.Length == 0)
				{
					DialogResult dialogResult3 = MessageBox.Show(this, text + "'s value cannot be empty.\nDo you want to ignore the parameter?", "QTM - Message Center", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
					if (dialogResult3 == DialogResult.Yes)
					{
						return RowResult.Ignore;
					}
					return RowResult.Cancel;
				}
				if (!IsDataValid(text3, text6))
				{
					DialogResult dialogResult4 = MessageBox.Show(this, text + "'s value not valid according to the data type.\nDo you want to ignore the parameter?", "QTM - Message Center", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
					if (dialogResult4 == DialogResult.Yes)
					{
						return RowResult.Ignore;
					}
					return RowResult.Cancel;
				}
				if (!IsDataValid(text2, text6))
				{
					row.Cells["Lower Limit"].Value = "";
				}
				if (!IsDataValid(text4, text6))
				{
					row.Cells["Upper Limit"].Value = "";
				}
				paramGrid.Update();
				break;
			case "Output":
			{
				if (!(text6 != "string"))
				{
					break;
				}
				if (text2.Length == 0 || text4.Length == 0)
				{
					switch (MessageBox.Show(this, text + "'s limit empty.\nDo you want to continue?", "QTM - Message Center", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation))
					{
					case DialogResult.Yes:
						return RowResult.Valid;
					case DialogResult.No:
						return RowResult.Cancel;
					}
				}
				bool flag = IsDataValid(text2, text6);
				bool flag2 = IsDataValid(text4, text6);
				if (!flag || !flag2)
				{
					DialogResult dialogResult2 = MessageBox.Show(this, text + "'s limits not valid according to the data type.\nDo you want to ignore the parameter?", "QTM - Message Center", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
					if (dialogResult2 == DialogResult.Yes)
					{
						return RowResult.Ignore;
					}
					return RowResult.Cancel;
				}
				if (!IsDataValid(text3, text6))
				{
					row.Cells["Value"].Value = "-99999";
					paramGrid.Update();
				}
				break;
			}
			}
		}
		catch
		{
		}
		return RowResult.Valid;
	}

	private bool IsDataValid(string strValue, string dType)
	{
		try
		{
			switch (dType)
			{
			case "int":
				int.Parse(strValue);
				break;
			case "double":
				double.Parse(strValue, CultureInfo.InvariantCulture);
				break;
			}
		}
		catch
		{
			return false;
		}
		return true;
	}

	private void btnInsertRow_Click(object sender, EventArgs e)
	{
		try
		{
			if (paramGrid.SelectedRows.Count == 1)
			{
				int num = 0;
				num = paramGrid.Rows.IndexOf(paramGrid.CurrentRow);
				paramGrid.Rows.Insert(num, new DataGridViewRow());
				paramGrid.Rows[num].Cells["IO Type"].Value = IOTypes[0];
				paramGrid.Rows[num].Cells["Data Type"].Value = DataTypes[0];
				paramGrid.CurrentCell = paramGrid.Rows[num].Cells[0];
				paramGrid.CurrentRow.Selected = true;
			}
			else if (paramGrid.SelectedRows.Count > 1)
			{
				MessageBox.Show("Insertion is done when only 1 row is selected!", "QSPR - Loop Setup", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
			}
			else
			{
				MessageBox.Show("Select a row!", "QSPR - Loop Setup", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
			}
		}
		catch (Exception ex)
		{
			MessageBox.Show("ERROR: " + ex.Message, "QSPR - Loop Setup", MessageBoxButtons.OK, MessageBoxIcon.Hand);
		}
	}

	private void btnDeleteRow_Click(object sender, EventArgs e)
	{
		if (paramGrid.SelectedRows.Count >= 1)
		{
			if (paramGrid.Rows.Count <= 0)
			{
				return;
			}
			int count = paramGrid.SelectedRows.Count;
			string text = "";
			if (count > 1)
			{
				text = count + " rows will be deleted";
			}
			else if (count == 1)
			{
				text = "1 row will be deleted";
			}
			DialogResult dialogResult = MessageBox.Show(this, text, "QSPR - Confirm Row Deletion", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
			if (dialogResult != DialogResult.OK)
			{
				return;
			}
			try
			{
				foreach (DataGridViewRow selectedRow in paramGrid.SelectedRows)
				{
					paramGrid.Rows.Remove(selectedRow);
				}
				_ = paramGrid.Rows.Count;
				return;
			}
			catch (Exception ex)
			{
				MessageBox.Show("ERROR: " + ex.Message, "QSPR - Loop Setup", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				return;
			}
		}
		MessageBox.Show("Select a row!", "QSPR - Loop Setup", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
	}

	private void btnCopyOptions_Click(object sender, EventArgs e)
	{
		string text = "";
		if (paramGrid.SelectedRows.Count == 0 && paramGrid.Rows.Count > 0)
		{
			paramGrid.Rows[0].Selected = true;
		}
		foreach (DataGridViewRow selectedRow in paramGrid.SelectedRows)
		{
			List<string> list = new List<string>();
			foreach (DataGridViewCell cell in selectedRow.Cells)
			{
				string item = "";
				if (cell.Value != null)
				{
					item = cell.Value.ToString();
				}
				list.Add(item);
			}
			text += string.Join("|", list.ToArray());
			text += "^";
		}
		if (!string.IsNullOrEmpty(text))
		{
			Clipboard.SetText(text);
		}
	}

	private void btnPasteOptions_Click(object sender, EventArgs e)
	{
		string text = Clipboard.GetText();
		string[] array = text.Split(new string[1] { "^" }, StringSplitOptions.RemoveEmptyEntries);
		string[] array2 = array;
		foreach (string text2 in array2)
		{
			string[] array3 = text2.Split(new string[1] { "|" }, StringSplitOptions.None);
			int index = paramGrid.Rows.Add();
			DataGridViewRow dataGridViewRow = paramGrid.Rows[index];
			dataGridViewRow.Cells["Name"].Value = array3[0];
			dataGridViewRow.Cells["Lower Limit"].Value = array3[1];
			dataGridViewRow.Cells["Value"].Value = array3[2];
			dataGridViewRow.Cells["Upper Limit"].Value = array3[3];
			dataGridViewRow.Cells["IO Type"].Value = array3[4];
			dataGridViewRow.Cells["Data Type"].Value = array3[5];
			dataGridViewRow.Cells["Unit"].Value = array3[6];
			dataGridViewRow.Cells["Description"].Value = array3[7];
		}
		paramGrid.Refresh();
	}

	private static string ObjectToString(object obj)
	{
		return obj.ToString();
	}
}
