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

namespace Skyside3gbySourceEditer
{
    public partial class HeroHeadEditer : Form
    {
        private const int HERO_MAX = 300;
        private const int HEAD_WIDTH = 120;
        private const int HEAD_HEIGHT = 120;
        private const int HEAD_COL = 10;
        private const int HEAD_ROW = 30;
        private const int ALL_WIDTH = 1200;
        private const int ALL_HEIGHT = 3600;

        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 tempPath1;
        private String tempPath2;
        private String tempPath3;
        private String tempPath4;

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

        int dataFlag = 0;//区分时期

        Bitmap bitmap1 ;
        Bitmap bitmap2 ;
        Bitmap bitmap3 ;
        Bitmap bitmap4 ;

        Bitmap[,] data1 = new Bitmap[HEAD_ROW, HEAD_COL];
        Bitmap[,] data2 = new Bitmap[HEAD_ROW, HEAD_COL];
        Bitmap[,] data3 = new Bitmap[HEAD_ROW, HEAD_COL];
        Bitmap[,] data4 = new Bitmap[HEAD_ROW, HEAD_COL];

        Bitmap[,] backdata1 = new Bitmap[HEAD_ROW, HEAD_COL];
        Bitmap[,] backdata2 = new Bitmap[HEAD_ROW, HEAD_COL];
        Bitmap[,] backdata3 = new Bitmap[HEAD_ROW, HEAD_COL];
        Bitmap[,] backdata4 = new Bitmap[HEAD_ROW, HEAD_COL];

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

        private void button1_Click(object sender, EventArgs e)       
        {         
            OpenFileDialog ofd = new OpenFileDialog();        
            ofd.Filter = "BMP图片文件|*.bmp";       
                   if (ofd.ShowDialog()==DialogResult.OK)       
                   {          
                       this.pictureBox1.Image = Image.FromFile(ofd.FileName);
                   }      
        }
        private void button2_Click(object sender, EventArgs e)     
        {

            //try
            {
                if (pictureBox1.Image.Width != HEAD_WIDTH || pictureBox1.Image.Width != HEAD_HEIGHT)
                {
                    MessageBox.Show("错误的图片大小！", "错误");
                }

                int rownum = no / HEAD_COL;
                int colnum = no % HEAD_COL;
                switch (dataFlag)
                {
                    case 1:
                        data1[rownum, colnum] = new Bitmap(pictureBox1.Image);
                        break;
                    case 2:
                        data2[rownum, colnum] = new Bitmap(pictureBox1.Image);
                        break;
                    case 3:
                        data3[rownum, colnum] = new Bitmap(pictureBox1.Image);
                        break;
                    case 4:
                        data4[rownum, colnum] = new Bitmap(pictureBox1.Image);
                        break;
                }

                SaveBmpToFile(rownum, colnum);
            }
            //catch (Exception)
            {
                //MessageBox.Show("没有修改过头像，无法保存！", "错误");
            }
        }

