namespace SHNDecrypt.Tools
{
    using SHNDecrypt;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Windows.Forms;

    public class toolSetEncoding : Form
    {
        private Button btnChange;
        private Button btnDefault;
        private IContainer components;
        private frmMain enc;
        private ListBox lstEncoding;
        private TextBox tbSearchText;

        public toolSetEncoding(frmMain main)
        {
            this.enc = main;
            this.InitializeComponent();
            this.PopulateEncodingList("");
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.lstEncoding.SelectedItem == null)
                {
                    MessageBox.Show("Please select an encoding type before continuing.");
                }
                else
                {
                    string selectedItem = this.lstEncoding.SelectedItem as string;
                    if (string.IsNullOrEmpty(selectedItem))
                    {
                        MessageBox.Show("Please select an encoding type before continuing.");
                    }
                    Program.rK.SetValue("0", selectedItem);
                    Program.eT = selectedItem;
                    this.lstEncoding.SelectedItem = selectedItem;
                    MessageBox.Show(string.Format("Encoding has been set to {0}. SHN Editor will now read and write in {0}.", selectedItem));
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show("An error occured trying to change the encoding. " + exception.Message);
            }
        }

        private void btnDefault_Click(object sender, EventArgs e)
        {
            Program.rK.SetValue("0", "iso-8859-1");
            Program.eT = "iso-8859-1";
            this.lstEncoding.SelectedItem = "iso-8859-1";
            this.lstEncoding.SelectedIndex = this.lstEncoding.SelectedIndex;
            MessageBox.Show("Encoding has been set to iso-8859-1. SHN Editor will now read and write in iso-8859-1.");
        }

        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(toolSetEncoding));
            this.tbSearchText = new TextBox();
            this.lstEncoding = new ListBox();
            this.btnChange = new Button();
            this.btnDefault = new Button();
            base.SuspendLayout();
            this.tbSearchText.Location = new Point(12, 12);
            this.tbSearchText.Name = "tbSearchText";
            this.tbSearchText.Size = new Size(0xd1, 20);
            this.tbSearchText.TabIndex = 0;
            this.tbSearchText.TextChanged += new EventHandler(this.tbSearchText_TextChanged);
            this.lstEncoding.FormattingEnabled = true;
            this.lstEncoding.Location = new Point(12, 0x26);
            this.lstEncoding.Name = "lstEncoding";
            this.lstEncoding.Size = new Size(0xd1, 160);
            this.lstEncoding.TabIndex = 1;
            this.btnChange.Location = new Point(12, 0xcc);
            this.btnChange.Name = "btnChange";
            this.btnChange.Size = new Size(100, 0x1a);
            this.btnChange.TabIndex = 2;
            this.btnChange.Text = "Change";
            this.btnChange.UseVisualStyleBackColor = true;
            this.btnChange.Click += new EventHandler(this.btnChange_Click);
            this.btnDefault.Location = new Point(0x79, 0xcc);
            this.btnDefault.Name = "btnDefault";
            this.btnDefault.Size = new Size(100, 0x1a);
            this.btnDefault.TabIndex = 3;
            this.btnDefault.Text = "Default";
            this.btnDefault.UseVisualStyleBackColor = true;
            this.btnDefault.Click += new EventHandler(this.btnDefault_Click);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0xe7, 0xef);
            base.Controls.Add(this.btnDefault);
            base.Controls.Add(this.btnChange);
            base.Controls.Add(this.lstEncoding);
            base.Controls.Add(this.tbSearchText);
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "toolSetEncoding";
            base.StartPosition = FormStartPosition.CenterParent;
            this.Text = "Encoding Changer";
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        protected virtual void PopulateEncodingList(string filter = "")
        {
            this.lstEncoding.Items.Clear();
            foreach (EncodingInfo info in from e in Encoding.GetEncodings()
                where e.Name.ToUpper().Contains(filter.ToUpper())
                select e)
            {
                this.lstEncoding.Items.Add(info.Name);
            }
            this.SelectCurrentEncoding();
        }

        protected void SelectCurrentEncoding()
        {
            string eT = Program.eT;
            if (this.lstEncoding.Items.Contains(eT))
            {
                this.lstEncoding.SelectedItems.Add(eT);
            }
        }

        private void tbSearchText_TextChanged(object sender, EventArgs e)
        {
            this.PopulateEncodingList(this.tbSearchText.Text);
        }
    }
}

