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

    public class createShop : Form
    {
        private Button button1;
        private Button button2;
        private Button button3;
        private Button button4;
        private Button button5;
        private ColumnHeader columnHeader1;
        private ColumnHeader columnHeader2;
        private ComboBox comboBox1;
        private IContainer components;
        private GroupBox groupBox1;
        private GroupBox groupBox3;
        private GroupBox groupBox4;
        private int I = -1;
        private bool ItemInfo = true;
        private Label label1;
        private Label label2;
        private ListView listView1;
        private frmMain main;
        private bool MatchFound;
        private NumericUpDown nmrStart;
        private int rowIndex;
        private bool Searching;
        private TextBox searchString;
        private TextBox txtInput;
        private TextBox txtOutput;

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

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.txtOutput.TextLength > 1)
            {
                this.txtOutput.Text = "";
            }
            this.rowIndex = (int) this.nmrStart.Value;
            double num = Math.Ceiling((double) (((double) this.txtInput.Lines.Length) / 6.0));
            int index = 0;
            this.txtOutput.Text = this.txtOutput.Text + ";    NPC Item List created with Shop Creator\r\n#ignore\t\\o042\t\t\t;Ignore quotes\r\n#exchange\t#\t\\x20\t\t; # => space\r\n;\t\t" + DateTime.Today.ToShortDateString() + "\r\n\r\n";
            for (int i = 0; i < num; i++)
            {
                string str = "#Record\t" + this.rowIndex.ToString();
                int num4 = 1;
                while (num4 < 7)
                {
                    if ((this.txtInput.Lines.Length - index) == 0)
                    {
                        str = str + " \t-";
                        num4++;
                    }
                    else
                    {
                        if (num4 == 7)
                        {
                            str = str + "\t\t\t\t";
                            num4++;
                            continue;
                        }
                        if (this.txtInput.Lines[index].Length == 0)
                        {
                            str = str + " \t-";
                        }
                        else
                        {
                            str = str + " \t" + this.txtInput.Lines[index];
                        }
                        index++;
                        num4++;
                    }
                }
                this.txtOutput.Text = this.txtOutput.Text + str + "\r\n";
                this.rowIndex++;
            }
            this.txtOutput.Text = this.txtOutput.Text + "\r\n#END";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.txtOutput.Text = "";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.txtInput.Text = "";
            this.txtOutput.Text = "";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.txtInput.Text = "";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.MatchFound = false;
            this.Searching = true;
            if (!this.ItemInfo)
            {
                MessageBox.Show("Please open ItemInfo.shn before using the search feature.");
                return;
            }
        Label_0019:
            if (this.searchString.Text.Length == 0)
            {
                MessageBox.Show("Please enter a search phrase.");
            }
            else
            {
                this.I++;
                try
                {
                    if (this.I < this.listView1.Items.Count)
                    {
                        if (this.comboBox1.SelectedIndex == 0)
                        {
                            if (this.listView1.Items[this.I].SubItems[0].Text.ToString().ToLower().Contains(this.searchString.Text.ToLower()))
                            {
                                this.resetListView();
                                return;
                            }
                        }
                        else if (this.listView1.Items[this.I].SubItems[1].Text.ToString().ToLower().Contains(this.searchString.Text.ToLower()))
                        {
                            this.resetListView();
                            return;
                        }
                        if (this.MatchFound)
                        {
                            return;
                        }
                        goto Label_0019;
                    }
                    if (this.getItemIndex >= 1)
                    {
                        MessageBox.Show("There are no more items found containing '" + this.searchString.Text + "'");
                    }
                    else if (this.getItemIndex <= 0)
                    {
                        MessageBox.Show("There were no items which contain '" + this.searchString.Text + "'");
                    }
                    this.I = 0;
                    this.listView1.Items[0].Selected = true;
                    this.listView1.Items[0].Focused = true;
                    this.listView1.EnsureVisible(0);
                }
                catch (Exception exception)
                {
                    MessageBox.Show("An exception occurred:\r\n" + exception.Message);
                }
            }
        }

        private void createShop_Load(object sender, EventArgs e)
        {
            this.comboBox1.SelectedIndex = 0;
            this.txtInput.Text = "Example_1\r\nExample_2\r\nExample_3";
            this.button1.PerformClick();
        }

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

        private void init()
        {
            string[] items = new string[] { "Please open ItemInfo.shn to use this." };
            ListViewItem item = new ListViewItem(items);
            if (this.main.FileTab.TabPages.Count > 0)
            {
                if (!this.main.FileTab.SelectedTab.Text.ToString().Contains("ItemInfo"))
                {
                    this.listView1.Items.Add(item);
                    this.listView1.Columns[0].Width = -1;
                    this.ItemInfo = false;
                }
                else
                {
                    foreach (DataGridViewRow row in (IEnumerable) this.main.dataGrid.Rows)
                    {
                        if (row.Index < (row.DataGridView.Rows.Count - 1))
                        {
                            string[] strArray2 = new string[] { row.DataGridView[1, row.Index].Value.ToString(), row.DataGridView[2, row.Index].Value.ToString() };
                            ListViewItem item2 = new ListViewItem(strArray2);
                            this.listView1.Items.Add(item2);
                            this.listView1.Columns[0].Width = -1;
                            this.listView1.Columns[1].Width = -1;
                        }
                    }
                }
            }
            else
            {
                this.listView1.Items.Add(item);
                this.listView1.Columns[0].Width = -1;
                this.ItemInfo = false;
            }
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(createShop));
            this.txtOutput = new TextBox();
            this.txtInput = new TextBox();
            this.nmrStart = new NumericUpDown();
            this.label1 = new Label();
            this.button1 = new Button();
            this.groupBox1 = new GroupBox();
            this.groupBox3 = new GroupBox();
            this.button4 = new Button();
            this.button3 = new Button();
            this.button2 = new Button();
            this.groupBox4 = new GroupBox();
            this.label2 = new Label();
            this.comboBox1 = new ComboBox();
            this.listView1 = new ListView();
            this.columnHeader1 = new ColumnHeader();
            this.columnHeader2 = new ColumnHeader();
            this.button5 = new Button();
            this.searchString = new TextBox();
            GroupBox box = new GroupBox();
            box.SuspendLayout();
            this.nmrStart.BeginInit();
            this.groupBox1.SuspendLayout();
            this.groupBox3.SuspendLayout();
            this.groupBox4.SuspendLayout();
            base.SuspendLayout();
            box.Controls.Add(this.txtOutput);
            box.Location = new Point(0xd6, 0x38);
            box.Name = "groupBox2";
            box.Size = new Size(430, 0x155);
            box.TabIndex = 6;
            box.TabStop = false;
            box.Text = "Output - Shop List";
            this.txtOutput.Location = new Point(6, 0x13);
            this.txtOutput.Multiline = true;
            this.txtOutput.Name = "txtOutput";
            this.txtOutput.ReadOnly = true;
            this.txtOutput.ScrollBars = ScrollBars.Both;
            this.txtOutput.Size = new Size(0x1a2, 0x13c);
            this.txtOutput.TabIndex = 3;
            this.txtOutput.TabStop = false;
            this.txtInput.Location = new Point(6, 0x13);
            this.txtInput.Multiline = true;
            this.txtInput.Name = "txtInput";
            this.txtInput.ScrollBars = ScrollBars.Both;
            this.txtInput.Size = new Size(0xb8, 0x13c);
            this.txtInput.TabIndex = 0;
            this.txtInput.TabStop = false;
            this.nmrStart.Location = new Point(0x60, 0x10);
            int[] bits = new int[4];
            bits[0] = 500;
            this.nmrStart.Maximum = new decimal(bits);
            this.nmrStart.Name = "nmrStart";
            this.nmrStart.Size = new Size(0x51, 20);
            this.nmrStart.TabIndex = 1;
            this.label1.AutoSize = true;
            this.label1.Location = new Point(6, 0x13);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x54, 13);
            this.label1.TabIndex = 2;
            this.label1.Text = "Item Start Index:";
            this.button1.Location = new Point(0xb7, 0x10);
            this.button1.Name = "button1";
            this.button1.Size = new Size(0x4a, 20);
            this.button1.TabIndex = 4;
            this.button1.Text = "Create";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new EventHandler(this.button1_Click);
            this.groupBox1.Controls.Add(this.txtInput);
            this.groupBox1.Location = new Point(12, 0x38);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new Size(0xc4, 0x155);
            this.groupBox1.TabIndex = 5;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Input - Item List";
            this.groupBox3.Controls.Add(this.button4);
            this.groupBox3.Controls.Add(this.button3);
            this.groupBox3.Controls.Add(this.button2);
            this.groupBox3.Controls.Add(this.nmrStart);
            this.groupBox3.Controls.Add(this.label1);
            this.groupBox3.Controls.Add(this.button1);
            this.groupBox3.Location = new Point(12, 6);
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new Size(0x278, 0x2c);
            this.groupBox3.TabIndex = 7;
            this.groupBox3.TabStop = false;
            this.groupBox3.Text = "Tools";
            this.button4.Location = new Point(0x107, 0x10);
            this.button4.Name = "button4";
            this.button4.Size = new Size(0x4a, 20);
            this.button4.TabIndex = 7;
            this.button4.Text = "Clear Input";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new EventHandler(this.button4_Click);
            this.button3.Location = new Point(0x1a7, 0x10);
            this.button3.Name = "button3";
            this.button3.Size = new Size(0x4a, 20);
            this.button3.TabIndex = 6;
            this.button3.Text = "Clear Both";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new EventHandler(this.button3_Click);
            this.button2.Location = new Point(0x157, 0x10);
            this.button2.Name = "button2";
            this.button2.Size = new Size(0x4a, 20);
            this.button2.TabIndex = 5;
            this.button2.Text = "Clear Output";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new EventHandler(this.button2_Click);
            this.groupBox4.Controls.Add(this.label2);
            this.groupBox4.Controls.Add(this.comboBox1);
            this.groupBox4.Controls.Add(this.listView1);
            this.groupBox4.Controls.Add(this.button5);
            this.groupBox4.Controls.Add(this.searchString);
            this.groupBox4.Location = new Point(650, 6);
            this.groupBox4.Name = "groupBox4";
            this.groupBox4.Size = new Size(0x1d8, 0x187);
            this.groupBox4.TabIndex = 9;
            this.groupBox4.TabStop = false;
            this.groupBox4.Text = "Items";
            this.label2.AutoSize = true;
            this.label2.Location = new Point(0x101, 0x13);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x52, 13);
            this.label2.TabIndex = 10;
            this.label2.Text = "Search Column:";
            this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Items.AddRange(new object[] { "InxName", "Name" });
            this.comboBox1.Location = new Point(0x159, 15);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new Size(0x79, 0x15);
            this.comboBox1.TabIndex = 12;
            this.comboBox1.KeyDown += new KeyEventHandler(this.searchString_KeyDown);
            this.listView1.Columns.AddRange(new ColumnHeader[] { this.columnHeader1, this.columnHeader2 });
            this.listView1.FullRowSelect = true;
            this.listView1.GridLines = true;
            this.listView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;
            this.listView1.HideSelection = false;
            this.listView1.Location = new Point(6, 0x2a);
            this.listView1.MultiSelect = false;
            this.listView1.Name = "listView1";
            this.listView1.Size = new Size(460, 0x157);
            this.listView1.TabIndex = 11;
            this.listView1.TabStop = false;
            this.listView1.UseCompatibleStateImageBehavior = false;
            this.listView1.View = View.Details;
            this.listView1.SelectedIndexChanged += new EventHandler(this.listView1_SelectedIndexChanged);
            this.listView1.DoubleClick += new EventHandler(this.listView1_DoubleClick);
            this.listView1.KeyDown += new KeyEventHandler(this.searchString_KeyDown);
            this.columnHeader1.Text = "InxName";
            this.columnHeader1.Width = 100;
            this.columnHeader2.Text = "Name";
            this.columnHeader2.Width = 80;
            this.button5.Location = new Point(180, 0x10);
            this.button5.Name = "button5";
            this.button5.Size = new Size(0x44, 0x15);
            this.button5.TabIndex = 10;
            this.button5.Text = "Search";
            this.button5.UseVisualStyleBackColor = true;
            this.button5.Click += new EventHandler(this.button5_Click);
            this.searchString.Location = new Point(7, 0x10);
            this.searchString.Name = "searchString";
            this.searchString.Size = new Size(0xa7, 20);
            this.searchString.TabIndex = 9;
            this.searchString.TextChanged += new EventHandler(this.searchString_TextChanged);
            this.searchString.KeyDown += new KeyEventHandler(this.searchString_KeyDown);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x46e, 0x195);
            base.Controls.Add(this.groupBox4);
            base.Controls.Add(this.groupBox3);
            base.Controls.Add(box);
            base.Controls.Add(this.groupBox1);
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.MaximizeBox = false;
            base.Name = "createShop";
            base.StartPosition = FormStartPosition.CenterParent;
            this.Text = "NPC Shop List Creator";
            base.Load += new EventHandler(this.createShop_Load);
            box.ResumeLayout(false);
            box.PerformLayout();
            this.nmrStart.EndInit();
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox3.ResumeLayout(false);
            this.groupBox3.PerformLayout();
            this.groupBox4.ResumeLayout(false);
            this.groupBox4.PerformLayout();
            base.ResumeLayout(false);
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (this.txtInput.Lines.Length == 0)
            {
                this.txtInput.Text = this.txtInput.Text + this.listView1.SelectedItems[0].Text;
            }
            else
            {
                this.txtInput.Text = this.txtInput.Text + "\r\n" + this.listView1.SelectedItems[0].Text;
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((this.listView1.FocusedItem != null) & !this.Searching)
            {
                this.I = this.listView1.FocusedItem.Index;
            }
        }

        private void resetListView()
        {
            this.MatchFound = true;
            this.listView1.Items[this.I].Selected = true;
            this.listView1.Items[this.I].Focused = true;
            this.listView1.EnsureVisible(this.I);
            this.listView1.Focus();
            this.Searching = false;
        }

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

        private void searchString_TextChanged(object sender, EventArgs e)
        {
            if (this.ItemInfo)
            {
                this.I = 0;
                this.listView1.Items[0].Selected = true;
                this.listView1.Items[0].Focused = true;
                this.listView1.EnsureVisible(0);
            }
        }

        public int getItemIndex
        {
            get
            {
                if (this.listView1.FocusedItem != null)
                {
                    return this.listView1.FocusedItem.Index;
                }
                return 0;
            }
        }
    }
}

