namespace SHNDecrypt.Search
{
    using SHNDecrypt;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text.RegularExpressions;
    using System.Windows.Forms;

    public class searchFindOld : Form
    {
        private Button button2;
        private ComboBox cmbIn;
        private IContainer components;
        private DataGridView dataGrid;
        private Label label1;
        private Label label2;
        private frmMain main;
        private Dictionary<int, int> originIndex = new Dictionary<int, int>();
        private Panel panel1;
        private RadioButton radioContains;
        private RadioButton radioEndsWith;
        private RadioButton radioEquals;
        private RadioButton radioStartsWith;
        private TextBox txtFor;

        public searchFindOld(frmMain main)
        {
            this.main = main;
            this.InitializeComponent();
            this.dataGrid.DoubleBuffered(true);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.dataGrid.Columns.Count > 1)
            {
                this.dataGrid.DataSource = null;
                this.originIndex.Clear();
            }
            try
            {
                if (this.main.file == null)
                {
                    return;
                }
                int num = Convert.ToInt32(this.cmbIn.Text.Substring(0, 2));
                DataTable table = new DataTable();
                table = this.main.file.table.Clone();
                for (int i = 0; i < this.main.file.table.Rows.Count; i++)
                {
                    DataRow row = this.main.file.table.Rows[i];
                    if (this.radioContains.Checked)
                    {
                        if (this.Contains(row[num].ToString().ToLower(), this.txtFor.Text.ToLower()))
                        {
                            this.originIndex.Add(table.Rows.Count, i);
                            table.ImportRow(row);
                        }
                    }
                    else if (this.radioEquals.Checked)
                    {
                        if (object.Equals(row[num].ToString().ToLower(), this.txtFor.Text.ToLower()))
                        {
                            this.originIndex.Add(table.Rows.Count, i);
                            table.ImportRow(row);
                        }
                    }
                    else if (this.radioEndsWith.Checked)
                    {
                        if (row[num].ToString().ToLower().EndsWith(this.txtFor.Text.ToLower()))
                        {
                            this.originIndex.Add(table.Rows.Count, i);
                            table.ImportRow(row);
                        }
                    }
                    else if (this.radioStartsWith.Checked && row[num].ToString().ToLower().StartsWith(this.txtFor.Text.ToLower()))
                    {
                        this.originIndex.Add(table.Rows.Count, i);
                        table.ImportRow(row);
                    }
                }
                this.dataGrid.DataSource = table;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
            if (string.IsNullOrEmpty(this.txtFor.Text))
            {
                ((DataTable) this.dataGrid.DataSource).DefaultView.Sort = string.Empty;
            }
        }

