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 HeroDataEditer : Form
    {
        private const int HERO_MAX = 300;
        private const int HERODATALENTH_MAX = 16;
        private const int CITY_MAX = 50;

        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];
        String[] cityName = new String[CITY_MAX];
        int dataFlag = 0;//区分时期

        int[,] data1 = new int[HERO_MAX, HERODATALENTH_MAX];
        int[,] data2 = new int[HERO_MAX, HERODATALENTH_MAX];
        int[,] data3 = new int[HERO_MAX, HERODATALENTH_MAX];
        int[,] data4 = new int[HERO_MAX, HERODATALENTH_MAX];

        int[,] backdata1 = new int[HERO_MAX, HERODATALENTH_MAX];
        int[,] backdata2 = new int[HERO_MAX, HERODATALENTH_MAX];
        int[,] backdata3 = new int[HERO_MAX, HERODATALENTH_MAX];
        int[,] backdata4 = new int[HERO_MAX, HERODATALENTH_MAX];
        public HeroDataEditer()
        {
            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;
                path1 = filePath + "\\Res\\Hero\\hero1.sdt";
                path2 = filePath + "\\Res\\Hero\\hero2.sdt";
                path3 = filePath + "\\Res\\Hero\\hero3.sdt";
                path4 = filePath + "\\Res\\Hero\\hero4.sdt";
                namePath1 = filePath + "\\Res\\Hero\\heroname1.sss";
                namePath2 = filePath + "\\Res\\Hero\\heroname2.sss";
                namePath3 = filePath + "\\Res\\Hero\\heroname3.sss";
                namePath4 = filePath + "\\Res\\Hero\\heroname4.sss";
                cityPath = filePath + "\\Res\\citynamebylist.sss";

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

        private void ReadFile()
        {
            FileStream fs = new FileStream(path1, FileMode.Open); 
            StreamReader m_streamReader = new StreamReader(fs);
            m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
            int i = 0;
            while (!m_streamReader.EndOfStream)
            { 
                string[] line = m_streamReader.ReadLine().Split(' ');
                for (int j = 0; j < HERODATALENTH_MAX; j++)
                {
                    data1[i, j] = int.Parse(line[j]);
                }
                i++;
            }
            m_streamReader.Close(); 
            m_streamReader.Dispose(); 
            fs.Close(); 
            fs.Dispose();

            FileStream fs2 = new FileStream(path2, FileMode.Open);
            StreamReader m_streamReader2 = new StreamReader(fs2);
            m_streamReader2.BaseStream.Seek(0, SeekOrigin.Begin);
            i = 0;
            while (!m_streamReader2.EndOfStream)
            {
                string[] line = m_streamReader2.ReadLine().Split(' ');
                for (int j = 0; j < HERODATALENTH_MAX; j++)
                {
                    data2[i, j] = int.Parse(line[j]);
                }
                i++;
            }
            m_streamReader2.Close();
            m_streamReader2.Dispose();
            fs2.Close();
            fs2.Dispose();

            FileStream fs3 = new FileStream(path3, FileMode.Open);
            StreamReader m_streamReader3 = new StreamReader(fs3);
            m_streamReader3.BaseStream.Seek(0, SeekOrigin.Begin);
            i = 0;
            while (!m_streamReader3.EndOfStream)
            {
                string[] line = m_streamReader3.ReadLine().Split(' ');
                for (int j = 0; j < HERODATALENTH_MAX; j++)
                {
                    data3[i, j] = int.Parse(line[j]);
                }
                i++;
            }
            m_streamReader3.Close();
            m_streamReader3.Dispose();
            fs3.Close();
            fs3.Dispose();

            FileStream fs4 = new FileStream(path4, FileMode.Open);
            StreamReader m_streamReader4 = new StreamReader(fs4);
            m_streamReader4.BaseStream.Seek(0, SeekOrigin.Begin);
            i = 0;
            while (!m_streamReader4.EndOfStream)
            {
                string[] line = m_streamReader4.ReadLine().Split(' ');
                for (int j = 0; j < HERODATALENTH_MAX; j++)
                {
                    data4[i, j] = int.Parse(line[j]);
                }
                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++)
            {
                for (int j = 0; j < HERODATALENTH_MAX; j++)
                {
                    backdata1[i, j] = data1[i, j];
                    backdata2[i, j] = data2[i, j];
                    backdata3[i, j] = data3[i, j];
                    backdata4[i, j] = data4[i, j];
                }
            }
        }

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

        private void ReStore()
        {
            for (int i = 0; i < HERO_MAX; i++)
            {
                for (int j = 0; j < HERODATALENTH_MAX; j++)
                {
                    data1[i, j] = backdata1[i, j];
                    data2[i, j] = backdata2[i, j];
                    data3[i, j] = backdata3[i, j];
                    data4[i, j] = backdata4[i, j];
                }
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            //comboBox1.SelectedIndex = 0;
            dataFlag = comboBox2.SelectedIndex+1;
            //textBox2.Text = dataFlag.ToString();
            try
            {
                switch (dataFlag)
                {
                    case 1:
                        comboBox7.Items.Clear();
                        comboBox3.Items.Clear();
                        for (int i = 0; i < heroName1.Length; i++)
                        {
                            comboBox7.Items.Add(heroName1[i]);
                            comboBox3.Items.Add(heroName1[i]);
                        }
                        break;
                    case 2:
                        comboBox7.Items.Clear();
                        comboBox3.Items.Clear();
                        for (int i = 0; i < heroName2.Length; i++)
                        {
                            comboBox7.Items.Add(heroName2[i]);
                            comboBox3.Items.Add(heroName2[i]);
                        }
                        break;
                    case 3:
                        comboBox7.Items.Clear();
                        comboBox3.Items.Clear();
                        for (int i = 0; i < heroName3.Length; i++)
                        {
                            comboBox7.Items.Add(heroName3[i]);
                            comboBox3.Items.Add(heroName3[i]);
                        }
                        break;
                    case 4:
                        comboBox7.Items.Clear();
                        comboBox3.Items.Clear();
                        for (int i = 0; i < heroName4.Length; i++)
                        {
                            comboBox7.Items.Add(heroName4[i]);
                            comboBox3.Items.Add(heroName4[i]);
                        }
                        break;
                }
            }
            catch(Exception){}
            GetHeroData(int.Parse(numericUpDown1.Value.ToString()));

        }

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

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

        }

        private void SaveChange()
        {
            try
            {
                switch (dataFlag)
                {
                    case 1:
                        data1[no, 0] = no;//序号
                        data1[no, 2] = int.Parse(textBox6.Text);//等级 
                        data1[no, 3] = int.Parse(textBox3.Text);//武力
                        data1[no, 4] = int.Parse(textBox4.Text);//智力
                        data1[no, 5] = int.Parse(textBox5.Text);//忠诚
                        data1[no, 13] = int.Parse(textBox7.Text);//年龄
                        data1[no, 6] = comboBox5.SelectedIndex;//性格
                        data1[no, 9] = comboBox4.SelectedIndex;//兵种
                        data1[no, 14] = comboBox6.SelectedIndex + 1;//城市
                        data1[no, 15] = comboBox7.SelectedIndex + 1;//搜索
                        data1[no, 1] = comboBox3.SelectedIndex + 1;//归属
                        break;
                    case 2:
                        data2[no, 0] = no;//序号
                        data2[no, 2] = int.Parse(textBox6.Text);//等级 
                        data2[no, 3] = int.Parse(textBox3.Text);//武力
                        data2[no, 4] = int.Parse(textBox4.Text);//智力
                        data2[no, 5] = int.Parse(textBox5.Text);//忠诚
                        data2[no, 13] = int.Parse(textBox7.Text);//年龄
                        data2[no, 6] = comboBox5.SelectedIndex;//性格
                        data2[no, 9] = comboBox4.SelectedIndex;//兵种
                        data2[no, 14] = comboBox6.SelectedIndex + 1;//城市
                        data2[no, 15] = comboBox7.SelectedIndex + 1;//搜索
                        data2[no, 1] = comboBox3.SelectedIndex + 1;//归属
                        break;
                    case 3:
                        data3[no, 0] = no;//序号
                        data3[no, 2] = int.Parse(textBox6.Text);//等级 
                        data3[no, 3] = int.Parse(textBox3.Text);//武力
                        data3[no, 4] = int.Parse(textBox4.Text);//智力
                        data3[no, 5] = int.Parse(textBox5.Text);//忠诚
                        data3[no, 13] = int.Parse(textBox7.Text);//年龄
                        data3[no, 6] = comboBox5.SelectedIndex;//性格
                        data3[no, 9] = comboBox4.SelectedIndex;//兵种
                        data3[no, 14] = comboBox6.SelectedIndex + 1;//城市
                        data3[no, 15] = comboBox7.SelectedIndex + 1;//搜索
                        data3[no, 1] = comboBox3.SelectedIndex + 1;//归属
                        break;
                    case 4:
                        data4[no, 0] = no;//序号
                        data4[no, 2] = int.Parse(textBox6.Text);//等级 
                        data4[no, 3] = int.Parse(textBox3.Text);//武力
                        data4[no, 4] = int.Parse(textBox4.Text);//智力
                        data4[no, 5] = int.Parse(textBox5.Text);//忠诚
                        data4[no, 13] = int.Parse(textBox7.Text);//年龄
                        data4[no, 6] = comboBox5.SelectedIndex;//性格
                        data4[no, 9] = comboBox4.SelectedIndex;//兵种
                        data4[no, 14] = comboBox6.SelectedIndex + 1;//城市
                        data4[no, 15] = comboBox7.SelectedIndex + 1;//搜索
                        data4[no, 1] = comboBox3.SelectedIndex + 1;//归属
                        break;
                }
            }
            catch (Exception) { }

        }

        private void GetHeroData(int id)
        {
            try
            {
                switch (dataFlag)
                {
                    case 1:
                        textBox2.Text = heroName1[id];//姓名
                        textBox6.Text = data1[id, 2].ToString();//等级 
                        textBox3.Text = data1[id, 3].ToString();//武力
                        textBox4.Text = data1[id, 4].ToString();//智力
                        textBox5.Text = data1[id, 5].ToString();//忠诚
                        textBox7.Text = data1[id, 13].ToString();//年龄
                        comboBox5.SelectedIndex = data1[id, 6];//性格
                        comboBox4.SelectedIndex = data1[id, 9];//兵种
                        comboBox6.SelectedIndex = data1[id, 14] - 1;//城市
                        comboBox7.SelectedIndex = data1[id, 15] - 1;//搜索
                        comboBox3.SelectedIndex = data1[id, 1] - 1;//归属
                        break;
                    case 2:
                        textBox2.Text = heroName2[id];//姓名
                        textBox6.Text = data2[id, 2].ToString();//等级
                        textBox3.Text = data2[id, 3].ToString();//武力
                        textBox4.Text = data2[id, 4].ToString();//智力 
                        textBox5.Text = data2[id, 5].ToString();//忠诚
                        textBox7.Text = data2[id, 13].ToString();//年龄
                        comboBox5.SelectedIndex = data2[id, 6];//性格
                        comboBox4.SelectedIndex = data2[id, 9];//兵种
                        comboBox6.SelectedIndex = data2[id, 14] - 1;//城市
                        comboBox7.SelectedIndex = data2[id, 15] - 1;//搜索
                        comboBox3.SelectedIndex = data2[id, 1] - 1;//归属
                        break;
                    case 3:
                        textBox2.Text = heroName3[id];//姓名
                        textBox6.Text = data3[id, 2].ToString();//等级
                        textBox3.Text = data3[id, 3].ToString();//武力
                        textBox4.Text = data3[id, 4].ToString();//智力 
                        textBox5.Text = data3[id, 5].ToString();//忠诚
                        textBox7.Text = data3[id, 13].ToString();//年龄
                        comboBox5.SelectedIndex = data3[id, 6];//性格
                        comboBox4.SelectedIndex = data3[id, 9];//兵种
                        comboBox6.SelectedIndex = data3[id, 14] - 1;//城市
                        comboBox7.SelectedIndex = data3[id, 15] - 1;//搜索
                        comboBox3.SelectedIndex = data3[id, 1] - 1;//归属
                        break;
                    case 4:
                        textBox2.Text = heroName4[id];//姓名
                        textBox6.Text = data4[id, 2].ToString();//等级
                        textBox3.Text = data4[id, 3].ToString();//武力
                        textBox4.Text = data4[id, 4].ToString();//智力
                        textBox5.Text = data4[id, 5].ToString();//忠诚
                        textBox7.Text = data4[id, 13].ToString();//年龄
                        comboBox5.SelectedIndex = data4[id, 6];//性格
                        comboBox4.SelectedIndex = data4[id, 9];//兵种
                        comboBox6.SelectedIndex = data4[id, 14] - 1;//城市
                        comboBox7.SelectedIndex = data4[id, 15] - 1;//搜索
                        comboBox3.SelectedIndex = data4[id, 1] - 1;//归属
                        break;
                }
            }
            catch (Exception) { };

        }

        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 GetCityNameFromFile()
        {
            FileStream fs = new FileStream(cityPath, 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)
            {
                cityName[i] = m_streamReader.ReadLine().Trim();
                comboBox6.Items.Add(cityName[i]);
                i++;
            }
            m_streamReader.Close();
            m_streamReader.Dispose();
            fs.Close();
            fs.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveChange();
            String tempPath="";
            switch (dataFlag)
            {
                case 1:
                    tempPath = path1;
                    break;
                case 2:
                    tempPath = path2;
                    break;
                case 3:
                    tempPath = path3;
                    break;
                case 4:
                    tempPath = path4;
                    break;
            }
            FileStream fs = new FileStream(tempPath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312"));
            for (int i = 0; i < HERO_MAX; i++)
            {
                if (data1[i, 0] == 0 && data1[i, 1] == 0) break;
                for (int j = 0; j < HERODATALENTH_MAX; j++)
                {
                    switch (dataFlag)
                    {
                        case 1:
                            sw.Write(data1[i, j] + " ");
                            break;
                        case 2:
                            sw.Write(data2[i, j] + " ");
                            break;
                        case 3:
                            sw.Write(data3[i, j] + " ");
                            break;
                        case 4:
                            sw.Write(data4[i, j] + " ");
                            break;
                    }

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

    }
}
