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 SaveEditer : Form
    {
        private const int HERO_MAX = 300;
        private const int CITYDATALENTH_MAX = 19;
        private const int CITY_MAX = 50;
        private const int HERODATALENTH_MAX = 18;
        private const int GOODDATALENTH_MAX = 10;
        private const int GOOD_MAX = 50;

        private int noOfHero = 0;//序号
        private int noOfCity = 0;
        private int noOfGood = 0;

        private String filePath;
        private String path1;
        private String path2;
        private String path3;

        private String namePath1;//武将名称
        private String namePath2;
        private String namePath3;
        private String namePath4;

        private String goodPath;//物品名称
        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];
        String[] goodName = new String[GOOD_MAX];
        int dataFlag = 0;//区分存档
        int timeFlag = 0;//区分时期
        int[] lockedData1= new int[3];//起始数据
        int[,] lockedData2 = new int[255,10];//tasklist
        int[] lockedData3 = new int[37];//结尾数据

        int[] lockedData11 = new int[3];//起始数据
        int[,] lockedData12 = new int[255, 10];//tasklist
        int[] lockedData13 = new int[37];//结尾数据

        int[] lockedData21 = new int[3];//起始数据
        int[,] lockedData22 = new int[255, 10];//tasklist
        int[] lockedData23 = new int[37];//结尾数据


        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[,] data11 = new int[CITY_MAX, CITYDATALENTH_MAX];
        int[,] data12 = new int[CITY_MAX, CITYDATALENTH_MAX];
        int[,] data13 = new int[CITY_MAX, CITYDATALENTH_MAX];


        int[,] data21 = new int[GOOD_MAX, GOODDATALENTH_MAX];
        int[,] data22 = new int[GOOD_MAX, GOODDATALENTH_MAX];
        int[,] data23 = new int[GOOD_MAX, GOODDATALENTH_MAX];


        int[,] backdata1 = new int[CITY_MAX, CITYDATALENTH_MAX];
        int[,] backdata2 = new int[CITY_MAX, CITYDATALENTH_MAX];
        int[,] backdata3 = new int[CITY_MAX, CITYDATALENTH_MAX];


        int[,] backdata11 = new int[CITY_MAX, CITYDATALENTH_MAX];
        int[,] backdata12 = new int[CITY_MAX, CITYDATALENTH_MAX];
        int[,] backdata13 = new int[CITY_MAX, CITYDATALENTH_MAX];


        int[,] backdata21 = new int[GOOD_MAX, GOODDATALENTH_MAX];
        int[,] backdata22 = new int[GOOD_MAX, GOODDATALENTH_MAX];
        int[,] backdata23 = new int[GOOD_MAX, GOODDATALENTH_MAX];


        public SaveEditer()
        {
            InitializeComponent();
            numericUpDown1.TextChanged += new EventHandler(numericUpDown1_ValueChanged);
            numericUpDown2.TextChanged += new EventHandler(numericUpDown2_ValueChanged);
            numericUpDown3.TextChanged += new EventHandler(numericUpDown3_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\\Save\\save0.ssv";
                path2 = filePath + "\\Res\\Save\\save1.ssv";
                path3 = filePath + "\\Res\\Save\\save2.ssv";

                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";
                goodPath = filePath + "\\Res\\goods.sss";

               // try
                {
                    ReadFile();
                    GetHeroNameFromFile();
                    GetCityNameFromFile();
                    GetGoodNameFromFile();
                    comboBox2.SelectedIndex = 0;
                    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 ReadFile()
        {

                ReadOne();
                ReadTwo();
                ReadThree();
           
        }

        
        private void ReadThree()
        {
            FileStream fs = new FileStream(path3, FileMode.Open);
            StreamReader m_streamReader = new StreamReader(fs);
            m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);

            string[] line1 = m_streamReader.ReadLine().Split(' ');
            lockedData21[0] = int.Parse(line1[0]);
            if (lockedData21[0] == 0) return;
            lockedData21[1] = int.Parse(line1[1]);
            lockedData21[2] = int.Parse(line1[2]);

            for (int i = 0; i < HERO_MAX; i++)
            {
                string[] line = m_streamReader.ReadLine().Split(' ');
                for (int j = 0; j < HERODATALENTH_MAX; j++)
                {
                    data3[i, j] = int.Parse(line[j]);
                }
            }
            m_streamReader.ReadLine();

            for (int i = 0; i < CITY_MAX; i++)
            {
                string[] line = m_streamReader.ReadLine().Split(' ');
                for (int j = 0; j < CITYDATALENTH_MAX; j++)
                {
                    data13[i, j] = int.Parse(line[j]);
                }
            }
            m_streamReader.ReadLine();


            for (int i = 0; i < GOOD_MAX; i++)
            {
                string[] line = m_streamReader.ReadLine().Split(' ');
                for (int j = 0; j < GOODDATALENTH_MAX; j++)
                {
                    data23[i, j] = int.Parse(line[j]);
                }
            }
            m_streamReader.ReadLine();

            for (int i = 0; i < 255; i++)
            {
                string[] line = m_streamReader.ReadLine().Split(' ');
                for (int j = 0; j < 10; j++)
                {
                    lockedData22[i, j] = int.Parse(line[j]);
                }
            }

            m_streamReader.ReadLine();

            line1 = m_streamReader.ReadLine().Split(' ');//12
            for (int i = 0; i < 12; i++)
            {
                lockedData23[i] = int.Parse(line1[i]);
            }

            line1 = m_streamReader.ReadLine().Split(' ');//4
            for (int i = 0; i < 4; i++)
            {
                lockedData23[i + 12] = int.Parse(line1[i]);
            }

            line1 = m_streamReader.ReadLine().Split(' ');//9
            for (int i = 0; i < 9; i++)
            {
                lockedData23[i + 16] = int.Parse(line1[i]);
            }

            line1 = m_streamReader.ReadLine().Split(' ');//9
            for (int i = 0; i < 9; i++)
            {
                lockedData23[i + 25] = int.Parse(line1[i]);
            }
            String line2 = m_streamReader.ReadLine();//1
            lockedData23[34] = int.Parse(line2);
            line2 = m_streamReader.ReadLine();//1
            lockedData23[35] = int.Parse(line2);
            line2 = m_streamReader.ReadLine();//1
            lockedData23[36] = int.Parse(line2);

            m_streamReader.Close();
            m_streamReader.Dispose();
            fs.Close();
            fs.Dispose();
        }

        private void ReadTwo()
        {
            FileStream fs = new FileStream(path2, FileMode.Open);
            StreamReader m_streamReader = new StreamReader(fs);
            m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);

            string[] line1 = m_streamReader.ReadLine().Split(' ');
            lockedData11[0] = int.Parse(line1[0]);
            if (lockedData11[0] == 0) return;
            lockedData11[1] = int.Parse(line1[1]);
            lockedData11[2] = int.Parse(line1[2]);

            for (int i = 0; i < HERO_MAX; i++)
            {
                string[] line = m_streamReader.ReadLine().Split(' ');
                for (int j = 0; j < HERODATALENTH_MAX; j++)
                {
                    data2[i, j] = int.Parse(line[j]);
                }
            }
            m_streamReader.ReadLine();

            for (int i = 0; i < CITY_MAX; i++)
            {
                string[] line = m_streamReader.ReadLine().Split(' ');
                for (int j = 0; j < CITYDATALENTH_MAX; j++)
                {
                    data12[i, j] = int.Parse(line[j]);
                }
            }
            m_streamReader.ReadLine();


            for (int i = 0; i < GOOD_MAX; i++)
            {
                string[] line = m_streamReader.ReadLine().Split(' ');
                for (int j = 0; j < GOODDATALENTH_MAX; j++)
                {
                    data22[i, j] = int.Parse(line[j]);
                }
            }
            m_streamReader.ReadLine();

            for (int i = 0; i < 255; i++)
            {
                string[] line = m_streamReader.ReadLine().Split(' ');
                for (int j = 0; j < 10; j++)
                {
                    lockedData12[i, j] = int.Parse(line[j]);
                }
            }

            m_streamReader.ReadLine();

            line1 = m_streamReader.ReadLine().Split(' ');//12
            for (int i = 0; i < 12; i++)
            {
                lockedData13[i] = int.Parse(line1[i]);
            }

            line1 = m_streamReader.ReadLine().Split(' ');//4
            for (int i = 0; i < 4; i++)
            {
                lockedData13[i + 12] = int.Parse(line1[i]);
            }

            line1 = m_streamReader.ReadLine().Split(' ');//9
            for (int i = 0; i < 9; i++)
            {
                lockedData13[i + 16] = int.Parse(line1[i]);
            }

            line1 = m_streamReader.ReadLine().Split(' ');//9
            for (int i = 0; i < 9; i++)
            {
                lockedData13[i + 25] = int.Parse(line1[i]);
            }
            String line2 = m_streamReader.ReadLine();//1
            lockedData13[34] = int.Parse(line2);
            line2 = m_streamReader.ReadLine();//1
            lockedData13[35] = int.Parse(line2);
            line2 = m_streamReader.ReadLine();//1
            lockedData13[36] = int.Parse(line2);

            m_streamReader.Close();
            m_streamReader.Dispose();
            fs.Close();
            fs.Dispose();
        }

        private void ReadOne()
        {
            FileStream fs = new FileStream(path1, FileMode.Open);
            StreamReader m_streamReader = new StreamReader(fs);
            m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);

            string[] line1 = m_streamReader.ReadLine().Split(' ');
            lockedData1[0] = int.Parse(line1[0]);
            if (lockedData1[0] == 0) return;
            lockedData1[1] = int.Parse(line1[1]);
            lockedData1[2] = int.Parse(line1[2]);
            timeFlag = lockedData1[2];

            for (int i = 0; i < HERO_MAX; i++)
            {
                string[] line = m_streamReader.ReadLine().Split(' ');
                for (int j = 0; j < HERODATALENTH_MAX; j++)
                {
                    data1[i, j] = int.Parse(line[j]);
                }
            }
            m_streamReader.ReadLine();

            for (int i = 0; i < CITY_MAX; i++)
            {
                string[] line = m_streamReader.ReadLine().Split(' ');
                for (int j = 0; j < CITYDATALENTH_MAX; j++)
                {
                    data11[i, j] = int.Parse(line[j]);
                }
            }
            m_streamReader.ReadLine();


            for (int i = 0; i < GOOD_MAX; i++)
            {
                string[] line = m_streamReader.ReadLine().Split(' ');
                for (int j = 0; j < GOODDATALENTH_MAX; j++)
                {
                    data21[i, j] = int.Parse(line[j]);
                }
            }
            m_streamReader.ReadLine();

            for (int i = 0; i < 255; i++)
            {
                string[] line = m_streamReader.ReadLine().Split(' ');
                for (int j = 0; j < 10; j++)
                {
                    lockedData2[i, j] = int.Parse(line[j]);
                }
            }

            m_streamReader.ReadLine();

            line1 = m_streamReader.ReadLine().Split(' ');//12
            for (int i = 0; i < 12; i++)
            {
                lockedData3[i] = int.Parse(line1[i]);
            }

            line1 = m_streamReader.ReadLine().Split(' ');//4
            for (int i = 0; i < 4; i++)
            {
                lockedData3[i + 12] = int.Parse(line1[i]);
            }

            line1 = m_streamReader.ReadLine().Split(' ');//9
            for (int i = 0; i < 9; i++)
            {
                lockedData3[i + 16] = int.Parse(line1[i]);
            }

            line1 = m_streamReader.ReadLine().Split(' ');//9
            for (int i = 0; i < 9; i++)
            {
                lockedData3[i + 25] = int.Parse(line1[i]);
            }
            String line2 = m_streamReader.ReadLine();//1
            lockedData3[34] = int.Parse(line2);
            line2 = m_streamReader.ReadLine();//1
            lockedData3[35] = int.Parse(line2);

            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 < 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];

                }
            }
            for (int i = 0; i < CITY_MAX; i++)
            {
                for (int j = 0; j < CITYDATALENTH_MAX; j++)
                {
                    backdata11[i, j] = data11[i, j];
                    backdata12[i, j] = data12[i, j];
                    backdata13[i, j] = data13[i, j];

                }
            }
            for (int i = 0; i < GOOD_MAX; i++)
            {
                for (int j = 0; j < GOODDATALENTH_MAX; j++)
                {
                    backdata21[i, j] = data21[i, j];
                    backdata22[i, j] = data22[i, j];
                    backdata23[i, j] = data23[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];

                }
            }
            for (int i = 0; i < CITY_MAX; i++)
            {
                for (int j = 0; j < CITYDATALENTH_MAX; j++)
                {
                    data11[i, j] = backdata11[i, j];
                    data12[i, j] = backdata12[i, j];
                    data13[i, j] = backdata13[i, j];

                }
            }
            for (int i = 0; i < GOOD_MAX; i++)
            {
                for (int j = 0; j < GOODDATALENTH_MAX; j++)
                {
                    data21[i, j] = backdata21[i, j];
                    data22[i, j] = backdata22[i, j];
                    data23[i, j] = backdata23[i, j];

                }
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            //comboBox1.SelectedIndex = 0;
            dataFlag = comboBox2.SelectedIndex + 1;
            //textBox2.Text = dataFlag.ToString();
            try
            {
                switch (timeFlag)
                {
                    case 0:

                        comboBox3.Items.Clear();
                        comboBox12.Items.Clear();
                        comboBox5.Items.Clear();
                        comboBox8.Items.Clear();
                        for (int i = 0; i < heroName1.Length; i++)
                        {

                            comboBox3.Items.Add(heroName1[i]);
                            comboBox12.Items.Add(heroName1[i]);
                            comboBox5.Items.Add(heroName1[i]);
                            comboBox8.Items.Add(heroName1[i]);
                        }
                        break;
                    case 1:

                        comboBox3.Items.Clear();
                        comboBox12.Items.Clear();
                        comboBox5.Items.Clear();
                        comboBox8.Items.Clear();
                        for (int i = 0; i < heroName2.Length; i++)
                        {

                            comboBox3.Items.Add(heroName2[i]);
                            comboBox12.Items.Add(heroName2[i]);
                            comboBox5.Items.Add(heroName2[i]);
                            comboBox8.Items.Add(heroName2[i]);
                        }
                        break;
                    case 2:

                        comboBox3.Items.Clear();
                        comboBox12.Items.Clear();
                        comboBox5.Items.Clear();
                        comboBox8.Items.Clear();
                        for (int i = 0; i < heroName3.Length; i++)
                        {

                            comboBox3.Items.Add(heroName3[i]);
                            comboBox12.Items.Add(heroName3[i]);
                            comboBox5.Items.Add(heroName3[i]);
                            comboBox8.Items.Add(heroName3[i]);
                        }
                        break;
                    case 3:

                        comboBox3.Items.Clear();
                        comboBox12.Items.Clear();
                        comboBox5.Items.Clear();
                        comboBox8.Items.Clear();
                        for (int i = 0; i < heroName4.Length; i++)
                        {

                            comboBox3.Items.Add(heroName4[i]);
                            comboBox12.Items.Add(heroName4[i]);
                            comboBox5.Items.Add(heroName4[i]);
                            comboBox8.Items.Add(heroName4[i]);
                        }
                        break;
                   
                }
            }
            catch (Exception) { }
            GetCityData(int.Parse(numericUpDown1.Value.ToString()) - 1);
            GetHeroData(int.Parse(numericUpDown3.Value.ToString()));
            GetGoodData(int.Parse(numericUpDown2.Value.ToString()) - 1);

        }

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

        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            SaveChangeCity();
            noOfCity = int.Parse(numericUpDown1.Value.ToString()) - 1;
            GetCityData(int.Parse(numericUpDown1.Value.ToString()) - 1);

        }

        private void SaveChangeCity()
        {
            try
            {
                switch (dataFlag)
                {
                    case 1:
                        data11[noOfCity, 0] = noOfCity;//序号
                        data11[noOfCity, 3] = int.Parse(textBox3.Text);//农业最大
                        data11[noOfCity, 4] = int.Parse(textBox4.Text);//农业
                        data11[noOfCity, 5] = int.Parse(textBox5.Text);//商业最大
                        data11[noOfCity, 6] = int.Parse(textBox13.Text);//商业
                        data11[noOfCity, 7] = int.Parse(textBox6.Text);//民忠
                        data11[noOfCity, 8] = int.Parse(textBox7.Text);//防灾
                        data11[noOfCity, 9] = int.Parse(textBox9.Text);//人口最大
                        data11[noOfCity, 10] = int.Parse(textBox10.Text);//人口
                        data11[noOfCity, 11] = int.Parse(textBox8.Text);//金钱
                        data11[noOfCity, 12] = int.Parse(textBox11.Text);//粮食
                        data11[noOfCity, 13] = int.Parse(textBox12.Text);//空闲兵力
                        data11[noOfCity, 1] = comboBox3.SelectedIndex + 1;//归属
                        break;
                    case 2:
                        data12[noOfCity, 0] = noOfCity;//序号
                        data12[noOfCity, 3] = int.Parse(textBox3.Text);//农业最大
                        data12[noOfCity, 4] = int.Parse(textBox4.Text);//农业
                        data12[noOfCity, 5] = int.Parse(textBox5.Text);//商业最大
                        data12[noOfCity, 6] = int.Parse(textBox13.Text);//商业
                        data12[noOfCity, 7] = int.Parse(textBox6.Text);//民忠
                        data12[noOfCity, 8] = int.Parse(textBox7.Text);//防灾
                        data12[noOfCity, 9] = int.Parse(textBox9.Text);//人口最大
                        data12[noOfCity, 10] = int.Parse(textBox10.Text);//人口
                        data12[noOfCity, 11] = int.Parse(textBox8.Text);//金钱
                        data12[noOfCity, 12] = int.Parse(textBox11.Text);//粮食
                        data12[noOfCity, 13] = int.Parse(textBox12.Text);//空闲兵力
                        data12[noOfCity, 1] = comboBox3.SelectedIndex + 1;//归属
                        break;
                    case 3:
                        data13[noOfCity, 0] = noOfCity;//序号
                        data13[noOfCity, 3] = int.Parse(textBox3.Text);//农业最大
                        data13[noOfCity, 4] = int.Parse(textBox4.Text);//农业
                        data13[noOfCity, 5] = int.Parse(textBox5.Text);//商业最大
                        data13[noOfCity, 6] = int.Parse(textBox13.Text);//商业
                        data13[noOfCity, 7] = int.Parse(textBox6.Text);//民忠
                        data13[noOfCity, 8] = int.Parse(textBox7.Text);//防灾
                        data13[noOfCity, 9] = int.Parse(textBox9.Text);//人口最大
                        data13[noOfCity, 10] = int.Parse(textBox10.Text);//人口
                        data13[noOfCity, 11] = int.Parse(textBox8.Text);//金钱
                        data13[noOfCity, 12] = int.Parse(textBox11.Text);//粮食
                        data13[noOfCity, 13] = int.Parse(textBox12.Text);//空闲兵力
                        data13[noOfCity, 1] = comboBox3.SelectedIndex + 1;//归属
                        break;
                    
                }
            }
            catch (Exception) { }

        }

        private void GetHeroData(int id)
        {
            try
            {
                switch (dataFlag)
                {
                    case 1:
                        textBox23.Text = heroName1[id];//姓名
                        textBox19.Text = data1[id, 2].ToString();//等级 
                        textBox22.Text = data1[id, 3].ToString();//武力
                        textBox21.Text = data1[id, 4].ToString();//智力
                        textBox20.Text = data1[id, 5].ToString();//忠诚
                        textBox18.Text = data1[id, 13].ToString();//年龄
                        comboBox10.SelectedIndex = data1[id, 6];//性格
                        comboBox11.SelectedIndex = data1[id, 9];//兵种
                        comboBox9.SelectedIndex = data1[id, 14] - 1;//城市
                        comboBox8.SelectedIndex = data1[id, 17] - 1;//搜索
                        comboBox12.SelectedIndex = data1[id, 1] - 1;//归属
                        break;
                    case 2:
                        textBox23.Text = heroName2[id];//姓名
                        textBox19.Text = data2[id, 2].ToString();//等级 
                        textBox22.Text = data2[id, 3].ToString();//武力
                        textBox21.Text = data2[id, 4].ToString();//智力
                        textBox20.Text = data2[id, 5].ToString();//忠诚
                        textBox18.Text = data2[id, 13].ToString();//年龄
                        comboBox10.SelectedIndex = data2[id, 6];//性格
                        comboBox11.SelectedIndex = data2[id, 9];//兵种
                        comboBox9.SelectedIndex = data2[id, 14] - 1;//城市
                        comboBox8.SelectedIndex = data2[id, 17] - 1;//搜索
                        comboBox12.SelectedIndex = data2[id, 1] - 1;//归属
                        break;
                    case 3:
                        textBox23.Text = heroName3[id];//姓名
                        textBox19.Text = data3[id, 2].ToString();//等级 
                        textBox22.Text = data3[id, 3].ToString();//武力
                        textBox21.Text = data3[id, 4].ToString();//智力
                        textBox20.Text = data3[id, 5].ToString();//忠诚
                        textBox18.Text = data3[id, 13].ToString();//年龄
                        comboBox10.SelectedIndex = data3[id, 6];//性格
                        comboBox11.SelectedIndex = data3[id, 9];//兵种
                        comboBox9.SelectedIndex = data3[id, 14] - 1;//城市
                        comboBox8.SelectedIndex = data3[id, 17] - 1;//搜索
                        comboBox12.SelectedIndex = data3[id, 1] - 1;//归属
                        break;
                    
                }
            }
            catch (Exception) { };

        }


        private void GetCityData(int id)
        {
            try
            {
                switch (dataFlag)
                {
                    case 1:
                        textBox2.Text = cityName[id];//名称
                        textBox3.Text = data11[id, 3].ToString();//农业最大
                        textBox4.Text = data11[id, 4].ToString();//农业
                        textBox5.Text = data11[id, 5].ToString();//商业最大
                        textBox13.Text = data11[id, 6].ToString();//商业
                        textBox6.Text = data11[id, 7].ToString();//民忠
                        textBox7.Text = data11[id, 8].ToString();// 防灾
                        textBox9.Text = data11[id, 9].ToString();//人口最大
                        textBox10.Text = data11[id, 10].ToString();//人口
                        textBox8.Text = data11[id, 11].ToString();//金钱
                        textBox11.Text = data11[id, 12].ToString();//粮食
                        textBox12.Text = data11[id, 13].ToString();//空闲兵力
                        comboBox3.SelectedIndex = data11[id, 1] - 1;//归属
                        break;
                    case 2:
                        textBox2.Text = cityName[id];//名称
                        textBox3.Text = data12[id, 3].ToString();//农业最大
                        textBox4.Text = data12[id, 4].ToString();//农业
                        textBox5.Text = data12[id, 5].ToString();//商业最大
                        textBox13.Text = data12[id, 6].ToString();//商业
                        textBox6.Text = data12[id, 7].ToString();//民忠
                        textBox7.Text = data12[id, 8].ToString();// 防灾
                        textBox9.Text = data12[id, 9].ToString();//人口最大
                        textBox10.Text = data12[id, 10].ToString();//人口
                        textBox8.Text = data12[id, 11].ToString();//金钱
                        textBox11.Text = data12[id, 12].ToString();//粮食
                        textBox12.Text = data12[id, 13].ToString();//空闲兵力
                        comboBox3.SelectedIndex = data12[id, 1] - 1;//归属
                        break;
                    case 3:
                        textBox2.Text = cityName[id];//名称
                        textBox3.Text = data13[id, 3].ToString();//农业最大
                        textBox4.Text = data13[id, 4].ToString();//农业
                        textBox5.Text = data13[id, 5].ToString();//商业最大
                        textBox13.Text = data13[id, 6].ToString();//商业
                        textBox6.Text = data13[id, 7].ToString();//民忠
                        textBox7.Text = data13[id, 8].ToString();// 防灾
                        textBox9.Text = data13[id, 9].ToString();//人口最大
                        textBox10.Text = data13[id, 10].ToString();//人口
                        textBox8.Text = data13[id, 11].ToString();//金钱
                        textBox11.Text = data13[id, 12].ToString();//粮食
                        textBox12.Text = data13[id, 13].ToString();//空闲兵力
                        comboBox3.SelectedIndex = data13[id, 1] - 1;//归属
                        break;
                    
                }
            }
            catch (Exception) { };

        }

        private void GetGoodData(int id)
        {

            try
            {
                textBox17.Text = goodName[id];//名称
                switch (dataFlag)
                {
                    case 1:
                        textBox16.Text = data21[id, 3].ToString();//武力提升
                        textBox15.Text = data21[id, 4].ToString();//智力提升
                        textBox14.Text = data21[id, 5].ToString();//速度提升
                        comboBox1.SelectedIndex = data21[id, 6];//目标兵种
                        comboBox7.SelectedIndex = data21[id, 2];//道具种类
                        comboBox4.SelectedIndex = data21[id, 7] - 1;//城市
                        comboBox5.SelectedIndex = data21[id, 8] - 1;//搜索者
                        comboBox6.SelectedIndex = data21[id, 9];//是否找到
                        if (data21[id, 2] == 0)
                        {
                            SetEnableStatue(0);
                        }
                        else
                        {
                            SetEnableStatue(1);
                        }
                        break;
                    case 2:
                        textBox16.Text = data22[id, 3].ToString();//武力提升
                        textBox15.Text = data22[id, 4].ToString();//智力提升
                        textBox14.Text = data22[id, 5].ToString();//速度提升
                        comboBox1.SelectedIndex = data22[id, 6];//目标兵种
                        comboBox7.SelectedIndex = data22[id, 2];//道具种类
                        comboBox4.SelectedIndex = data22[id, 7] - 1;//城市
                        comboBox5.SelectedIndex = data22[id, 8] - 1;//搜索者
                        comboBox6.SelectedIndex = data22[id, 9];//是否找到
                        if (data22[id, 2] == 0)
                        {
                            SetEnableStatue(0);
                        }
                        else
                        {
                            SetEnableStatue(1);
                        }
                        break;
                    case 3:
                        textBox16.Text = data23[id, 3].ToString();//武力提升
                        textBox15.Text = data23[id, 4].ToString();//智力提升
                        textBox14.Text = data23[id, 5].ToString();//速度提升
                        comboBox1.SelectedIndex = data23[id, 6];//目标兵种
                        comboBox7.SelectedIndex = data23[id, 2];//道具种类
                        comboBox4.SelectedIndex = data23[id, 7] - 1;//城市
                        comboBox5.SelectedIndex = data23[id, 8] - 1;//搜索者
                        comboBox6.SelectedIndex = data23[id, 9];//是否找到
                        if (data23[id, 2] == 0)
                        {
                            SetEnableStatue(0);
                        }
                        else
                        {
                            SetEnableStatue(1);
                        }
                        break;
                    
                }
            }
            catch (Exception) { };


        }

        private void SetEnableStatue(int p)
        {
            if (p == 0)
            {
                textBox16.Enabled = true;
                textBox15.Enabled = true;
                textBox14.Enabled = true;
                comboBox1.Enabled = false;
                comboBox1.SelectedIndex = -1;
            }
            else
            {
                textBox16.Enabled = false;
                textBox15.Enabled = false;
                textBox14.Enabled = false;
                textBox3.Text = "0";
                textBox4.Text = "0";
                textBox8.Text = "0";
                comboBox1.Enabled = true;
                comboBox1.SelectedIndex = 0;
            }
        }


        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;
            comboBox9.Items.Clear();
            comboBox4.Items.Clear();
            while (!m_streamReader.EndOfStream)
            {
                cityName[i] = m_streamReader.ReadLine().Trim();
                comboBox9.Items.Add(cityName[i]);
                comboBox4.Items.Add(cityName[i]);

                i++;
            }
            m_streamReader.Close();
            m_streamReader.Dispose();
            fs.Close();
            fs.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveChangeCity();
            SaveChangeHero();
            SaveChangeGood();
            String tempPath = "";
            switch (dataFlag)
            {
                case 1:
                    tempPath = path1;
                    break;
                case 2:
                    tempPath = path2;
                    break;
                case 3:
                    tempPath = path3;
                    break;

            }
            FileStream fs = new FileStream(tempPath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312"));
            switch (dataFlag)
            {
                case 1:
                    sw.Write(lockedData1[0] + " " + lockedData1[1] + " " + lockedData1[2]);
                    sw.Write("\r\n");
                    for (int i = 0; i < HERO_MAX; i++)
                    {
                        for (int j = 0; j < HERODATALENTH_MAX; j++)
                        {

                            sw.Write(data1[i, j] + " ");
                        }
                        sw.Write("\r\n");

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


                    for (int i = 0; i < CITY_MAX; i++)
                    {
                        for (int j = 0; j < CITYDATALENTH_MAX; j++)
                        {

                            sw.Write(data11[i, j] + " ");
                        }
                        sw.Write("\r\n");
                    }
                    sw.Write("\r\n");

                    for (int i = 0; i < GOOD_MAX; i++)
                    {
                        for (int j = 0; j < GOODDATALENTH_MAX; j++)
                        {

                            sw.Write(data21[i, j] + " ");


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

                    for (int i = 0; i < 255; i++)
                    {
                        for (int j = 0; j < 10; j++)
                        {
                            sw.Write(lockedData2[i, j]+" ");
                        }
                        sw.Write("\r\n");
                    }
                    sw.Write("\r\n");

                    for (int i = 0; i < 12; i++)
                    {
                       sw.Write(lockedData3[i]+" ");
                    }
                    sw.Write("\r\n");

                    for (int i = 12; i < 16; i++)
                    {
                       sw.Write(lockedData3[i]+" ");
                    }
                    sw.Write("\r\n");
                    for (int i = 16; i < 25; i++)
                    {
                       sw.Write(lockedData3[i]+" ");
                    }
                    sw.Write("\r\n");
                    for (int i = 25; i < 34; i++)
                    {
                       sw.Write(lockedData3[i]+" ");
                    }
                    sw.Write("\r\n");
                    sw.Write(lockedData3[34] + "\r\n");
                    sw.Write(lockedData3[35] + "\r\n");
                    sw.Write(lockedData3[36] + "\r\n");
                    break;
                case 2:
                    sw.Write(lockedData11[0] + " " + lockedData11[1] + " " + lockedData11[2]);

                    for (int i = 0; i < HERO_MAX; i++)
                    {
                        for (int j = 0; j < HERODATALENTH_MAX; j++)
                        {

                            sw.Write(data2[i, j] + " ");
                        }
                        sw.Write("\r\n");

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


                    for (int i = 0; i < CITY_MAX; i++)
                    {
                        for (int j = 0; j < CITYDATALENTH_MAX; j++)
                        {

                            sw.Write(data12[i, j] + " ");
                        }
                        sw.Write("\r\n");
                    }
                    sw.Write("\r\n");

                    for (int i = 0; i < GOOD_MAX; i++)
                    {
                        for (int j = 0; j < GOODDATALENTH_MAX; j++)
                        {

                            sw.Write(data22[i, j] + " ");


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

                    for (int i = 0; i < 255; i++)
                    {
                        for (int j = 0; j < 10; j++)
                        {
                            sw.Write(lockedData12[i, j] + " ");
                        }
                        sw.Write("\r\n");
                    }
                    sw.Write("\r\n");

                    for (int i = 0; i < 12; i++)
                    {
                        sw.Write(lockedData13[i] + " ");
                    }
                    sw.Write("\r\n");

                    for (int i = 12; i < 16; i++)
                    {
                        sw.Write(lockedData13[i] + " ");
                    }
                    sw.Write("\r\n");
                    for (int i = 16; i < 25; i++)
                    {
                        sw.Write(lockedData13[i] + " ");
                    }
                    sw.Write("\r\n");
                    for (int i = 25; i < 34; i++)
                    {
                        sw.Write(lockedData13[i] + " ");
                    }
                    sw.Write("\r\n");
                    sw.Write(lockedData13[34] + "\r\n");
                    sw.Write(lockedData13[35] + "\r\n");
                    sw.Write(lockedData13[36] + "\r\n");
                    break;
                case 3:
                    sw.Write(lockedData21[0] + " " + lockedData21[1] + " " + lockedData21[2]);

                    for (int i = 0; i < HERO_MAX; i++)
                    {
                        for (int j = 0; j < HERODATALENTH_MAX; j++)
                        {

                            sw.Write(data3[i, j] + " ");
                        }
                        sw.Write("\r\n");

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


                    for (int i = 0; i < CITY_MAX; i++)
                    {
                        for (int j = 0; j < CITYDATALENTH_MAX; j++)
                        {

                            sw.Write(data13[i, j] + " ");
                        }
                        sw.Write("\r\n");
                    }
                    sw.Write("\r\n");

                    for (int i = 0; i < GOOD_MAX; i++)
                    {
                        for (int j = 0; j < GOODDATALENTH_MAX; j++)
                        {

                            sw.Write(data23[i, j] + " ");


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

                    for (int i = 0; i < 255; i++)
                    {
                        for (int j = 0; j < 10; j++)
                        {
                            sw.Write(lockedData22[i, j] + " ");
                        }
                        sw.Write("\r\n");
                    }
                    sw.Write("\r\n");

                    for (int i = 0; i < 12; i++)
                    {
                        sw.Write(lockedData23[i] + " ");
                    }
                    sw.Write("\r\n");

                    for (int i = 12; i < 16; i++)
                    {
                        sw.Write(lockedData23[i] + " ");
                    }
                    sw.Write("\r\n");
                    for (int i = 16; i < 25; i++)
                    {
                        sw.Write(lockedData23[i] + " ");
                    }
                    sw.Write("\r\n");
                    for (int i = 25; i < 34; i++)
                    {
                        sw.Write(lockedData23[i] + " ");
                    }
                    sw.Write("\r\n");
                    sw.Write(lockedData23[34] + "\r\n");
                    sw.Write(lockedData23[35] + "\r\n");
                    sw.Write(lockedData23[36] + "\r\n");
                    break;
               
            }

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


            
        }

        private void SaveChangeGood()
        {
            try
            {
                switch (dataFlag)
                {
                    case 1:
                        data21[noOfGood, 0] = noOfGood;//序号
                        data21[noOfGood, 3] = int.Parse(textBox16.Text);//武力提升
                        data21[noOfGood, 4] = int.Parse(textBox15.Text);//智力提升
                        data21[noOfGood, 5] = int.Parse(textBox14.Text);//速度提升
                        data21[noOfGood, 6] = comboBox1.SelectedIndex;//目标兵种
                        data21[noOfGood, 2] = comboBox7.SelectedIndex;//道具种类
                        data21[noOfGood, 7] = comboBox4.SelectedIndex + 1;//城市
                        data21[noOfGood, 8] = comboBox5.SelectedIndex + 1;//搜索者
                        data21[noOfGood, 9] = comboBox6.SelectedIndex;//是否找到
                        break;
                    case 2:
                        data22[noOfGood, 0] = noOfGood;//序号
                        data22[noOfGood, 3] = int.Parse(textBox16.Text);//武力提升
                        data22[noOfGood, 4] = int.Parse(textBox15.Text);//智力提升
                        data22[noOfGood, 5] = int.Parse(textBox14.Text);//速度提升
                        data22[noOfGood, 6] = comboBox1.SelectedIndex;//目标兵种
                        data22[noOfGood, 2] = comboBox7.SelectedIndex;//道具种类
                        data22[noOfGood, 7] = comboBox4.SelectedIndex + 1;//城市
                        data22[noOfGood, 8] = comboBox5.SelectedIndex + 1;//搜索者
                        data22[noOfGood, 9] = comboBox6.SelectedIndex;//是否找到
                        break;
                    case 3:
                        data23[noOfGood, 0] = noOfGood;//序号
                        data23[noOfGood, 3] = int.Parse(textBox16.Text);//武力提升
                        data23[noOfGood, 4] = int.Parse(textBox15.Text);//智力提升
                        data23[noOfGood, 5] = int.Parse(textBox14.Text);//速度提升
                        data23[noOfGood, 6] = comboBox1.SelectedIndex;//目标兵种
                        data23[noOfGood, 2] = comboBox7.SelectedIndex;//道具种类
                        data23[noOfGood, 7] = comboBox4.SelectedIndex + 1;//城市
                        data23[noOfGood, 8] = comboBox5.SelectedIndex + 1;//搜索者
                        data23[noOfGood, 9] = comboBox6.SelectedIndex;//是否找到
                        break;
                  
                }
            }
            catch (Exception) { }
        }

        private void SaveChangeHero()
        {
      
            try
            {
                switch (dataFlag)
                {
                    case 1:
                        data1[noOfHero, 0] = noOfHero;//序号
                        data1[noOfHero, 2] = int.Parse(textBox19.Text);//等级 
                        data1[noOfHero, 3] = int.Parse(textBox22.Text);//武力
                        data1[noOfHero, 4] = int.Parse(textBox21.Text);//智力
                        data1[noOfHero, 5] = int.Parse(textBox20.Text);//忠诚
                        data1[noOfHero, 13] = int.Parse(textBox18.Text);//年龄
                        data1[noOfHero, 6] = comboBox10.SelectedIndex;//性格
                        data1[noOfHero, 9] = comboBox11.SelectedIndex;//兵种
                        data1[noOfHero, 14] = comboBox9.SelectedIndex + 1;//城市
                        data1[noOfHero, 17] = comboBox8.SelectedIndex + 1;//搜索
                        data1[noOfHero, 1] = comboBox12.SelectedIndex + 1;//归属
                        break;
                    case 2:
                        data2[noOfHero, 0] = noOfHero;//序号
                        data2[noOfHero, 2] = int.Parse(textBox19.Text);//等级 
                        data2[noOfHero, 3] = int.Parse(textBox22.Text);//武力
                        data2[noOfHero, 4] = int.Parse(textBox21.Text);//智力
                        data2[noOfHero, 5] = int.Parse(textBox20.Text);//忠诚
                        data2[noOfHero, 13] = int.Parse(textBox18.Text);//年龄
                        data2[noOfHero, 6] = comboBox10.SelectedIndex;//性格
                        data2[noOfHero, 9] = comboBox11.SelectedIndex;//兵种
                        data2[noOfHero, 14] = comboBox9.SelectedIndex + 1;//城市
                        data2[noOfHero, 17] = comboBox8.SelectedIndex + 1;//搜索
                        data2[noOfHero, 1] = comboBox12.SelectedIndex + 1;//归属
                        break;
                    case 3:
                        data3[noOfHero, 0] = noOfHero;//序号
                        data3[noOfHero, 2] = int.Parse(textBox19.Text);//等级 
                        data3[noOfHero, 3] = int.Parse(textBox22.Text);//武力
                        data3[noOfHero, 4] = int.Parse(textBox21.Text);//智力
                        data3[noOfHero, 5] = int.Parse(textBox20.Text);//忠诚
                        data3[noOfHero, 13] = int.Parse(textBox18.Text);//年龄
                        data3[noOfHero, 6] = comboBox10.SelectedIndex;//性格
                        data3[noOfHero, 9] = comboBox11.SelectedIndex;//兵种
                        data3[noOfHero, 14] = comboBox9.SelectedIndex + 1;//城市
                        data3[noOfHero, 17] = comboBox8.SelectedIndex + 1;//搜索
                        data3[noOfHero, 1] = comboBox12.SelectedIndex + 1;//归属
                        break;
                    
                }
            }
            catch (Exception) { }

        }

        private void numericUpDown3_ValueChanged(object sender, EventArgs e)
        {
            SaveChangeHero();
            noOfHero = int.Parse(numericUpDown3.Value.ToString());
            GetHeroData(int.Parse(numericUpDown3.Value.ToString()));
        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            SaveChangeGood();
            noOfGood = int.Parse(numericUpDown2.Value.ToString()) - 1;
            GetGoodData(int.Parse(numericUpDown2.Value.ToString()) - 1);
        }

        private void comboBox7_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox7.SelectedIndex == 0)
            {
                SetEnableStatue(0);
            }
            else
            {
                SetEnableStatue(1);
            }
        }


    }
}
