namespace SHNDecrypt
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Text.RegularExpressions;
    using System.Windows.Forms;

    public class searchReplace : Form
    {
        private Button btnFindNext;
        private Button btnReplace;
        private Button btnReplaceAll;
        private CheckBox checkBox1;
        private ComboBox cmbColumn;
        private IContainer components;
        private string currentTab;
        private int I = -1;
        private Label label1;
        private Label label2;
        private Label label3;
        private bool MatchFound;
        private frmMain searchRep;
        private TextBox txtFind;
        private TextBox txtReplaceWith;

        public searchReplace(frmMain form)
        {
            this.searchRep = form;
            this.InitializeComponent();
        }

        private void btnFindNext_Click(object sender, EventArgs e)
        {
            this.MatchFound = false;
            if (this.I != -1)
            {
                this.I = this.searchRep.dataGrid.CurrentCell.RowIndex;
            }
        Label_002B:
            if (this.txtFind.Text.Length == 0)
            {
                MessageBox.Show("Please enter a search phrase.");
            }
            else
            {
                this.I++;
                try
                {
                    if (this.I < (this.searchRep.dataGrid.Rows.Count - 1))
                    {
                        if (this.searchRep.dataGrid.Rows[this.I].Cells[this.cmbColumn.SelectedIndex].Value.ToString().ToLower().Contains(this.txtFind.Text.ToLower()))
                        {
                            this.searchRep.dataGrid.CurrentCell = this.searchRep.dataGrid.Rows[this.I].Cells[this.cmbColumn.SelectedIndex];
                            this.MatchFound = true;
                            return;
                        }
                        if (this.MatchFound)
                        {
                            return;
                        }
                        goto Label_002B;
                    }
                    if (this.searchRep.dataGrid.CurrentCell.RowIndex >= 1)
                    {
                        MessageBox.Show("There are no more items found containing '" + this.txtFind.Text + "'");
                    }
                    else if (this.searchRep.dataGrid.CurrentCell.RowIndex <= 0)
                    {
                        MessageBox.Show("There were no items which contain '" + this.txtFind.Text + "'");
                    }
                    this.I = -1;
                    this.searchRep.dataGrid.CurrentCell = this.searchRep.dataGrid.Rows[0].Cells[0];
                }
                catch (Exception exception)
                {
                    MessageBox.Show("An exception occurred:\r\n" + exception.Message);
                }
            }
        }

        private void btnReplace_Click(object sender, EventArgs e)
        {
            if (this.I != -1)
            {
                this.replace();
                this.btnFindNext.PerformClick();
            }
            else
            {
                this.btnFindNext.PerformClick();
                this.replace();
            }
        }

        private void btnReplaceAll_Click(object sender, EventArgs e)
        {
            if (!this.checkBox1.Checked)
            {
                this.searchRep.dataGrid.CurrentCell = this.searchRep.dataGrid.Rows[0].Cells[0];
                for (int i = 0; i < (this.searchRep.file.table.Rows.Count - 1); i++)
                {
                    if (this.searchRep.dataGridError)
                    {
                        break;
                    }
                    if (this.searchRep.dataGrid.Rows[i].Cells[this.cmbColumn.SelectedIndex].Value.ToString().ToLower().Contains(this.txtFind.Text.ToLower()))
                    {
                        string str2 = Regex.Replace(this.searchRep.dataGrid.Rows[i].Cells[this.cmbColumn.SelectedIndex].Value.ToString(), this.txtFind.Text, this.txtReplaceWith.Text, RegexOptions.IgnoreCase);
                        this.searchRep.dataGrid.Rows[i].Cells[this.cmbColumn.SelectedIndex].Value = str2;
                    }
                }
            }
            else if (this.searchRep.dataGrid.SelectedRows.Count != 0)
            {
                foreach (DataGridViewRow row in this.searchRep.dataGrid.SelectedRows)
                {
                    if (this.searchRep.dataGridError)
                    {
                        break;
                    }
                    if (this.searchRep.dataGrid.Rows[row.Index].Cells[this.cmbColumn.SelectedIndex].Value.ToString().ToLower().Contains(this.txtFind.Text.ToLower()))
                    {
                        string str4 = Regex.Replace(this.searchRep.dataGrid.Rows[row.Index].Cells[this.cmbColumn.SelectedIndex].Value.ToString(), this.txtFind.Text, this.txtReplaceWith.Text, RegexOptions.IgnoreCase);
                        this.searchRep.dataGrid.Rows[row.Index].Cells[this.cmbColumn.SelectedIndex].Value = str4;
                    }
                }
            }
            else
            {
                MessageBox.Show("Please select the appropriate rows to replace all values in.");
                this.searchRep.SQLStatus.Text = "Please select the appropriate rows to replace all values in.";
            }
            this.searchRep.SQLStatus.Text = "Replace completed.";
        }

        private void cmbColumn_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.currentTab = this.searchRep.FileTab.SelectedTab.Text;
        }

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

        public void init()
        {
            if (this.searchRep.file.table != null)
            {
                this.cmbColumn.Items.Clear();
                for (int i = 0; i < this.searchRep.file.table.Columns.Count; i++)
                {
                    this.cmbColumn.Items.Add(this.searchRep.file.table.Columns[i].ColumnName);
                }
                this.cmbColumn.SelectedIndex = 0;
            }
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(searchReplace));
            this.label1 = new Label();
            this.label2 = new Label();
            this.txtFind = new TextBox();
            this.txtReplaceWith = new TextBox();
            this.btnFindNext = new Button();
            this.btnReplace = new Button();
            this.btnReplaceAll = new Button();
            this.label3 = new Label();
            this.cmbColumn = new ComboBox();
            this.checkBox1 = new CheckBox();
            base.SuspendLayout();
            this.label1.AutoSize = true;
            this.label1.Location = new Point(0x1f, 15);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x38, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Find what:";
            this.label2.AutoSize = true;
            this.label2.Location = new Point(15, 0x29);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x48, 13);
            this.label2.TabIndex = 1;
            this.label2.Text = "Replace with:";
            this.txtFind.Location = new Point(0x5d, 12);
            this.txtFind.Name = "txtFind";
            this.txtFind.Size = new Size(0x7d, 20);
            this.txtFind.TabIndex = 1;
            this.txtFind.KeyDown += new KeyEventHandler(this.txtFind_KeyDown);
            this.txtReplaceWith.Location = new Point(0x5d, 0x26);
            this.txtReplaceWith.Name = "txtReplaceWith";
            this.txtReplaceWith.Size = new Size(0x7d, 20);
            this.txtReplaceWith.TabIndex = 2;
            this.txtReplaceWith.KeyDown += new KeyEventHandler(this.txtFind_KeyDown);
            this.btnFindNext.Location = new Point(0xe0, 11);
            this.btnFindNext.Name = "btnFindNext";
            this.btnFindNext.Size = new Size(0x4b, 0x15);
            this.btnFindNext.TabIndex = 4;
            this.btnFindNext.Text = "Find Next";
            this.btnFindNext.UseVisualStyleBackColor = true;
            this.btnFindNext.Click += new EventHandler(this.btnFindNext_Click);
            this.btnReplace.Location = new Point(0xe0, 0x25);
            this.btnReplace.Name = "btnReplace";
            this.btnReplace.Size = new Size(0x4b, 0x15);
            this.btnReplace.TabIndex = 5;
            this.btnReplace.Text = "Replace";
            this.btnReplace.UseVisualStyleBackColor = true;
            this.btnReplace.Click += new EventHandler(this.btnReplace_Click);
            this.btnReplaceAll.Location = new Point(0xe0, 0x40);
            this.btnReplaceAll.Name = "btnReplaceAll";
            this.btnReplaceAll.Size = new Size(0x4b, 0x16);
            this.btnReplaceAll.TabIndex = 6;
            this.btnReplaceAll.Text = "Replace All";
            this.btnReplaceAll.UseVisualStyleBackColor = true;
            this.btnReplaceAll.Click += new EventHandler(this.btnReplaceAll_Click);
            this.label3.AutoSize = true;
            this.label3.Location = new Point(0x1f, 0x44);
            this.label3.Name = "label3";
            this.label3.Size = new Size(0x38, 13);
            this.label3.TabIndex = 8;
            this.label3.Text = "In column:";
            this.cmbColumn.DropDownStyle = ComboBoxStyle.DropDownList;
            this.cmbColumn.FormattingEnabled = true;
            this.cmbColumn.Location = new Point(0x5d, 0x41);
            this.cmbColumn.Name = "cmbColumn";
            this.cmbColumn.Size = new Size(0x7d, 0x15);
            this.cmbColumn.TabIndex = 3;
            this.cmbColumn.SelectedIndexChanged += new EventHandler(this.cmbColumn_SelectedIndexChanged);
            this.checkBox1.AutoSize = true;
            this.checkBox1.Location = new Point(0x5d, 0x5c);
            this.checkBox1.Name = "checkBox1";
            this.checkBox1.Size = new Size(0x52, 0x11);
            this.checkBox1.TabIndex = 0x10;
            this.checkBox1.Text = "In Selection";
            this.checkBox1.UseVisualStyleBackColor = true;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x133, 0x73);
            base.Controls.Add(this.checkBox1);
            base.Controls.Add(this.label3);
            base.Controls.Add(this.cmbColumn);
            base.Controls.Add(this.txtFind);
            base.Controls.Add(this.txtReplaceWith);
            base.Controls.Add(this.btnFindNext);
            base.Controls.Add(this.label1);
            base.Controls.Add(this.btnReplace);
            base.Controls.Add(this.btnReplaceAll);
            base.Controls.Add(this.label2);
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "searchReplace";
            base.Opacity = 0.9;
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "Replace";
            base.Activated += new EventHandler(this.searchReplace_Activated);
            base.Deactivate += new EventHandler(this.searchReplace_Deactivate);
            base.Load += new EventHandler(this.searchReplace_Load);
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        public void replace()
        {
            try
            {
                if (this.searchRep.dataGrid.Rows[this.I].Cells[this.cmbColumn.SelectedIndex].Value.ToString().ToLower().Contains(this.txtFind.Text.ToLower()))
                {
                    string input = this.searchRep.dataGrid.CurrentCell.Value.ToString();
                    this.searchRep.dataGrid.CurrentCell.Value = Regex.Replace(input, this.txtFind.Text, this.txtReplaceWith.Text, RegexOptions.IgnoreCase);
                }
            }
            catch (IndexOutOfRangeException)
            {
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        private void searchReplace_Activated(object sender, EventArgs e)
        {
            base.Opacity = 1.0;
            if (this.searchRep.FileTab.SelectedTab.Text != this.currentTab)
            {
                this.init();
            }
        }

        private void searchReplace_Deactivate(object sender, EventArgs e)
        {
            base.Opacity = 0.7;
        }

        private void searchReplace_Load(object sender, EventArgs e)
        {
            if (this.searchRep.file == null)
            {
                base.Close();
            }
            else
            {
                this.currentTab = this.searchRep.FileTab.SelectedTab.Text;
                this.init();
            }
        }

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

