using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using QC.QsprShared.Dialogs;

namespace QC.QTMApplication;

public class TTDDataTableParamEdit : Form
{
	private const int NO_COL_SELECTED = -1;

	private const int NO_ROW_SELECTED = -1;

	private const string ADD_COLUMN = "Add Column";

	private const string DELETE_COLUMN = "Delete Column";

	private const string ADD_ROW = "Add Row";

	private const string DELETE_ROW = "Delete Row";

	private const int ADD_COLUMN_INDEX = 0;

	private const int DELETE_COLUMN_INDEX = 1;

	private const int ADD_ROW_INDEX = 2;

	private const int DELETE_ROW_INDEX = 3;

	private ContextMenu cMenu = new ContextMenu();

	private int selectedCol = -1;

	private int selectedRow = -1;

	private DataTable paramDataTable;

	private DataTable tmpParamDataTable = new DataTable("Default");

	private bool modified;

	private IContainer components;

	private DataGridView dataGridView1;

	private Button btnOK;

	private Button btnClear;

	private Button btnReload;

	private Panel panel1;

	private Button btnUpdateSelectedColHeader;

	private TextBox txtColHeaderName;

	private Button btnCancel;

	private Panel panel2;

	private CheckBox chkColHeadersIncluded;

	private Button btnLoadFromFile;

	private Label label1;

	private ToolTip toolTip1;

	private Label label2;

	private Label label3;

	public DataTable ParamDataTable
	{
		get
		{
			return paramDataTable;
		}
		set
		{
			paramDataTable = value;
			if (paramDataTable != null)
			{
				tmpParamDataTable = paramDataTable.Copy();
				dataGridView1.DataSource = tmpParamDataTable;
				dataGridView1.Refresh();
			}
		}
	}

	public TTDDataTableParamEdit()
	{
		InitializeComponent();
		cMenu.MenuItems.Add("Add Column", AddColumn);
		cMenu.MenuItems.Add("Delete Column", DeleteColumn);
		cMenu.MenuItems.Add("Add Row", AddRow);
		cMenu.MenuItems.Add("Delete Row", DeleteRow);
		if (tmpParamDataTable != null)
		{
			DataColumn column = new DataColumn("Col1");
			tmpParamDataTable.Columns.Add(column);
		}
		dataGridView1.DataSource = tmpParamDataTable;
		dataGridView1.Refresh();
	}

	private void btnClear_Click(object sender, EventArgs e)
	{
		if (AppMessageDialog.Show("Do you really want to clear the data in the grid ? ", "Warning", MessageBoxButtons.YesNo, DialogResult.Yes, MessageDialogType.Warning) == DialogResult.Yes)
		{
			tmpParamDataTable.Clear();
			dataGridView1.Refresh();
		}
	}

	private void btnReload_Click(object sender, EventArgs e)
	{
		if (paramDataTable != null)
		{
			tmpParamDataTable = paramDataTable.Copy();
		}
		dataGridView1.DataSource = tmpParamDataTable;
		dataGridView1.Refresh();
		modified = false;
	}

	private void btnOk_Click(object sender, EventArgs e)
	{
		for (int i = 0; i < dataGridView1.Columns.Count; i++)
		{
			tmpParamDataTable.Columns[i].ColumnName = dataGridView1.Columns[i].HeaderText;
			tmpParamDataTable.Columns[i].SetOrdinal(dataGridView1.Columns[i].DisplayIndex);
		}
		paramDataTable = tmpParamDataTable.Copy();
		base.DialogResult = DialogResult.OK;
		Close();
	}

	private void AddColumn(object sender, EventArgs e)
	{
		try
		{
			DataColumn column = new DataColumn("Col" + (tmpParamDataTable.Columns.Count + 1));
			tmpParamDataTable.Columns.Add(column);
			dataGridView1.Refresh();
			modified = true;
		}
		catch (Exception ex)
		{
			AppMessageDialog.Show(ex.Message, DialogResult.OK, MessageDialogType.Exception);
		}
	}

	private void AddRow(object sender, EventArgs e)
	{
		try
		{
			DataRow row = tmpParamDataTable.NewRow();
			tmpParamDataTable.Rows.InsertAt(row, selectedRow);
			dataGridView1.Refresh();
			modified = true;
		}
		catch (Exception ex)
		{
			AppMessageDialog.Show(ex.Message, DialogResult.OK, MessageDialogType.Exception);
		}
	}

	private void DeleteColumn(object sender, EventArgs e)
	{
		if (AppMessageDialog.Show("Do you really want to delete the Column ? ", "Warning", MessageBoxButtons.YesNo, DialogResult.Yes, MessageDialogType.Warning) == DialogResult.Yes)
		{
			tmpParamDataTable.Columns.RemoveAt(selectedCol);
			dataGridView1.Refresh();
			modified = true;
		}
	}

