using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using QC.QsprShared.Dialogs;
using QC.QsprShared.Trace;
using QC.QsprShared.Utilities;
using QC.QsprTestObjects.Parameters;
using QC.QTMInterface;

namespace QC.QTMApplication;

public class TTDArrayParamEdit : Form
{
	private DataGridView paramGrid;

	private Panel panelButton;

	private Panel panelGrid;

	private Button btnCancel;

	private Button btnOk;

	private ParameterCls parameter = new ParameterCls();

	private string stringValue = "";

	private string stringLL = "";

	private string stringUL = "";

	private ArrayList enableDataLogging = new ArrayList();

	private ArrayList enableCharting = new ArrayList();

	private Button btnDeleteRow;

	private Button btnInsertRow;

	private bool readOnly;

	private bool isArray = true;

	private string testType = "";

	public ParameterCls Parameter
	{
		set
		{
			parameter = value;
		}
	}

	public bool IsArray
	{
		get
		{
			return isArray;
		}
		set
		{
			isArray = value;
		}
	}

	public string StringValue => stringValue;

	public string StringLL => stringLL;

	public string StringUL => stringUL;

	public ArrayList EnableDataLogging => enableDataLogging;

	public ArrayList EnableCharting => enableCharting;

	public bool ReadOnly
	{
		get
		{
			return readOnly;
		}
		set
		{
			readOnly = value;
		}
	}

	public string TestType
	{
		get
		{
			return testType;
		}
		set
		{
			testType = value;
		}
	}

	public TTDArrayParamEdit()
	{
		InitializeComponent();
	}

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

