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

    public class searchFilter : Form
    {
        private Button btnSearch;
        private ComboBox cmbIn;
        private IContainer components;
        private Label label1;
        private Label label2;
        private frmMain main;
        private RadioButton radioContains;
        private RadioButton radioEndsWith;
        private RadioButton radioEquals;
        private RadioButton radioStartsWith;
        private TextBox txtFor;

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

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string[] strArray = this.cmbIn.Text.Split(new char[] { ':' });
            if (string.IsNullOrEmpty(this.txtFor.Text))
            {
                this.main.file.table.DefaultView.RowFilter = null;
            }
            else if (this.radioContains.Checked)
            {
                this.main.file.table.DefaultView.RowFilter = $"Convert({strArray[1]}, 'System.String') LIKE '%{this.txtFor.Text}%'";
            }
            else if (this.radioEquals.Checked)
            {
                this.main.file.table.DefaultView.RowFilter = $"Convert({strArray[1]}, 'System.String') = '{this.txtFor.Text}'";
            }
            else if (this.radioStartsWith.Checked)
            {
                this.main.file.table.DefaultView.RowFilter = $"Convert({strArray[1]}, 'System.String') LIKE '{this.txtFor.Text}*'";
            }
            else if (this.radioEndsWith.Checked)
            {
                this.main.file.table.DefaultView.RowFilter = $"Convert({strArray[1]}, 'System.String') LIKE '*{this.txtFor.Text}'";
            }
        }

        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();
                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(searchFilter));
            this.radioEndsWith = new RadioButton();
            this.radioStartsWith = new RadioButton();
            this.radioEquals = new RadioButton();
            this.radioContains = new RadioButton();
            this.btnSearch = new Button();
            this.cmbIn = new ComboBox();
            this.label2 = new Label();
            this.label1 = new Label();
            this.txtFor = new TextBox();
            base.SuspendLayout();
            this.radioEndsWith.AutoSize = true;
            this.radioEndsWith.Location = new Point(0x119, 0x3f);
            this.radioEndsWith.Name = "radioEndsWith";
            this.radioEndsWith.Size = new Size(0x4a, 0x11);
            this.radioEndsWith.TabIndex = 15;
            this.radioEndsWith.TabStop = true;
            this.radioEndsWith.Text = "Ends With";
            this.radioEndsWith.UseVisualStyleBackColor = true;
            this.radioStartsWith.AutoSize = true;
            this.radioStartsWith.Location = new Point(0x119, 0x2e);
            this.radioStartsWith.Name = "radioStartsWith";
            this.radioStartsWith.Size = new Size(0x4d, 0x11);
            this.radioStartsWith.TabIndex = 14;
            this.radioStartsWith.TabStop = true;
            this.radioStartsWith.Text = "Starts With";
            this.radioStartsWith.UseVisualStyleBackColor = true;
            this.radioEquals.AutoSize = true;
            this.radioEquals.Location = new Point(0x119, 0x1d);
            this.radioEquals.Name = "radioEquals";
            this.radioEquals.Size = new Size(0x39, 0x11);
            this.radioEquals.TabIndex = 13;
            this.radioEquals.TabStop = true;
            this.radioEquals.Text = "Equals";
            this.radioEquals.UseVisualStyleBackColor = true;
            this.radioContains.AutoSize = true;
            this.radioContains.Location = new Point(0x119, 12);
            this.radioContains.Name = "radioContains";
            this.radioContains.Size = new Size(0x42, 0x11);
            this.radioContains.TabIndex = 12;
            this.radioContains.TabStop = true;
            this.radioContains.Text = "Contains";
            this.radioContains.UseVisualStyleBackColor = true;
            this.btnSearch.Location = new Point(80, 0x3d);
            this.btnSearch.Name = "btnSearch";
            this.btnSearch.Size = new Size(0xc2, 0x18);
            this.btnSearch.TabIndex = 10;
            this.btnSearch.Text = "Search";
            this.btnSearch.UseVisualStyleBackColor = true;
            this.btnSearch.Click += new EventHandler(this.btnSearch_Click);
            this.cmbIn.DropDownStyle = ComboBoxStyle.DropDownList;
            this.cmbIn.FormattingEnabled = true;
            this.cmbIn.Location = new Point(80, 0x23);
            this.cmbIn.Name = "cmbIn";
            this.cmbIn.Size = new Size(0xc2, 0x15);
            this.cmbIn.TabIndex = 8;
            this.label2.AutoSize = true;
            this.label2.Location = new Point(15, 0x26);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x37, 13);
            this.label2.TabIndex = 11;
            this.label2.Text = "Search in:";
            this.label1.AutoSize = true;
            this.label1.Location = new Point(15, 12);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x3b, 13);
            this.label1.TabIndex = 9;
            this.label1.Text = "Search for:";
            this.txtFor.Location = new Point(80, 9);
            this.txtFor.Name = "txtFor";
            this.txtFor.Size = new Size(0xc2, 20);
            this.txtFor.TabIndex = 7;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x16e, 0x61);
            base.Controls.Add(this.radioEndsWith);
            base.Controls.Add(this.radioStartsWith);
            base.Controls.Add(this.radioEquals);
            base.Controls.Add(this.radioContains);
            base.Controls.Add(this.btnSearch);
            base.Controls.Add(this.cmbIn);
            base.Controls.Add(this.label2);
            base.Controls.Add(this.label1);
            base.Controls.Add(this.txtFor);
            base.FormBorderStyle = FormBorderStyle.FixedDialog;
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.MaximizeBox = false;
            base.Name = "searchFilter";
            this.Text = "Filter";
            base.Activated += new EventHandler(this.searchFindModern_Activated);
            base.FormClosing += new FormClosingEventHandler(this.searchFindModern_FormClosing);
            base.Load += new EventHandler(this.searchFindModern_Load);
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void searchFindModern_Activated(object sender, EventArgs e)
        {
            this.Init();
            this.Text = "Filter: " + this.main.FileTab.SelectedTab.Text;
        }

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

        private void searchFindModern_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;
        }
    }
}