        private void SaveBmpToFile(int row,int col)
        {
            switch (dataFlag)
            {
                case 1:
                    for (int i = 0; i < HEAD_HEIGHT; i++)
                    {
                        for (int j = 0; j < HEAD_WIDTH; j++)
                        {
                            bitmap1.SetPixel(col * HEAD_WIDTH + i, row * HEAD_HEIGHT + j, data1[row, col].GetPixel(i, j));
                        }
                    }
                    
                    bitmap1.Save(path1);
                    break;
                case 2:
                    for (int i = 0; i < HEAD_HEIGHT; i++)
                    {
                        for (int j = 0; j < HEAD_WIDTH; j++)
                        {
                            bitmap2.SetPixel(col * HEAD_WIDTH + i, row * HEAD_HEIGHT + j, data2[row, col].GetPixel(i, j));
                        }
                    }
                    bitmap2.Save(path2);
                    break;
                case 3:
                    for (int i = 0; i < HEAD_HEIGHT; i++)
                    {
                        for (int j = 0; j < HEAD_WIDTH; j++)
                        {
                            bitmap3.SetPixel(col * HEAD_WIDTH + i, row * HEAD_HEIGHT + j, data3[row, col].GetPixel(i, j));
                        }
                    }
                    bitmap3.Save(path3);
                    break;
                case 4:
                    for (int i = 0; i < HEAD_HEIGHT; i++)
                    {
                        for (int j = 0; j < HEAD_WIDTH; j++)
                        {
                            bitmap4.SetPixel(col * HEAD_WIDTH + i, row * HEAD_HEIGHT + j, data4[row, col].GetPixel(i, j));
                        }
                    }
                    bitmap4.Save(path4);
                    break;
            }

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if ((folderBrowserDialog1.ShowDialog() == DialogResult.OK) && folderBrowserDialog1.SelectedPath != "")
            {
                filePath = folderBrowserDialog1.SelectedPath;
                textBox1.Text = filePath;
                path1 = filePath + "\\Res\\Head\\1dznq.sbp";
                path2 = filePath + "\\Res\\Head\\2ccjq.sbp";
                path3 = filePath + "\\Res\\Head\\3cbzz.sbp";
                path4 = filePath + "\\Res\\Head\\4sgdl.sbp";
                namePath1 = filePath + "\\Res\\Hero\\heroname1.sss";
                namePath2 = filePath + "\\Res\\Hero\\heroname2.sss";
                namePath3 = filePath + "\\Res\\Hero\\heroname3.sss";
                namePath4 = filePath + "\\Res\\Hero\\heroname4.sss";

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

        private void GetBmpFromFile()
        {
            tempPath1 = path1 + "1";
            tempPath2 = path2 + "1";
            tempPath3 = path3 + "1";
            tempPath4 = path4 + "1";


            Bitmap bmp1 = new Bitmap(path1);
            bmp1.Save(tempPath1);
            bmp1.Dispose();
            Bitmap bmp2 = new Bitmap(path2);
            bmp2.Save(tempPath2);
            bmp2.Dispose();
            Bitmap bmp3 = new Bitmap(path3);
            bmp3.Save(tempPath3);
            bmp3.Dispose();
            Bitmap bmp4 = new Bitmap(path4);
            bmp4.Save(tempPath4);
            bmp4.Dispose();

            bitmap1 = new Bitmap(tempPath1);
            bitmap2 = new Bitmap(tempPath2);
            bitmap3 = new Bitmap(tempPath3);
            bitmap4 = new Bitmap(tempPath4);

           

            Decimal MaxRow = HEAD_ROW;
            Decimal MaxColumn = HEAD_COL;
            for (int i = 0; i < MaxRow; i++)
            {
                for (int j = 0; j < MaxColumn; j++)
                {
                    data1[i, j] = new Bitmap(HEAD_WIDTH, HEAD_HEIGHT);
                    data2[i, j] = new Bitmap(HEAD_WIDTH, HEAD_HEIGHT);
                    data3[i, j] = new Bitmap(HEAD_WIDTH, HEAD_HEIGHT);
                    data4[i, j] = new Bitmap(HEAD_WIDTH, HEAD_HEIGHT);
                    for (int offsetX = 0; offsetX < HEAD_WIDTH; offsetX++)
                    {
                        for (int offsetY = 0; offsetY < HEAD_HEIGHT; offsetY++)
                        {
                            if (((j * HEAD_WIDTH + offsetX) < bitmap1.Width) && ((i * HEAD_HEIGHT + offsetY) < bitmap1.Height))
                            {
                                data1[i, j].SetPixel(offsetX, offsetY, bitmap1.GetPixel((int)(j * HEAD_WIDTH + offsetX), (int)(i * HEAD_HEIGHT + offsetY)));
                                data2[i, j].SetPixel(offsetX, offsetY, bitmap2.GetPixel((int)(j * HEAD_WIDTH + offsetX), (int)(i * HEAD_HEIGHT + offsetY)));
                                data3[i, j].SetPixel(offsetX, offsetY, bitmap3.GetPixel((int)(j * HEAD_WIDTH + offsetX), (int)(i * HEAD_HEIGHT + offsetY)));
                                data4[i, j].SetPixel(offsetX, offsetY, bitmap4.GetPixel((int)(j * HEAD_WIDTH + offsetX), (int)(i * HEAD_HEIGHT + offsetY)));
                                
                            }
                        }
                    }
                    toolStripProgressBar1.Value += 1;
                }
            }
            toolStripProgressBar1.Value =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 comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            //comboBox1.SelectedIndex = 0;
            dataFlag = comboBox2.SelectedIndex + 1;
            //textBox2.Text = dataFlag.ToString();
            numericUpDown1_ValueChanged(sender, e);
        }


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

        private void BackUp()
        {
            for (int i = 0; i < HEAD_ROW; i++)
            {
                for (int j = 0; j < HEAD_COL; 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 button5_Click(object sender, EventArgs e)
        {
            ReStore();
        }

        private void ReStore()
        {
            for (int i = 0; i < HEAD_ROW; i++)
            {
                for (int j = 0; j < HEAD_COL; 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 numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            no = int.Parse(numericUpDown1.Value.ToString());
            int rownum = no / HEAD_COL;
            int colnum = no % HEAD_COL;
            switch (dataFlag)
            {
                case 1:
                    textBox2.Text = heroName1[no];
                    pictureBox2.Image = data1[rownum, colnum];
                    break;
                case 2:
                    textBox2.Text = heroName2[no];
                    pictureBox2.Image = data2[rownum, colnum];
                    break;
                case 3:
                    textBox2.Text = heroName3[no];
                    pictureBox2.Image = data3[rownum, colnum];
                    break;
                case 4:
                    textBox2.Text = heroName4[no];
                    pictureBox2.Image = data4[rownum, colnum];
                    break;
            }
            
            
        }

        protected override void OnClosing(CancelEventArgs e) 
        {
            try
            {
                bitmap1.Dispose();
                bitmap2.Dispose();
                bitmap3.Dispose();
                bitmap4.Dispose();
                File.Delete(tempPath1);
                File.Delete(tempPath2);
                File.Delete(tempPath3);
                File.Delete(tempPath4);
            }
            catch (Exception) { }
        }


    }
}