	private void InitializeComponent()
	{
		System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(QC.QTMApplication.TTDArrayParamEdit));
		this.paramGrid = new System.Windows.Forms.DataGridView();
		this.panelButton = new System.Windows.Forms.Panel();
		this.btnInsertRow = new System.Windows.Forms.Button();
		this.btnDeleteRow = new System.Windows.Forms.Button();
		this.btnOk = new System.Windows.Forms.Button();
		this.btnCancel = new System.Windows.Forms.Button();
		this.panelGrid = new System.Windows.Forms.Panel();
		((System.ComponentModel.ISupportInitialize)this.paramGrid).BeginInit();
		this.panelButton.SuspendLayout();
		this.panelGrid.SuspendLayout();
		base.SuspendLayout();
		this.paramGrid.AllowUserToAddRows = false;
		this.paramGrid.AllowUserToDeleteRows = false;
		this.paramGrid.AllowUserToResizeRows = false;
		this.paramGrid.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
		this.paramGrid.Cursor = System.Windows.Forms.Cursors.Hand;
		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(608, 299);
		this.paramGrid.TabIndex = 0;
		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.paramGrid.SelectionChanged += new System.EventHandler(paramGrid_SelectionChanged);
		this.paramGrid.KeyDown += new System.Windows.Forms.KeyEventHandler(paramGrid_KeyDown);
		this.paramGrid.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(paramGrid_DataError);
		this.panelButton.BackColor = System.Drawing.Color.Transparent;
		this.panelButton.Controls.Add(this.btnInsertRow);
		this.panelButton.Controls.Add(this.btnDeleteRow);
		this.panelButton.Controls.Add(this.btnOk);
		this.panelButton.Controls.Add(this.btnCancel);
		this.panelButton.Dock = System.Windows.Forms.DockStyle.Bottom;
		this.panelButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
		this.panelButton.Location = new System.Drawing.Point(0, 299);
		this.panelButton.Name = "panelButton";
		this.panelButton.Size = new System.Drawing.Size(608, 43);
		this.panelButton.TabIndex = 3;
		this.btnInsertRow.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left;
		this.btnInsertRow.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		this.btnInsertRow.Location = new System.Drawing.Point(12, 7);
		this.btnInsertRow.Name = "btnInsertRow";
		this.btnInsertRow.Size = new System.Drawing.Size(92, 24);
		this.btnInsertRow.TabIndex = 4;
		this.btnInsertRow.Text = "&Insert Row";
		this.btnInsertRow.Click += new System.EventHandler(btnInsertRow_Click);
		this.btnDeleteRow.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left;
		this.btnDeleteRow.Enabled = false;
		this.btnDeleteRow.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		this.btnDeleteRow.Location = new System.Drawing.Point(110, 7);
		this.btnDeleteRow.Name = "btnDeleteRow";
		this.btnDeleteRow.Size = new System.Drawing.Size(92, 24);
		this.btnDeleteRow.TabIndex = 3;
		this.btnDeleteRow.Text = "&Delete Row(s)";
		this.btnDeleteRow.Click += new System.EventHandler(btnDeleteRow_Click);
		this.btnOk.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnOk.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		this.btnOk.Location = new System.Drawing.Point(512, 11);
		this.btnOk.Name = "btnOk";
		this.btnOk.Size = new System.Drawing.Size(75, 24);
		this.btnOk.TabIndex = 1;
		this.btnOk.Text = "Ok";
		this.btnOk.Click += new System.EventHandler(btnOK_Click);
		this.btnCancel.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		this.btnCancel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		this.btnCancel.Location = new System.Drawing.Point(424, 11);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(75, 24);
		this.btnCancel.TabIndex = 0;
		this.btnCancel.Text = "Cancel";
		this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
		this.panelGrid.Controls.Add(this.paramGrid);
		this.panelGrid.Dock = System.Windows.Forms.DockStyle.Fill;
		this.panelGrid.Location = new System.Drawing.Point(0, 0);
		this.panelGrid.Name = "panelGrid";
		this.panelGrid.Size = new System.Drawing.Size(608, 299);
		this.panelGrid.TabIndex = 4;
		this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
		base.ClientSize = new System.Drawing.Size(608, 342);
		base.Controls.Add(this.panelGrid);
		base.Controls.Add(this.panelButton);
		base.Icon = (System.Drawing.Icon)resources.GetObject("$this.Icon");
		base.Name = "TTDArrayParamEdit";
		base.ShowIcon = false;
		base.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
		this.Text = "QSPR - Array Parameter";
		base.TransparencyKey = System.Drawing.Color.Magenta;
		base.Load += new System.EventHandler(TTDArrayParamEdit_Load);
		((System.ComponentModel.ISupportInitialize)this.paramGrid).EndInit();
		this.panelButton.ResumeLayout(false);
		this.panelGrid.ResumeLayout(false);
		base.ResumeLayout(false);
	}

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

	private void TTDArrayParamEdit_Load(object sender, EventArgs e)
	{
		paramGrid.AllowUserToAddRows = parameter.IsDynamicArray;
		InitTable();
		SetEditingProperty(editable: true);
		LoadGrid();
		paramGrid.CommitEdit(DataGridViewDataErrorContexts.Commit);
		if (parameter.Type == IOType.Input)
		{
			CreateDropDownCell();
			ActivateRowsOnce();
		}
		SetEditingProperty(!readOnly);
		if (!parameter.IsDynamicArray)
		{
			btnInsertRow.Visible = false;
			btnDeleteRow.Visible = false;
		}
	}

	private void SetEditingProperty(bool editable)
	{
		try
		{
			if (!editable)
			{
				if (parameter.Type == IOType.Input)
				{
					paramGrid.Columns["Value"].ReadOnly = true;
					paramGrid.Columns["Data_Logging"].ReadOnly = true;
				}
				else
				{
					paramGrid.Columns["LL"].ReadOnly = true;
					paramGrid.Columns["UL"].ReadOnly = true;
					paramGrid.Columns["Data_Logging"].ReadOnly = true;
					paramGrid.Columns["Reporting_Enabled"].ReadOnly = true;
				}
				btnOk.Enabled = false;
			}
			else
			{
				if (parameter.Type == IOType.Input)
				{
					paramGrid.Columns["Value"].ReadOnly = false;
					paramGrid.Columns["Data_Logging"].ReadOnly = false;
				}
				else
				{
					paramGrid.Columns["LL"].ReadOnly = false;
					paramGrid.Columns["UL"].ReadOnly = false;
					paramGrid.Columns["Data_Logging"].ReadOnly = false;
					paramGrid.Columns["Reporting_Enabled"].ReadOnly = false;
				}
				btnOk.Enabled = true;
			}
			paramGrid.Columns["Index"].ReadOnly = true;
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "SetEditingProperty " + ex.Message, TraceMessageType.Exception);
		}
	}

	private void ActivateRowsOnce()
	{
		foreach (DataGridViewRow item in (IEnumerable)paramGrid.Rows)
		{
			DataGridViewCell dataGridViewCell = item.Cells["Value"];
			if (parameter.Type == IOType.Input)
			{
				if (dataGridViewCell != null && !(dataGridViewCell.OwningColumn.Name == "Value"))
				{
				}
			}
			else if (dataGridViewCell != null && dataGridViewCell.OwningColumn.Name != "Value")
			{
				paramGrid.BeginEdit(selectAll: true);
			}
		}
		if (paramGrid.Rows.Count > 0)
		{
			paramGrid.CurrentCell = paramGrid.Rows[0].Cells["Value"];
		}
	}

	private void InitTable()
	{
		Text = "Parameter Name: " + parameter.Name;
		DataGridViewTextBoxColumn dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
		dataGridViewTextBoxColumn.Name = "Index";
		DataGridViewTextBoxColumn dataGridViewTextBoxColumn2 = new DataGridViewTextBoxColumn();
		dataGridViewTextBoxColumn2.Name = "LL";
		DataGridViewTextBoxColumn dataGridViewTextBoxColumn3 = new DataGridViewTextBoxColumn();
		dataGridViewTextBoxColumn3.Name = "Value";
		DataGridViewTextBoxColumn dataGridViewTextBoxColumn4 = new DataGridViewTextBoxColumn();
		dataGridViewTextBoxColumn4.Name = "UL";
		DataGridViewCheckBoxColumn dataGridViewCheckBoxColumn = new DataGridViewCheckBoxColumn();
		dataGridViewCheckBoxColumn.Name = "Data_Logging";
		DataGridViewCheckBoxColumn dataGridViewCheckBoxColumn2 = new DataGridViewCheckBoxColumn();
		dataGridViewCheckBoxColumn2.Name = "Reporting_Enabled";
		paramGrid.Columns.AddRange(dataGridViewTextBoxColumn, dataGridViewTextBoxColumn2, dataGridViewTextBoxColumn3, dataGridViewTextBoxColumn4, dataGridViewCheckBoxColumn, dataGridViewCheckBoxColumn2);
		paramGrid.Columns["Index"].Width = 35;
		if (parameter.Type == IOType.Input)
		{
			paramGrid.Columns["LL"].Visible = false;
			paramGrid.Columns["UL"].Visible = false;
			paramGrid.Columns["Reporting_Enabled"].Visible = false;
		}
		else
		{
			paramGrid.Columns["Value"].ReadOnly = true;
		}
	}

	private void LoadGrid()
	{
		try
		{
			string[] array = parameter.Value.Split(',');
			string[] array2 = null;
			string[] array3 = null;
			if (parameter.Type == IOType.Output)
			{
				array2 = parameter.LowerLimit.Split(',');
				array3 = parameter.UpperLimit.Split(',');
			}
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = array[i].Trim();
			}
			if (parameter.DataType == DataType.String && isArray)
			{
				array = ParameterCls.SplitStringArrayParameter(parameter.Value);
			}
			int num = parameter.NoOfElements;
			if (parameter.IsDynamicArray)
			{
				num = array.Length;
				if (parameter.Type == IOType.Output)
				{
					if (num < array2.Length)
					{
						num = array2.Length;
					}
					if (num < array3.Length)
					{
						num = array3.Length;
					}
				}
			}
			for (int j = 0; j < num; j++)
			{
				if (paramGrid.Rows.Count <= j)
				{
					paramGrid.Rows.Add();
				}
			}
			paramGrid.Sort(paramGrid.Columns[0], ListSortDirection.Ascending);
			for (int k = 0; k < num; k++)
			{
				DataGridViewRow dataGridViewRow = paramGrid.Rows[k];
				dataGridViewRow.Cells["Index"].Value = k;
				if (k < array.Length)
				{
					dataGridViewRow.Cells["Value"].Value = array[k];
				}
				else
				{
					dataGridViewRow.Cells["Value"].Value = "";
				}
				if (k < parameter.EnableDataLogging.Count)
				{
					dataGridViewRow.Cells["Data_Logging"].Value = parameter.EnableDataLogging[k].ToString();
				}
				else
				{
					dataGridViewRow.Cells["Data_Logging"].Value = false;
				}
				if (parameter.Type == IOType.Output)
				{
					if (k < array2.Length)
					{
						dataGridViewRow.Cells["LL"].Value = array2[k].Trim();
					}
					else
					{
						dataGridViewRow.Cells["LL"].Value = "";
					}
					if (k < array3.Length)
					{
						dataGridViewRow.Cells["UL"].Value = array3[k].Trim();
					}
					else
					{
						dataGridViewRow.Cells["UL"].Value = "";
					}
					if (k < parameter.EnableCharting.Count)
					{
						dataGridViewRow.Cells["Reporting_Enabled"].Value = parameter.EnableCharting[k].ToString();
					}
					else
					{
						dataGridViewRow.Cells["Reporting_Enabled"].Value = false;
					}
				}
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "LoadGrid " + ex.Message, TraceMessageType.Exception);
			AppMessageDialog.Show("Parameter value list not valid", "QSPR - Message Center", MessageBoxButtons.OK, MessageBoxIcon.Hand, DialogResult.OK, MessageDialogType.Exception);
		}
	}

	private void paramGrid_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
	{
		int rowIndex = e.RowIndex;
		int num = e.RowIndex + e.RowCount;
		for (int i = rowIndex; i < num; i++)
		{
			paramGrid.Rows[i].Cells["Index"].Value = i;
			if (i >= e.RowIndex && i < e.RowIndex + e.RowCount)
			{
				paramGrid.Rows[i].Cells["LL"].Value = "";
				paramGrid.Rows[i].Cells["Value"].Value = "";
				paramGrid.Rows[i].Cells["UL"].Value = "";
				if (parameter.EnableDataLogging.Count > i)
				{
					paramGrid.Rows[i].Cells["Data_Logging"].Value = parameter.EnableDataLogging[i];
				}
				else
				{
					bool flag = parameter.Type == IOType.Output;
					paramGrid.Rows[i].Cells["Data_Logging"].Value = flag;
				}
				paramGrid.Rows[i].Cells["Reporting_Enabled"].Value = false;
			}
		}
	}

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

	private void paramGrid_CurrentCellChanged(object sender, EventArgs e)
	{
		if (parameter.Type != 0 && paramGrid.CurrentCell != null && paramGrid.CurrentCell.OwningColumn.HeaderText != "Value")
		{
			paramGrid.BeginEdit(selectAll: true);
		}
	}

	public bool CreateDropDownCell()
	{
		try
		{
			HandleDisplayHints();
			return true;
		}
		catch (Exception ex)
		{
			TraceMessage.Write("QC.QTMApplication.TTDParamView", "CreateDropDownCell " + ex.Message, TraceMessageType.Exception);
			return false;
		}
	}

	private string GetDefaultValue(ParameterCls tParam)
	{
		string[] array = tParam.Constraints.Trim().Split(';');
		return array[0];
	}

	private void HandleDisplayHints()
	{
		string[] hints = parameter.Constraints.Trim().Split(';');
		if ((parameter.DisplayHint & 3) == 3)
		{
			return;
		}
		if ((parameter.DisplayHint & 0x3000) == 12288)
		{
			ApplyDisplayHint(GetConstraints(hints, "False;True"), parameter.IsNullable, enumType: true, parameter.DataType);
		}
		else if ((parameter.DisplayHint & 2) == 2)
		{
			string defaultConstraints = "False;True";
			if ((parameter.DisplayHint & 0x1000) == 4096)
			{
				defaultConstraints = "No;Yes";
			}
			else if ((parameter.DisplayHint & 0x2000) == 8192)
			{
				defaultConstraints = "Off;On";
			}
			else if ((parameter.DisplayHint & 0x100) == 256)
			{
				defaultConstraints = "No;Yes";
			}
			ApplyDisplayHint(GetConstraints(hints, defaultConstraints), parameter.IsNullable, enumType: true, parameter.DataType);
		}
		else
		{
			if ((parameter.DisplayHint & 1) == 1 || (parameter.DisplayHint & 4) == 4)
			{
				return;
			}
			if ((parameter.DisplayHint & 0x1000) == 4096)
			{
				ApplyDisplayHint(GetConstraints(hints, "No;Yes"), parameter.IsNullable, enumType: true, parameter.DataType);
				return;
			}
			if ((parameter.DisplayHint & 0x2000) == 8192)
			{
				ApplyDisplayHint(GetConstraints(hints, "Off;On"), parameter.IsNullable, enumType: true, parameter.DataType);
				return;
			}
			if ((parameter.DisplayHint & 0x100) == 256)
			{
				ApplyDisplayHint(parameter.Constraints.Trim(), parameter.IsNullable, enumType: false, parameter.DataType);
				return;
			}
			if ((parameter.DisplayHint & 8) == 8)
			{
				ApplyDisplayHint(parameter.Constraints.Trim(), parameter.IsNullable, enumType: true, parameter.DataType);
				return;
			}
			_ = parameter.DisplayHint;
			if (0 == 0)
			{
				ApplyDataTypeDisplayHint("False;True", parameter.DataType, parameter.IsNullable);
			}
		}
	}

	private void ApplyDataTypeDisplayHint(string constraints, DataType dType, bool isNullable)
	{
		try
		{
			if (dType == DataType.Boolean)
			{
				ApplyDisplayHint(constraints, isNullable, enumType: false, dType);
			}
		}
		catch
		{
		}
	}

	private void ApplyDisplayHint(string constraints, bool isNullable, bool enumType, DataType dType)
	{
		try
		{
			if (isNullable)
			{
				ShowDropDown(constraints.Trim() + ";", enumType, dType);
			}
			else
			{
				ShowDropDown(constraints.Trim(), enumType, dType);
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ex.Source, "ApplyDisplayHint " + ex.Message, TraceMessageType.Exception);
		}
	}

	private string GetConstraints(string[] hints, string defaultConstraints)
	{
		if (hints.Length > 1)
		{
			return hints[0].Trim() + ";" + hints[1].Trim();
		}
		return defaultConstraints;
	}

	private void ShowDropDown(string constraints, bool enumType, DataType dType)
	{
		try
		{
			string[] array = constraints.Trim().Split(';');
			if (array.Length <= 0)
			{
				return;
			}
			List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
			ArrayList names = new ArrayList();
			ArrayList values = new ArrayList();
			if (enumType)
			{
				if (ParameterCollection.CreateNameValueList(constraints, out names, out values))
				{
					bool flag = QTMUtility.IsAllConstraintsHaveTwoParts(constraints);
					for (int i = 0; i < names.Count; i++)
					{
						string key = (flag ? $"{values[i].ToString()}  {names[i].ToString()}" : names[i].ToString());
						string value = values[i].ToString();
						list.Add(new KeyValuePair<string, string>(key, value));
					}
				}
			}
			else
			{
				for (int j = 0; j < array.Length; j++)
				{
					list.Add(new KeyValuePair<string, string>(array[j].Trim(), array[j].Trim()));
				}
			}
			if (dType == DataType.Boolean && list.Count > 2)
			{
				list.Remove(list[2]);
				list.Add(new KeyValuePair<string, string>("", ""));
			}
			ArrayList arrayList = new ArrayList();
			foreach (DataGridViewRow item in (IEnumerable)paramGrid.Rows)
			{
				DataGridViewCell dataGridViewCell = item.Cells["Value"];
				_ = dataGridViewCell.ColumnIndex;
				_ = dataGridViewCell.RowIndex;
				int num = 0;
				if (enumType)
				{
					string text = ((dataGridViewCell.Value != null) ? dataGridViewCell.Value.ToString() : string.Empty);
					for (int k = 0; k < values.Count; k++)
					{
						if (text == values[k].ToString())
						{
							num = (int)values[k];
							break;
						}
						if (text == names[k].ToString())
						{
							num = (int)values[k];
							break;
						}
					}
					arrayList.Add(num);
				}
				else if (dType == DataType.Boolean)
				{
					string value2 = ((dataGridViewCell.Value != null) ? dataGridViewCell.Value.ToString() : string.Empty);
					bool result = false;
					value2 = ((!bool.TryParse(value2, out result)) ? "" : result.ToString());
					arrayList.Add(value2);
				}
			}
			DataGridViewColumn dataGridViewColumn = paramGrid.Columns["Value"];
			if (!(paramGrid.Columns["Value"] is DataGridViewComboBoxColumn))
			{
				dataGridViewColumn = new DataGridViewComboBoxColumn();
				int index = paramGrid.Columns["Value"].Index;
				dataGridViewColumn.Name = paramGrid.Columns["Value"].Name;
				(dataGridViewColumn as DataGridViewComboBoxColumn).DataSource = list;
				(dataGridViewColumn as DataGridViewComboBoxColumn).DisplayMember = "Key";
				(dataGridViewColumn as DataGridViewComboBoxColumn).ValueMember = "Value";
				paramGrid.Columns.Insert(index, dataGridViewColumn);
				paramGrid.Columns.RemoveAt(index + 1);
			}
			for (int l = 0; l < paramGrid.Rows.Count; l++)
			{
				DataGridViewRow dataGridViewRow2 = paramGrid.Rows[l];
				dataGridViewRow2.Cells["Value"].Value = arrayList[l].ToString();
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write("QC.QTMApplication.TTDParamView", "ShowDropDown " + ex.Message, TraceMessageType.Exception);
		}
	}

	private bool IsDotNetTest()
	{
		if (testType != null)
		{
			return testType.EndsWith("DotNetTest");
		}
		return false;
	}

	private void btnOK_Click(object sender, EventArgs e)
	{
		try
		{
			bool flag = false;
			foreach (DataGridViewRow item in (IEnumerable)paramGrid.Rows)
			{
				foreach (DataGridViewCell cell in item.Cells)
				{
					if (cell.Value == null)
					{
						continue;
					}
					if (!TTDParameterView.ValidateParameter(cell.Value.ToString(), cell.OwningColumn.HeaderText, parameter.DataType, parameter.IsNullable))
					{
						if (!parameter.IsOutput || !(cell.OwningColumn.HeaderText == "Value"))
						{
							cell.Style.BackColor = Color.Yellow;
							flag = true;
						}
					}
					else
					{
						cell.Style.BackColor = Color.White;
					}
				}
			}
			if (flag)
			{
				return;
			}
			enableDataLogging.Clear();
			enableCharting.Clear();
			for (int i = 0; i < paramGrid.Rows.Count - 1; i++)
			{
				DataGridViewRow dataGridViewRow2 = paramGrid.Rows[i];
				if (dataGridViewRow2.Cells["Value"].Value.ToString().Contains(","))
				{
					stringValue = stringValue + "_\"_" + dataGridViewRow2.Cells["Value"].Value.ToString() + "_\"_,";
				}
				else
				{
					stringValue = stringValue + dataGridViewRow2.Cells["Value"].Value.ToString() + ",";
				}
				stringLL = stringLL + dataGridViewRow2.Cells["LL"].Value.ToString().Trim() + ",";
				stringUL = stringUL + dataGridViewRow2.Cells["UL"].Value.ToString().Trim() + ",";
				enableDataLogging.Add(Convert.ToBoolean(dataGridViewRow2.Cells["Data_Logging"].Value.ToString()));
				enableCharting.Add(Convert.ToBoolean(dataGridViewRow2.Cells["Reporting_Enabled"].Value.ToString()));
			}
			DataGridViewRow dataGridViewRow3 = paramGrid.Rows[paramGrid.Rows.Count - 1];
			if (dataGridViewRow3.Cells["Value"].Value.ToString().Contains(","))
			{
				stringValue = stringValue + "_\"_" + dataGridViewRow3.Cells["Value"].Value.ToString() + "_\"_";
			}
			else
			{
				stringValue += dataGridViewRow3.Cells["Value"].Value.ToString();
			}
			stringLL += dataGridViewRow3.Cells["LL"].Value.ToString().Trim();
			stringUL += dataGridViewRow3.Cells["UL"].Value.ToString().Trim();
			enableDataLogging.Add(Convert.ToBoolean(dataGridViewRow3.Cells["Data_Logging"].Value.ToString()));
			enableCharting.Add(Convert.ToBoolean(dataGridViewRow3.Cells["Reporting_Enabled"].Value.ToString()));
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "Ok " + ex.Message, TraceMessageType.Exception);
		}
		base.DialogResult = DialogResult.OK;
		Close();
	}

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

	private void btnInsertRow_Click(object sender, EventArgs e)
	{
		int num = 0;
		try
		{
			if (paramGrid.SelectedRows.Count == 1)
			{
				num = paramGrid.Rows.IndexOf(paramGrid.CurrentRow);
				paramGrid.Rows.Insert(num, new DataGridViewRow());
				paramGrid.CurrentCell = paramGrid.Rows[num].Cells[0];
			}
			else
			{
				num = paramGrid.Rows.Add();
			}
		}
		catch (Exception ex)
		{
			MessageBox.Show("ERROR: " + ex.Message, "QSPR - Array Param Editor", MessageBoxButtons.OK, MessageBoxIcon.Hand);
			return;
		}
		if (paramGrid.Rows.Count <= num)
		{
			return;
		}
		paramGrid.Rows[num].Cells["Data_Logging"].Value = false;
		paramGrid.Rows[num].Cells["Reporting_Enabled"].Value = false;
		RefreshIndexNumbering(null);
		paramGrid.CurrentCell = paramGrid.Rows[num].Cells[0];
		if (parameter.IsInput)
		{
			paramGrid.CurrentCell = paramGrid.Rows[num].Cells["Value"];
			paramGrid.BeginEdit(selectAll: true);
			return;
		}
		if (paramGrid.Rows.Count > 1)
		{
			int index = num - 1;
			if (num == 0)
			{
				index = num + 1;
			}
			for (int i = 0; i < paramGrid.Rows[num].Cells.Count; i++)
			{
				if (paramGrid.Rows[num].Cells[i].OwningColumn.HeaderText != "Index")
				{
					paramGrid.Rows[num].Cells[i].Value = paramGrid.Rows[index].Cells[i].Value;
				}
			}
		}
		paramGrid.CurrentCell = paramGrid.Rows[num].Cells["LL"];
		paramGrid.BeginEdit(selectAll: true);
	}

	private void btnDeleteRow_Click(object sender, EventArgs e)
	{
		if (paramGrid.Rows.Count <= 0)
		{
			return;
		}
		foreach (DataGridViewRow selectedRow in paramGrid.SelectedRows)
		{
			paramGrid.Rows.Remove(selectedRow);
		}
		RefreshIndexNumbering(null);
	}

	private void paramGrid_SelectionChanged(object sender, EventArgs e)
	{
		if (paramGrid.SelectedRows.Count > 0 && IsDotNetTest())
		{
			btnDeleteRow.Enabled = true;
		}
		else
		{
			btnDeleteRow.Enabled = false;
		}
	}

	private void paramGrid_KeyDown(object sender, KeyEventArgs e)
	{
		if (e.KeyCode == Keys.Return && paramGrid.CurrentCell != null && parameter.Type == IOType.Input && paramGrid.CurrentCell.OwningColumn.HeaderText == "Value" && paramGrid.Rows.Count >= paramGrid.CurrentRow.Index + 1)
		{
			if (paramGrid.Rows.Count > paramGrid.CurrentRow.Index + 1)
			{
				DataGridViewRow dataGridViewRow = paramGrid.Rows[paramGrid.CurrentRow.Index + 1];
				paramGrid.CurrentCell = dataGridViewRow.Cells["Value"];
			}
			else if (parameter.IsDynamicArray)
			{
				int index = paramGrid.Rows.Add();
				paramGrid.CurrentCell = paramGrid.Rows[index].Cells["Value"];
			}
			paramGrid.BeginEdit(selectAll: true);
		}
	}

	private void RefreshIndexNumbering(DataGridViewRow row)
	{
		if (row != null)
		{
			row.Cells[0].Value = row.Index;
			row.Selected = false;
			return;
		}
		for (int i = 0; i < paramGrid.Rows.Count; i++)
		{
			paramGrid.Rows[i].Cells[0].Value = i;
		}
		paramGrid.ClearSelection();
	}
}
