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 GoodDataEditer : Form
    {
        private const int HERO_MAX = 300;
        private const int GOODDATALENTH_MAX = 10;
        private const int CITY_MAX = 50;
        private const int GOOD_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;
        private String goodPath;

        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[,] data1 = new int[GOOD_MAX, GOODDATALENTH_MAX];
        int[,] data2 = new int[GOOD_MAX, GOODDATALENTH_MAX];
        int[,] data3 = new int[GOOD_MAX, GOODDATALENTH_MAX];
        int[,] data4 = new int[GOOD_MAX, GOODDATALENTH_MAX];

        int[,] backdata1 = new int[GOOD_MAX, GOODDATALENTH_MAX];
        int[,] backdata2 = new int[GOOD_MAX, GOODDATALENTH_MAX];
        int[,] backdata3 = new int[GOOD_MAX, GOODDATALENTH_MAX];
        int[,] backdata4 = new int[GOOD_MAX, GOODDATALENTH_MAX];
        public GoodDataEditer()
        {
            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\\Good\\goods1.sdt";
                path2 = filePath + "\\Res\\Good\\goods2.sdt";
                path3 = filePath + "\\Res\\Good\\goods3.sdt";
                path4 = filePath + "\\Res\\Good\\goods4.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";
                goodPath = filePath + "\\Res\\goods.sss";

                //try
                {
                    ReadFile();
                    GetGoodNameFromFile();
                    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)
            {
                try
                {
                    string[] line = m_streamReader.ReadLine().Split(' ');
                    for (int j = 0; j < GOODDATALENTH_MAX; j++)
                    {
                        data1[i, j] = int.Parse(line[j]);
                    }
                    i++;
                }
                catch { };
            }
            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)
            {
                try
                {
                    string[] line = m_streamReader2.ReadLine().Split(' ');
                    for (int j = 0; j < GOODDATALENTH_MAX; j++)
                    {
                        data2[i, j] = int.Parse(line[j]);
                    }
                    i++;
                }
                catch { };
            }
            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)
            {
                try
                {
                    string[] line = m_streamReader3.ReadLine().Split(' ');
                    for (int j = 0; j < GOODDATALENTH_MAX; j++)
                    {
                        data3[i, j] = int.Parse(line[j]);
                    }
                    i++;
                }
                catch { };
            }
            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)
            {
                try
                {
                    string[] line = m_streamReader4.ReadLine().Split(' ');
                    for (int j = 0; j < GOODDATALENTH_MAX; j++)
                    {
                        data4[i, j] = int.Parse(line[j]);
                    }
                    i++;
                }
                catch { };
            }
            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 < GOOD_MAX; i++)
            {
                for (int j = 0; j < GOODDATALENTH_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 < GOOD_MAX; i++)
            {
                for (int j = 0; j < GOODDATALENTH_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();
            comboBox4.Items.Clear();
            for (int i = 0; i < cityName.Length; i++)
            {
                if (cityName[i] == null) break;
                comboBox4.Items.Add(cityName[i]);
            }
            try
            {
                switch (dataFlag)
                {
                    case 1:
                        comboBox5.Items.Clear();
                        for (int i = 0; i < heroName1.Length; i++)
                        {
                            comboBox5.Items.Add(heroName1[i]);
                        }
                        break;
                    case 2:
                        comboBox5.Items.Clear();
                        for (int i = 0; i < heroName2.Length; i++)
                        {
                            comboBox5.Items.Add(heroName2[i]);
                        }
                        break;
                    case 3:
                        comboBox5.Items.Clear();
                        for (int i = 0; i < heroName3.Length; i++)
                        {
                            comboBox5.Items.Add(heroName3[i]);
                        }
                        break;
                    case 4:
                        comboBox5.Items.Clear();
                        for (int i = 0; i < heroName4.Length; i++)
                        {
                            comboBox5.Items.Add(heroName4[i]);
                        }
                        break;
                }
            }
            catch (Exception) { }
            GetGoodData(int.Parse(numericUpDown1.Value.ToString())-1);

        }

        private void GoodDataEditer_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()) - 1;
            GetGoodData(int.Parse(numericUpDown1.Value.ToString()) - 1);
        }

        private void GetGoodData(int id)
        {

            try
            {
                textBox2.Text = goodName[id];//名称
                switch (dataFlag)
                {
                    case 1:
                        textBox3.Text = data1[id, 3].ToString();//武力提升
                        textBox4.Text = data1[id, 4].ToString();//智力提升
                        textBox8.Text = data1[id, 5].ToString();//速度提升
                        comboBox1.SelectedIndex = data1[id, 6]-1;//目标兵种
                        comboBox3.SelectedIndex = data1[id, 2];//道具种类
                        comboBox4.SelectedIndex = data1[id, 7]-1;//城市
                        comboBox5.SelectedIndex = data1[id, 8]-1;//搜索者

                        if (data1[id, 2] == 0)
                        {
                            SetEnableStatue(0);
                        }
                        else
                        {
                            SetEnableStatue(1);
                        }
                        break;
                    case 2:
                        textBox3.Text = data2[id, 3].ToString();//武力提升
                        textBox4.Text = data2[id, 4].ToString();//智力提升
                        textBox8.Text = data2[id, 5].ToString();//速度提升
                        comboBox1.SelectedIndex = data2[id, 6]-1;//目标兵种
                        comboBox3.SelectedIndex = data2[id, 2];//道具种类
                        comboBox4.SelectedIndex = data2[id, 7]-1;//城市
                        comboBox5.SelectedIndex = data2[id, 8]-1;//搜索者

                        if (data2[id, 2] == 0)
                        {
                            SetEnableStatue(0);
                        }
                        else
                        {
                            SetEnableStatue(1);
                        }
                        break;
                    case 3:
                        textBox3.Text = data3[id, 3].ToString();//武力提升
                        textBox4.Text = data3[id, 4].ToString();//智力提升
                        textBox8.Text = data3[id, 5].ToString();//速度提升
                        comboBox1.SelectedIndex = data3[id, 6]-1;//目标兵种
                        comboBox3.SelectedIndex = data3[id, 2];//道具种类
                        comboBox4.SelectedIndex = data3[id, 7]-1;//城市
                        comboBox5.SelectedIndex = data3[id, 8]-1;//搜索者

                        if (data3[id, 2] == 0)
                        {
                            SetEnableStatue(0);
                        }
                        else
                        {
                            SetEnableStatue(1);
                        }
                        break;
                    case 4:
                        textBox3.Text = data4[id, 3].ToString();//武力提升
                        textBox4.Text = data4[id, 4].ToString();//智力提升
                        textBox8.Text = data4[id, 5].ToString();//速度提升
                        comboBox1.SelectedIndex = data4[id, 6]-1;//目标兵种
                        comboBox3.SelectedIndex = data4[id, 2];//道具种类
                        comboBox4.SelectedIndex = data4[id, 7]-1;//城市
                        comboBox5.SelectedIndex = data4[id, 8]-1;//搜索者

                        if (data4[id, 2] == 0)
                        {
                            SetEnableStatue(0);
                        }
                        else
                        {
                            SetEnableStatue(1);
                        }
                        break;
                }
            }
            catch (Exception) { };
           

        }

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

        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();
                i++;
            }
            m_streamReader.Close();
            m_streamReader.Dispose();
            fs.Close();
            fs.Dispose();
        }

        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 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 < CITY_MAX; i++)
            {

                for (int j = 0; j < GOODDATALENTH_MAX; j++)
                {
                    switch (dataFlag)
                    {
                        case 1:
                           if (data1[i, 0] == 0) break;
                            sw.Write(data1[i, j] + " ");
                            break;
                        case 2:
                            if (data2[i, 0] == 0) break;
                            sw.Write(data2[i, j] + " ");
                            break;
                        case 3:
                            if (data3[i, 0] == 0) break;
                            sw.Write(data3[i, j] + " ");
                            break;
                        case 4:
                            if (data4[i, 0] == 0) break;
                            sw.Write(data4[i, j] + " ");
                            break;
                    }

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

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

        private void SaveChange()
        {
            try
            {
                switch (dataFlag)
                {
                    case 1:
                        data1[no, 0] = no+1;//序号
                        data1[no, 3] = int.Parse(textBox3.Text);//武力提升
                        data1[no, 4] = int.Parse(textBox4.Text);//智力提升
                        data1[no, 5] = int.Parse(textBox8.Text);//速度提升
                        data1[no, 6] = comboBox1.SelectedIndex +1 ;//目标兵种
                        data1[no, 2] = comboBox3.SelectedIndex;//道具种类
                        data1[no, 7] = comboBox4.SelectedIndex + 1;//城市
                        data1[no, 8] = comboBox5.SelectedIndex + 1;//搜索者

                        break;
                    case 2:
                        data2[no, 0] = no+1;//序号
                        data2[no, 3] = int.Parse(textBox3.Text);//武力提升
                        data2[no, 4] = int.Parse(textBox4.Text);//智力提升
                        data2[no, 5] = int.Parse(textBox8.Text);//速度提升
                        data2[no, 6] = comboBox1.SelectedIndex +1;//目标兵种
                        data2[no, 2] = comboBox3.SelectedIndex;//道具种类
                        data2[no, 7] = comboBox4.SelectedIndex + 1;//城市
                        data2[no, 8] = comboBox5.SelectedIndex + 1;//搜索者

                        break;
                    case 3:
                        data3[no, 0] = no+1;//序号
                        data3[no, 3] = int.Parse(textBox3.Text);//武力提升
                        data3[no, 4] = int.Parse(textBox4.Text);//智力提升
                        data3[no, 5] = int.Parse(textBox8.Text);//速度提升
                        data3[no, 6] = comboBox1.SelectedIndex +1;//目标兵种
                        data3[no, 2] = comboBox3.SelectedIndex;//道具种类
                        data3[no, 7] = comboBox4.SelectedIndex + 1;//城市
                        data3[no, 8] = comboBox5.SelectedIndex + 1;//搜索者

                        break;
                    case 4:
                        data4[no, 0] = no+1;//序号
                        data4[no, 3] = int.Parse(textBox3.Text);//武力提升
                        data4[no, 4] = int.Parse(textBox4.Text);//智力提升
                        data4[no, 5] = int.Parse(textBox8.Text);//速度提升
                        data4[no, 6] = comboBox1.SelectedIndex +1;//目标兵种
                        data4[no, 2] = comboBox3.SelectedIndex;//道具种类
                        data4[no, 7] = comboBox4.SelectedIndex + 1;//城市
                        data4[no, 8] = comboBox5.SelectedIndex + 1;//搜索者

                        break;
                }
            }
            catch (Exception) { }
        }

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

    }
}