        private void button2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.button2.PerformClick();
                e.SuppressKeyPress = true;
            }
            if (e.KeyCode == Keys.Escape)
            {
                e.SuppressKeyPress = true;
                base.Hide();
            }
        }

        public bool Contains(string input, string Filter)
        {
            Regex regex = new Regex(Filter);
            return regex.IsMatch(input);
        }

        private void dataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (((DataTable) this.dataGrid.DataSource).DefaultView.Sort == string.Empty)
                {
                    if (!this.originIndex.ContainsKey(e.RowIndex))
                    {
                        this.main.dataGrid.CurrentCell = this.main.dataGrid.Rows[this.dataGrid.CurrentCell.RowIndex].Cells[this.dataGrid.CurrentCell.ColumnIndex];
                    }
                    else
                    {
                        this.main.dataGrid.CurrentCell = this.main.dataGrid.Rows[this.originIndex[e.RowIndex]].Cells[this.dataGrid.CurrentCell.ColumnIndex];
                    }
                }
            }
            catch
            {
            }
        }

        private void dataGrid_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (!this.originIndex.ContainsKey(e.RowIndex))
            {
                this.main.dataGrid.ClearSelection();
                this.main.dataGrid.FirstDisplayedCell = this.main.dataGrid[0, this.dataGrid.CurrentCell.RowIndex];
                this.main.dataGrid.Rows[this.dataGrid.CurrentCell.RowIndex].Selected = true;
            }
            else
            {
                this.main.dataGrid.ClearSelection();
                this.main.dataGrid.FirstDisplayedCell = this.main.dataGrid[0, this.originIndex[e.RowIndex]];
                this.main.dataGrid.Rows[this.originIndex[e.RowIndex]].Selected = true;
            }
        }

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

        public void Init()
        {
            if (this.main.file != null)
            {
                this.cmbIn.Items.Clear();
                this.originIndex.Clear();
                for (int i = 0; i < this.main.file.table.Columns.Count; i++)
                {
                    DataColumn column = this.main.file.table.Columns[i];
                    this.cmbIn.Items.Add(i.ToString("00") + ": " + column.ToString());
                }
                this.cmbIn.SelectedIndex = 0;
            }
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(searchFindOld));
            this.button2 = new Button();
            this.cmbIn = new ComboBox();
            this.label2 = new Label();
            this.label1 = new Label();
            this.txtFor = new TextBox();
            this.dataGrid = new DataGridView();
            this.panel1 = new Panel();
            this.radioEndsWith = new RadioButton();
            this.radioStartsWith = new RadioButton();
            this.radioEquals = new RadioButton();
            this.radioContains = new RadioButton();
            ((ISupportInitialize) this.dataGrid).BeginInit();
            this.panel1.SuspendLayout();
            base.SuspendLayout();
            this.button2.Location = new Point(0x4f, 0x40);
            this.button2.Name = "button2";
            this.button2.Size = new Size(0xc2, 0x17);
            this.button2.TabIndex = 5;
            this.button2.Text = "Search";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new EventHandler(this.button2_Click);
            this.button2.KeyDown += new KeyEventHandler(this.button2_KeyDown);
            this.cmbIn.DropDownStyle = ComboBoxStyle.DropDownList;
            this.cmbIn.FormattingEnabled = true;
            this.cmbIn.Location = new Point(0x4f, 0x26);
            this.cmbIn.Name = "cmbIn";
            this.cmbIn.Size = new Size(0xc2, 0x15);
            this.cmbIn.TabIndex = 3;
            this.label2.AutoSize = true;
            this.label2.Location = new Point(14, 0x29);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x37, 13);
            this.label2.TabIndex = 2;
            this.label2.Text = "Search in:";
            this.label1.AutoSize = true;
            this.label1.Location = new Point(14, 15);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x3b, 13);
            this.label1.TabIndex = 1;
            this.label1.Text = "Search for:";
            this.txtFor.Location = new Point(0x4f, 12);
            this.txtFor.Name = "txtFor";
            this.txtFor.Size = new Size(0xc2, 20);
            this.txtFor.TabIndex = 0;
            this.txtFor.KeyDown += new KeyEventHandler(this.button2_KeyDown);
            this.dataGrid.AllowUserToAddRows = false;
            this.dataGrid.AllowUserToDeleteRows = false;
            this.dataGrid.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGrid.Dock = DockStyle.Fill;
            this.dataGrid.Location = new Point(0, 0x62);
            this.dataGrid.Name = "dataGrid";
            this.dataGrid.ReadOnly = true;
            this.dataGrid.Size = new Size(0x2e7, 0x1cd);
            this.dataGrid.TabIndex = 2;
            this.dataGrid.CellDoubleClick += new DataGridViewCellEventHandler(this.dataGrid_CellClick);
            this.dataGrid.RowHeaderMouseDoubleClick += new DataGridViewCellMouseEventHandler(this.dataGrid_RowHeaderMouseClick);
            this.panel1.Controls.Add(this.radioEndsWith);
            this.panel1.Controls.Add(this.radioStartsWith);
            this.panel1.Controls.Add(this.radioEquals);
            this.panel1.Controls.Add(this.radioContains);
            this.panel1.Controls.Add(this.button2);
            this.panel1.Controls.Add(this.label1);
            this.panel1.Controls.Add(this.txtFor);
            this.panel1.Controls.Add(this.cmbIn);
            this.panel1.Controls.Add(this.label2);
            this.panel1.Dock = DockStyle.Top;
            this.panel1.Location = new Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new Size(0x2e7, 0x62);
            this.panel1.TabIndex = 6;
            this.radioEndsWith.AutoSize = true;
            this.radioEndsWith.Location = new Point(280, 0x40);
            this.radioEndsWith.Name = "radioEndsWith";
            this.radioEndsWith.Size = new Size(0x4a, 0x11);
            this.radioEndsWith.TabIndex = 10;
            this.radioEndsWith.TabStop = true;
            this.radioEndsWith.Text = "Ends With";
            this.radioEndsWith.UseVisualStyleBackColor = true;
            this.radioStartsWith.AutoSize = true;
            this.radioStartsWith.Location = new Point(280, 0x2f);
            this.radioStartsWith.Name = "radioStartsWith";
            this.radioStartsWith.Size = new Size(0x4d, 0x11);
            this.radioStartsWith.TabIndex = 9;
            this.radioStartsWith.TabStop = true;
            this.radioStartsWith.Text = "Starts With";
            this.radioStartsWith.UseVisualStyleBackColor = true;
            this.radioEquals.AutoSize = true;
            this.radioEquals.Location = new Point(280, 30);
            this.radioEquals.Name = "radioEquals";
            this.radioEquals.Size = new Size(0x39, 0x11);
            this.radioEquals.TabIndex = 8;
            this.radioEquals.TabStop = true;
            this.radioEquals.Text = "Equals";
            this.radioEquals.UseVisualStyleBackColor = true;
            this.radioContains.AutoSize = true;
            this.radioContains.Location = new Point(280, 13);
            this.radioContains.Name = "radioContains";
            this.radioContains.Size = new Size(0x42, 0x11);
            this.radioContains.TabIndex = 7;
            this.radioContains.TabStop = true;
            this.radioContains.Text = "Contains";
            this.radioContains.UseVisualStyleBackColor = true;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x2e7, 0x22f);
            base.Controls.Add(this.dataGrid);
            base.Controls.Add(this.panel1);
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.Name = "searchFindOld";
            base.StartPosition = FormStartPosition.CenterParent;
            this.Text = "Find";
            base.Activated += new EventHandler(this.searchFindOld_Activated);
            base.FormClosing += new FormClosingEventHandler(this.SearchForm_FormClosing);
            base.Load += new EventHandler(this.SearchForm_Load);
            ((ISupportInitialize) this.dataGrid).EndInit();
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            base.ResumeLayout(false);
        }

        private void searchFindOld_Activated(object sender, EventArgs e)
        {
            if (this.Text.Trim().Split(new char[] { ':' })[1].Trim() != this.main.FileTab.SelectedTab.Text.Trim())
            {
                this.Init();
                this.Text = "Find: " + this.main.FileTab.SelectedTab.Text;
            }
        }

        private void SearchForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            base.Hide();
        }

        private void SearchForm_Load(object sender, EventArgs e)
        {
            if (this.main != null)
            {
                base.Location = new Point((this.main.Location.X + (this.main.Width / 2)) - (base.Width / 2), (this.main.Location.Y + (this.main.Height / 2)) - (base.Height / 2));
            }
            this.Init();
            this.radioContains.Checked = true;
            this.button2_Click(null, null);
            base.ActiveControl = this.txtFor;
        }
    }
}