	private void DeleteRow(object sender, EventArgs e)
	{
		if (AppMessageDialog.Show("Do you really want to delete the Row ? ", "Warning", MessageBoxButtons.YesNo, DialogResult.Yes, MessageDialogType.Warning) == DialogResult.Yes)
		{
			try
			{
				tmpParamDataTable.Rows.RemoveAt(selectedRow);
				dataGridView1.Refresh();
				modified = true;
			}
			catch (Exception ex)
			{
				AppMessageDialog.Show(ex.Message, DialogResult.OK, MessageDialogType.Exception);
			}
		}
	}

	private void dataGridView1_MouseUp(object sender, MouseEventArgs e)
	{
		try
		{
			if (e.Button == MouseButtons.Right)
			{
				DataGridView.HitTestInfo hitTestInfo = dataGridView1.HitTest(e.X, e.Y);
				if (hitTestInfo.Type == DataGridViewHitTestType.ColumnHeader)
				{
					selectedCol = hitTestInfo.ColumnIndex;
					selectedRow = -1;
					cMenu.MenuItems[0].Enabled = true;
					cMenu.MenuItems[1].Enabled = true;
					cMenu.MenuItems[2].Enabled = false;
					cMenu.MenuItems[3].Enabled = false;
					cMenu.Show(dataGridView1, new Point(e.X, e.Y));
				}
				else if (hitTestInfo.Type == DataGridViewHitTestType.Cell || hitTestInfo.Type == DataGridViewHitTestType.RowHeader)
				{
					selectedCol = -1;
					selectedRow = hitTestInfo.RowIndex;
					cMenu.MenuItems[0].Enabled = false;
					cMenu.MenuItems[1].Enabled = false;
					cMenu.MenuItems[2].Enabled = true;
					cMenu.MenuItems[3].Enabled = true;
					cMenu.Show(dataGridView1, new Point(e.X, e.Y));
				}
			}
		}
		catch (Exception ex)
		{
			AppMessageDialog.Show(ex.Message, DialogResult.OK, MessageDialogType.Exception);
		}
	}

	private void btnUpdateSelectedColHeader_Click(object sender, EventArgs e)
	{
		if (dataGridView1.SelectedCells.Count > 0 && txtColHeaderName.Text.Length > 0)
		{
			int columnIndex = dataGridView1.SelectedCells[0].ColumnIndex;
			dataGridView1.Columns[columnIndex].HeaderText = txtColHeaderName.Text;
			tmpParamDataTable.Columns[columnIndex].ColumnName = txtColHeaderName.Text;
			modified = true;
		}
	}

	private void btnCancel_Click(object sender, EventArgs e)
	{
		if (!modified || AppMessageDialog.Show("Any changes made will be lost, Continue?", "Warning", MessageBoxButtons.YesNo, DialogResult.Yes, MessageDialogType.Warning) != DialogResult.No)
		{
			base.DialogResult = DialogResult.Cancel;
			Close();
		}
	}

	private void btnLoadFromFile_Click(object sender, EventArgs e)
	{
		if (AppMessageDialog.Show("This will clear the exising data. Continue?", "Warning", MessageBoxButtons.YesNo, DialogResult.Yes, MessageDialogType.Warning) == DialogResult.No)
		{
			return;
		}
		OpenFileDialog openFileDialog = new OpenFileDialog();
		openFileDialog.DefaultExt = "*.csv";
		if (openFileDialog.ShowDialog() != DialogResult.OK)
		{
			return;
		}
		try
		{
			string fileName = openFileDialog.FileName;
			StreamReader streamReader = new StreamReader(fileName);
			if (streamReader.Peek() <= 0)
			{
				return;
			}
			string text = streamReader.ReadLine();
			string[] array = text.Split(',');
			tmpParamDataTable = new DataTable("Default");
			string[] array2 = array;
			foreach (string columnName in array2)
			{
				DataColumn dataColumn = new DataColumn();
				if (chkColHeadersIncluded.Checked)
				{
					dataColumn.ColumnName = columnName;
				}
				tmpParamDataTable.Columns.Add(dataColumn);
			}
			if (!chkColHeadersIncluded.Checked)
			{
				DataRow dataRow = tmpParamDataTable.NewRow();
				dataRow.ItemArray = array;
				tmpParamDataTable.Rows.Add(dataRow);
			}
			while (streamReader.Peek() > 0)
			{
				text = streamReader.ReadLine();
				DataRow dataRow2 = tmpParamDataTable.NewRow();
				dataRow2.ItemArray = text.Split(',');
				tmpParamDataTable.Rows.Add(dataRow2);
			}
			dataGridView1.DataSource = tmpParamDataTable;
			dataGridView1.Refresh();
			modified = true;
		}
		catch (Exception ex)
		{
			AppMessageDialog.Show(ex.Message, DialogResult.OK, MessageDialogType.Exception);
		}
	}

