namespace SHNDecrypt
{
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class createRow : Form
    {
        private Button button1;
        private IContainer components;
        private Label label1;
        private Label label2;
        private frmMain main;
        private TextBox textBox1;
        private TextBox textBox2;

        public createRow(frmMain form)
        {
            this.InitializeComponent();
            this.main = form;
        }

        private void AddARow(DataTable table)
        {
            DataRow row = table.NewRow();
            table.Rows.Add(row);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if ((this.main.file != null) && (this.main.dataGrid != null))
            {
                if ((this.textBox1.TextLength == 0) & (this.textBox2.TextLength == 0))
                {
                    this.AddARow(this.main.file.table);
                    this.main.dataGrid.CurrentCell = this.main.dataGrid[0, this.main.file.table.Rows.Count - 1];
                    this.main.dataGrid.Rows[this.main.file.table.Rows.Count - 1].Selected = true;
                }
                else if (this.textBox1.TextLength <= 0)
                {
                    if (this.textBox2.TextLength > 0)
                    {
                        int sourceRowID = Convert.ToInt32(this.textBox2.Text);
                        if (sourceRowID < this.main.file.table.Rows.Count)
                        {
                            this.AddARow(this.main.file.table);
                            this.CopyRows(this.main.dataGrid, sourceRowID, this.main.file.table.Rows.Count - 1);
                            this.main.dataGrid.CurrentCell = this.main.dataGrid[0, this.main.file.table.Rows.Count - 1];
                            this.main.dataGrid.Rows[this.main.file.table.Rows.Count - 1].Selected = true;
                        }
                    }
                }
                else
                {
                    DataRow row = this.main.file.table.NewRow();
                    if (this.main.dataGrid.Rows[this.main.dataGrid.NewRowIndex].Selected)
                    {
                        this.main.dataGrid.CurrentCell = this.main.dataGrid.Rows[this.main.file.table.Rows.Count - 1].Cells[0];
                    }
                    for (int i = 0; i < this.main.file.table.Columns.Count; i++)
                    {
                        string columnName = this.main.file.table.Columns[i].ColumnName;
                        try
                        {
                            row[i] = this.textBox1.Text;
                        }
                        catch (Exception exception)
                        {
                            MessageBox.Show("An unknown error occured: " + Environment.NewLine + exception.Message);
                            this.main.SQLStatus.Text = "Clipboard has wrong type of data.";
                            break;
                        }
                    }
                    this.main.file.table.Rows.Add(row);
                    this.main.dataGrid.CurrentCell = this.main.dataGrid[0, this.main.file.table.Rows.Count - 1];
                    this.main.dataGrid.Rows[this.main.file.table.Rows.Count - 1].Selected = true;
                }
            }
        }

        private void CopyRows(DataGridView dataGrid, int SourceRowID, int DestinationRowID)
        {
            for (int i = 0; i < dataGrid.Rows[SourceRowID].Cells.Count; i++)
            {
                dataGrid.Rows[DestinationRowID].Cells[i].Value = dataGrid.Rows[SourceRowID].Cells[i].Value;
            }
        }

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

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(createRow));
            this.textBox1 = new TextBox();
            this.label1 = new Label();
            this.button1 = new Button();
            this.label2 = new Label();
            this.textBox2 = new TextBox();
            base.SuspendLayout();
            this.textBox1.Location = new Point(0x57, 12);
            this.textBox1.MaxLength = 0x40;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new Size(0x75, 20);
            this.textBox1.TabIndex = 0;
            this.textBox1.TextChanged += new EventHandler(this.textBox1_TextChanged);
            this.textBox1.KeyDown += new KeyEventHandler(this.textBox1_KeyDown);
            this.label1.AutoSize = true;
            this.label1.Location = new Point(7, 15);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x4a, 13);
            this.label1.TabIndex = 1;
            this.label1.Text = "Default Value:";
            this.button1.Location = new Point(0x57, 0x40);
            this.button1.Name = "button1";
            this.button1.Size = new Size(0x75, 0x1c);
            this.button1.TabIndex = 2;
            this.button1.Text = "Create Row";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new EventHandler(this.button1_Click);
            this.label2.AutoSize = true;
            this.label2.Location = new Point(0x16, 0x29);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x3b, 13);
            this.label2.TabIndex = 4;
            this.label2.Text = "Base Row:";
            this.textBox2.Location = new Point(0x57, 0x26);
            this.textBox2.MaxLength = 0x20;
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new Size(0x75, 20);
            this.textBox2.TabIndex = 3;
            this.textBox2.TextChanged += new EventHandler(this.textBox2_TextChanged);
            this.textBox2.KeyDown += new KeyEventHandler(this.textBox2_KeyDown);
            this.textBox2.KeyPress += new KeyPressEventHandler(this.textBox2_KeyPress);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0xd8, 0x68);
            base.Controls.Add(this.label2);
            base.Controls.Add(this.textBox2);
            base.Controls.Add(this.button1);
            base.Controls.Add(this.label1);
            base.Controls.Add(this.textBox1);
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "CreateRow";
            base.StartPosition = FormStartPosition.CenterParent;
            this.Text = "Create Row";
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.button1.PerformClick();
                e.SuppressKeyPress = true;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox1.TextLength >= 1)
            {
                this.textBox2.Enabled = false;
            }
            else
            {
                this.textBox2.Enabled = true;
            }
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.button1.PerformClick();
                e.SuppressKeyPress = true;
            }
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox2.TextLength > 0)
            {
                this.textBox1.Enabled = false;
            }
            else
            {
                this.textBox1.Enabled = true;
            }
        }
    }
}

