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 HeroNameEditer : Form
    {
        private const int HERO_MAX = 255;

        private int no = 0;//序号
        private String filePath;
        private String path1;
        private String path2;
        private String path3;
        private String path4;

        private String namePath1;
        private String namePath2;
        private String namePath3;
        private String namePath4;

        private String cityPath;

        String[] heroName1 = new String[HERO_MAX];
        String[] heroName2 = new String[HERO_MAX];
        String[] heroName3 = new String[HERO_MAX];
        String[] heroName4 = new String[HERO_MAX];

        int dataFlag = 0;//区分时期

        String[] backdata1 = new String[HERO_MAX];
        String[] backdata2 = new String[HERO_MAX];
        String[] backdata3 = new String[HERO_MAX];
        String[] backdata4 = new String[HERO_MAX];
        
        public HeroNameEditer()
        {
            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;
                namePath1 = filePath + "\\Res\\Hero\\heroname1.sss";
                namePath2 = filePath + "\\Res\\Hero\\heroname2.sss";
                namePath3 = filePath + "\\Res\\Hero\\heroname3.sss";
                namePath4 = filePath + "\\Res\\Hero\\heroname4.sss";

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

        private void GetHeroNameFromFile()
        {
            FileStream fs = new FileStream(namePath1, 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)
            {
                heroName1[i] = m_streamReader.ReadLine().Trim();
                i++;
            }
            m_streamReader.Close();
            m_streamReader.Dispose();
            fs.Close();
            fs.Dispose();

            FileStream fs2 = new FileStream(namePath2, FileMode.Open);
            StreamReader m_streamReader2 = new StreamReader(fs2, Encoding.GetEncoding("GB2312"));
            m_streamReader2.BaseStream.Seek(0, SeekOrigin.Begin);
            i = 0;
            while (!m_streamReader2.EndOfStream)
            {
                heroName2[i] = m_streamReader2.ReadLine().Trim();
                i++;
            }
            m_streamReader2.Close();
            m_streamReader2.Dispose();
            fs2.Close();
            fs2.Dispose();

            FileStream fs3 = new FileStream(namePath3, FileMode.Open);
            StreamReader m_streamReader3 = new StreamReader(fs3, Encoding.GetEncoding("GB2312"));
            m_streamReader3.BaseStream.Seek(0, SeekOrigin.Begin);
            i = 0;
            while (!m_streamReader3.EndOfStream)
            {
                heroName3[i] = m_streamReader3.ReadLine().Trim();
                i++;
            }
            m_streamReader3.Close();
            m_streamReader3.Dispose();
            fs3.Close();
            fs3.Dispose();

            FileStream fs4 = new FileStream(namePath4, FileMode.Open);
            StreamReader m_streamReader4 = new StreamReader(fs4, Encoding.GetEncoding("GB2312"));
            m_streamReader4.BaseStream.Seek(0, SeekOrigin.Begin);
            i = 0;
            while (!m_streamReader4.EndOfStream)
            {
                heroName4[i] = m_streamReader4.ReadLine().Trim();
                i++;
            }
            m_streamReader4.Close();
            m_streamReader4.Dispose();
            fs4.Close();
            fs4.Dispose();
        }

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

        private void BackUp()
        {
            for (int i = 0; i < HERO_MAX; i++)
            {
                backdata1[i] = heroName1[i];
                backdata2[i] = heroName2[i];
                backdata3[i] = heroName3[i];
                backdata4[i] = heroName4[i];
            }
        }

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

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

                heroName1[i] = backdata1[i];
                heroName2[i] = backdata2[i];
                heroName3[i] = backdata3[i];
                heroName4[i] = backdata4[i];
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveChange();
            String tempPath = "";
            switch (dataFlag)
            {
                case 1:
                    tempPath = namePath1;
                    break;
                case 2:
                    tempPath = namePath2;
                    break;
                case 3:
                    tempPath = namePath3;
                    break;
                case 4:
                    tempPath = namePath4;
                    break;
            }
            FileStream fs = new FileStream(tempPath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312"));
            for (int i = 0; i < HERO_MAX; i++)
            {
               
                    switch (dataFlag)
                    {
                        case 1:
                            sw.Write(heroName1[i]);
                            break;
                        case 2:
                            sw.Write(heroName2[i]);
                            break;
                        case 3:
                            sw.Write(heroName3[i]);
                            break;
                        case 4:
                            sw.Write(heroName4[i]);
                            break;

                }
                sw.Write("\r\n");
            }

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

        private void SaveChange()
        {
            try
            {
                switch (dataFlag)
                {
                    case 1:
                        heroName1[no] = textBox2.Text;//姓名
                        break;
                    case 2:
                        heroName2[no] = textBox2.Text;//姓名
                        break;
                    case 3:
                        heroName3[no] = textBox2.Text;//姓名
                        break;
                    case 4:
                        heroName4[no] = textBox2.Text;//姓名
                        break;
                }
            }
            catch (Exception) { }
        }

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

        private void GetHeroName(int id)
        {
            try
            {
                switch (dataFlag)
                {
                    case 1:
                        textBox2.Text = heroName1[id];//姓名
                        
                        break;
                    case 2:
                        textBox2.Text = heroName2[id];//姓名
                        
                        break;
                    case 3:
                        textBox2.Text = heroName3[id];//姓名
                        
                        break;
                    case 4:
                        textBox2.Text = heroName4[id];//姓名
                        
                        break;
                }
            }
            catch (Exception) { };
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataFlag = comboBox2.SelectedIndex + 1;
            GetHeroName(0);
        }

        private void HeroNameEditer_Load(object sender, EventArgs e)
        {
            dataFlag = 1;
            comboBox2.SelectedIndex = 0;
        }

       
    }
}