	private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
	{
		try
		{
			dataGridView1.SelectedCells[0].Selected = false;
			dataGridView1.Rows[0].Cells[e.ColumnIndex].Selected = true;
		}
		catch
		{
		}
	}

	private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
	{
		modified = 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();
		System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle = new System.Windows.Forms.DataGridViewCellStyle();
		System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
		System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle();
		this.dataGridView1 = new System.Windows.Forms.DataGridView();
		this.btnOK = new System.Windows.Forms.Button();
		this.btnClear = new System.Windows.Forms.Button();
		this.btnReload = new System.Windows.Forms.Button();
		this.panel1 = new System.Windows.Forms.Panel();
		this.btnUpdateSelectedColHeader = new System.Windows.Forms.Button();
		this.txtColHeaderName = new System.Windows.Forms.TextBox();
		this.btnCancel = new System.Windows.Forms.Button();
		this.panel2 = new System.Windows.Forms.Panel();
		this.chkColHeadersIncluded = new System.Windows.Forms.CheckBox();
		this.btnLoadFromFile = new System.Windows.Forms.Button();
		this.label1 = new System.Windows.Forms.Label();
		this.label2 = new System.Windows.Forms.Label();
		this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
		this.label3 = new System.Windows.Forms.Label();
		((System.ComponentModel.ISupportInitialize)this.dataGridView1).BeginInit();
		this.panel1.SuspendLayout();
		this.panel2.SuspendLayout();
		base.SuspendLayout();
		this.dataGridView1.AllowUserToOrderColumns = true;
		dataGridViewCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
		dataGridViewCellStyle.BackColor = System.Drawing.SystemColors.Control;
		dataGridViewCellStyle.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		dataGridViewCellStyle.ForeColor = System.Drawing.SystemColors.WindowText;
		dataGridViewCellStyle.SelectionBackColor = System.Drawing.SystemColors.Highlight;
		dataGridViewCellStyle.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
		dataGridViewCellStyle.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
		this.dataGridView1.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle;
		this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
		dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
		dataGridViewCellStyle2.BackColor = System.Drawing.SystemColors.Window;
		dataGridViewCellStyle2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		dataGridViewCellStyle2.ForeColor = System.Drawing.SystemColors.ControlText;
		dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
		dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
		dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
		this.dataGridView1.DefaultCellStyle = dataGridViewCellStyle2;
		this.dataGridView1.Location = new System.Drawing.Point(3, 56);
		this.dataGridView1.Name = "dataGridView1";
		dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
		dataGridViewCellStyle3.BackColor = System.Drawing.SystemColors.Control;
		dataGridViewCellStyle3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		dataGridViewCellStyle3.ForeColor = System.Drawing.SystemColors.WindowText;
		dataGridViewCellStyle3.SelectionBackColor = System.Drawing.SystemColors.Highlight;
		dataGridViewCellStyle3.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
		dataGridViewCellStyle3.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
		this.dataGridView1.RowHeadersDefaultCellStyle = dataGridViewCellStyle3;
		this.dataGridView1.Size = new System.Drawing.Size(662, 296);
		this.dataGridView1.TabIndex = 0;
		this.dataGridView1.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
		this.dataGridView1.ColumnHeaderMouseClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(dataGridView1_ColumnHeaderMouseClick);
		this.dataGridView1.MouseUp += new System.Windows.Forms.MouseEventHandler(dataGridView1_MouseUp);
		this.btnOK.Location = new System.Drawing.Point(478, 440);
		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.btnClear.Location = new System.Drawing.Point(13, 382);
		this.btnClear.Name = "btnClear";
		this.btnClear.Size = new System.Drawing.Size(75, 23);
		this.btnClear.TabIndex = 2;
		this.btnClear.Text = "Clear";
		this.toolTip1.SetToolTip(this.btnClear, "Clears the data from the Grid");
		this.btnClear.UseVisualStyleBackColor = true;
		this.btnClear.Click += new System.EventHandler(btnClear_Click);
		this.btnReload.Location = new System.Drawing.Point(103, 382);
		this.btnReload.Name = "btnReload";
		this.btnReload.Size = new System.Drawing.Size(75, 23);
		this.btnReload.TabIndex = 3;
		this.btnReload.Text = "Reload";
		this.toolTip1.SetToolTip(this.btnReload, "Reloads the original data at the time of loading this table.");
		this.btnReload.UseVisualStyleBackColor = true;
		this.btnReload.Click += new System.EventHandler(btnReload_Click);
		this.panel1.Controls.Add(this.btnUpdateSelectedColHeader);
		this.panel1.Controls.Add(this.txtColHeaderName);
		this.panel1.Location = new System.Drawing.Point(3, 13);
		this.panel1.Name = "panel1";
		this.panel1.Size = new System.Drawing.Size(374, 37);
		this.panel1.TabIndex = 4;
		this.btnUpdateSelectedColHeader.Location = new System.Drawing.Point(135, 4);
		this.btnUpdateSelectedColHeader.Name = "btnUpdateSelectedColHeader";
		this.btnUpdateSelectedColHeader.Size = new System.Drawing.Size(212, 23);
		this.btnUpdateSelectedColHeader.TabIndex = 1;
		this.btnUpdateSelectedColHeader.Text = "Update selected Col Header Name";
		this.btnUpdateSelectedColHeader.UseVisualStyleBackColor = true;
		this.btnUpdateSelectedColHeader.Click += new System.EventHandler(btnUpdateSelectedColHeader_Click);
		this.txtColHeaderName.Location = new System.Drawing.Point(10, 4);
		this.txtColHeaderName.Name = "txtColHeaderName";
		this.txtColHeaderName.Size = new System.Drawing.Size(100, 20);
		this.txtColHeaderName.TabIndex = 0;
		this.btnCancel.Location = new System.Drawing.Point(590, 440);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(75, 23);
		this.btnCancel.TabIndex = 5;
		this.btnCancel.Text = "Cancel";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
		this.panel2.Controls.Add(this.chkColHeadersIncluded);
		this.panel2.Controls.Add(this.btnLoadFromFile);
		this.panel2.Location = new System.Drawing.Point(201, 370);
		this.panel2.Name = "panel2";
		this.panel2.Size = new System.Drawing.Size(394, 47);
		this.panel2.TabIndex = 6;
		this.chkColHeadersIncluded.AutoSize = true;
		this.chkColHeadersIncluded.Location = new System.Drawing.Point(6, 17);
		this.chkColHeadersIncluded.Name = "chkColHeadersIncluded";
		this.chkColHeadersIncluded.Size = new System.Drawing.Size(170, 17);
		this.chkColHeadersIncluded.TabIndex = 8;
		this.chkColHeadersIncluded.Text = "Col headers included in the file";
		this.chkColHeadersIncluded.UseVisualStyleBackColor = true;
		this.btnLoadFromFile.Location = new System.Drawing.Point(211, 13);
		this.btnLoadFromFile.Name = "btnLoadFromFile";
		this.btnLoadFromFile.Size = new System.Drawing.Size(183, 23);
		this.btnLoadFromFile.TabIndex = 7;
		this.btnLoadFromFile.Text = "Clear Data and Load from file ...";
		this.btnLoadFromFile.UseVisualStyleBackColor = true;
		this.btnLoadFromFile.Click += new System.EventHandler(btnLoadFromFile_Click);
		this.label1.AutoSize = true;
		this.label1.Location = new System.Drawing.Point(12, 420);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(269, 13);
		this.label1.TabIndex = 7;
		this.label1.Text = "Right click on column headers to add or delete columns";
		this.label2.AutoSize = true;
		this.label2.Location = new System.Drawing.Point(12, 440);
		this.label2.Name = "label2";
		this.label2.Size = new System.Drawing.Size(198, 13);
		this.label2.TabIndex = 8;
		this.label2.Text = "Right click on cells to add or delete rows";
		this.label3.AutoSize = true;
		this.label3.Location = new System.Drawing.Point(12, 464);
		this.label3.Name = "label3";
		this.label3.Size = new System.Drawing.Size(125, 13);
		this.label3.TabIndex = 9;
		this.label3.Text = "Table index starts from 0.";
		this.label3.TextAlign = System.Drawing.ContentAlignment.TopCenter;
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(785, 486);
		base.Controls.Add(this.label3);
		base.Controls.Add(this.label2);
		base.Controls.Add(this.label1);
		base.Controls.Add(this.panel2);
		base.Controls.Add(this.btnCancel);
		base.Controls.Add(this.panel1);
		base.Controls.Add(this.btnReload);
		base.Controls.Add(this.btnClear);
		base.Controls.Add(this.btnOK);
		base.Controls.Add(this.dataGridView1);
		base.Name = "TTDDataTableParamEdit";
		base.ShowIcon = false;
		this.Text = "DataTable Parameter Editor";
		((System.ComponentModel.ISupportInitialize)this.dataGridView1).EndInit();
		this.panel1.ResumeLayout(false);
		this.panel1.PerformLayout();
		this.panel2.ResumeLayout(false);
		this.panel2.PerformLayout();
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
