using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Skyside3gbySourceEditer
{
    public partial class GoodNameEditer : Form
    {
        private const int GOOD_MAX = 50;

        private int no = 0;//序号
        private String filePath;

        private String goodPath;

        String[] goodName = new String[GOOD_MAX];

        String[] backdata = new String[GOOD_MAX];


        public GoodNameEditer()
        {
            InitializeComponent();
            numericUpDown1.TextChanged += new EventHandler(numericUpDown1_ValueChanged);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if ((folderBrowserDialog1.ShowDialog() == DialogResult.OK) && folderBrowserDialog1.SelectedPath != "")
            {
                filePath = folderBrowserDialog1.SelectedPath;
                textBox1.Text = filePath;
                goodPath = filePath + "\\Res\\goods.sss";

                try
                {
                    //ReadFile();
                    GetGoodNameFromFile();
                    comboBox2_SelectedIndexChanged(sender, e);
                }
                catch (Exception)
                {
                    textBox1.Text = "路径出错，请重新选择！";
                }
            }
        }

        private void GetGoodNameFromFile()
        {
            FileStream fs = new FileStream(goodPath, FileMode.Open);
            StreamReader m_streamReader = new StreamReader(fs, Encoding.GetEncoding("GB2312"));
            m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
            int i = 0;
            while (!m_streamReader.EndOfStream)
            {
                goodName[i] = m_streamReader.ReadLine().Trim();
                i++;
            }
            m_streamReader.Close();
            m_streamReader.Dispose();
            fs.Close();
            fs.Dispose();


        }

        private void button3_Click(object sender, EventArgs e)
        {
            BackUp();
            button4.Enabled = true;
        }

        private void BackUp()
        {
            for (int i = 0; i < GOOD_MAX; i++)
            {
                backdata[i] = goodName[i];

            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ReStore();
        }

        private void ReStore()
        {
            for (int i = 0; i < GOOD_MAX; i++)
            {

                goodName[i] = backdata[i];

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveChange();

            FileStream fs = new FileStream(goodPath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312"));
            for (int i = 0; i < GOOD_MAX; i++)
            {

                sw.Write(goodName[i]);
                sw.Write("\r\n");
            }

            sw.Close();
            fs.Close();
        }

        private void SaveChange()
        {
            try
            {

                goodName[no] = textBox2.Text;//姓名

            }
            catch (Exception) { }
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            SaveChange();
            no = int.Parse(numericUpDown1.Value.ToString()) - 1;
            GetHeroName(int.Parse(numericUpDown1.Value.ToString()) - 1);
        }

        private void GetHeroName(int id)
        {
            try
            {

                textBox2.Text = goodName[id];//姓名

            }
            catch (Exception) { };
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetHeroName(0);
        }



    }
}
