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 FightHeroPicEditer : Form
    {
        //right 190 *215 2*7 380*1505
        //left 215*205 7*7 1505*1435
        private const int LEFT_COLMAX = 7;
        private const int LEFT_ROW = 7;
        private const int RIGHT_COLMAX = 2;
        private const int RIGHT_ROW = 7;
        private const int LEFT_WIDTH = 380;
        private const int LEFT_HEIGHT = 1505;
        private const int RIGHT_WIDTH = 1505;
        private const int RIGHT_HEIGHT = 1435;
        private const int LEFT_ONE_WIDTH = 215;
        private const int LEFT_ONE_HEIGHT = 205;
        private const int RIGHT_ONE_WIDTH = 190;
        private const int RIGHT_ONE_HEIGHT = 215;

        private int type;//左右
        private int style;//兵种
        private int num;//选择值

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

        private String tempPath1;
        private String tempPath2;

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

        Bitmap[,] data1 = new Bitmap[LEFT_ROW, LEFT_COLMAX];
        Bitmap[,] data2 = new Bitmap[RIGHT_ROW, RIGHT_COLMAX];


        Bitmap[,] backdata1 = new Bitmap[LEFT_ROW, LEFT_COLMAX];
        Bitmap[,] backdata2 = new Bitmap[RIGHT_ROW, RIGHT_COLMAX];




        public FightHeroPicEditer()
        {
            InitializeComponent();
        }

        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 != LEFT_ONE_WIDTH || pictureBox1.Image.Height != LEFT_ONE_HEIGHT )&&type==0)
                {
                    MessageBox.Show("错误的图片大小！", "错误");
                    return;
                }
                if ((pictureBox1.Image.Width != RIGHT_ONE_WIDTH || pictureBox1.Image.Height != RIGHT_ONE_HEIGHT) && type == 1)
                {
                    MessageBox.Show("错误的图片大小！", "错误");
                    return;
                }

                switch (type)
                {
                    case 0:
                        data1[style, num] = new Bitmap(pictureBox1.Image);
                        break;
                    case 1:
                        data2[style, num] = new Bitmap(pictureBox1.Image);
                        break;
                    
                }

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

        private void SaveBmpToFile(int row, int col)
        {
            switch (type)
            {
                case 0:
                    for (int i = 0; i <LEFT_ONE_WIDTH ; i++)
                    {
                        for (int j = 0; j <LEFT_ONE_HEIGHT; j++)
                        {
                            bitmap1.SetPixel(col * LEFT_ONE_WIDTH + i, row * LEFT_ONE_HEIGHT + j, data1[row, col].GetPixel(i, j));
                        }
                    }

                    bitmap1.Save(path1);
                    break;
                case 1:
                    for (int i = 0; i < RIGHT_ONE_WIDTH; i++)
                    {
                        for (int j = 0; j < RIGHT_ONE_HEIGHT; j++)
                        {
                            bitmap2.SetPixel(col * RIGHT_ONE_WIDTH + i, row * RIGHT_ONE_HEIGHT + j, data2[row, col].GetPixel(i, j));
                        }
                    }
                    bitmap2.Save(path2);
                    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\\Fight\\fight_left.sbp";
                path2 = filePath + "\\Res\\Fight\\fight_right.sbp";


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

        private void GetBmpFromFile()
        {
            tempPath1 = path1 + "1";
            tempPath2 = path2 + "1";



            Bitmap bmp1 = new Bitmap(path1);
            bmp1.Save(tempPath1);
            bmp1.Dispose();
            Bitmap bmp2 = new Bitmap(path2);
            bmp2.Save(tempPath2);
            bmp2.Dispose();


            bitmap1 = new Bitmap(tempPath1);
            bitmap2 = new Bitmap(tempPath2);

            for (int i = 0; i < LEFT_ROW; i++)
            {
                for (int j = 0; j < LEFT_COLMAX; j++)
                {
                    data1[i, j] = new Bitmap(LEFT_ONE_WIDTH, LEFT_ONE_HEIGHT);

                    for (int offsetX = 0; offsetX < LEFT_ONE_WIDTH; offsetX++)
                    {
                        for (int offsetY = 0; offsetY < LEFT_ONE_HEIGHT; offsetY++)
                        {
                            if (((j * LEFT_ONE_WIDTH + offsetX) < bitmap1.Width) && ((i * LEFT_ONE_HEIGHT + offsetY) < bitmap1.Height))
                            {
                                data1[i, j].SetPixel(offsetX, offsetY, bitmap1.GetPixel((int)(j * LEFT_ONE_WIDTH + offsetX), (int)(i * LEFT_ONE_HEIGHT + offsetY)));
                                
                            }
                        }
                    }
                    toolStripProgressBar1.Value += 1;
                }
            }
            for (int i = 0; i < RIGHT_ROW; i++)
            {
                for (int j = 0; j < RIGHT_COLMAX; j++)
                {
                    data2[i, j] = new Bitmap(RIGHT_ONE_WIDTH, RIGHT_ONE_HEIGHT);

                    for (int offsetX = 0; offsetX < RIGHT_ONE_WIDTH; offsetX++)
                    {
                        for (int offsetY = 0; offsetY < RIGHT_ONE_HEIGHT; offsetY++)
                        {
                            if (((j * RIGHT_ONE_WIDTH + offsetX) < bitmap2.Width) && ((i * RIGHT_ONE_HEIGHT + offsetY) < bitmap2.Height))
                            {
                                data2[i, j].SetPixel(offsetX, offsetY, bitmap2.GetPixel((int)(j * RIGHT_ONE_WIDTH + offsetX), (int)(i * RIGHT_ONE_HEIGHT + offsetY)));

                            }
                        }
                    }
                    toolStripProgressBar1.Value += 1;
                }
            }
            toolStripProgressBar1.Value = 0;

        }


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

        private void BackUp()
        {
            for (int i = 0; i < LEFT_ROW; i++)
            {
                for (int j = 0; j < LEFT_COLMAX; j++)
                {
                    backdata1[i, j] = data1[i, j];

                }
            }
            for (int i = 0; i < RIGHT_ROW; i++)
            {
                for (int j = 0; j < RIGHT_COLMAX; j++)
                {
                    backdata2[i, j] = data2[i, j];

                }
            }
        }



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

        private void ReStore()
        {
            for (int i = 0; i < LEFT_ROW; i++)
            {
                for (int j = 0; j < LEFT_COLMAX; j++)
                {
                    data1[i, j] = backdata1[i, j];


                }
            }
            for (int i = 0; i < RIGHT_ROW; i++)
            {
                for (int j = 0; j < RIGHT_COLMAX; j++)
                {
                    data2[i, j] = backdata2[i, j];

                }
            }
        }

        

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {

            type = comboBox3.SelectedIndex;
            comboBox1.SelectedIndex = 0;
            comboBox1_SelectedIndexChanged(sender,e);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            style = comboBox1.SelectedIndex;
            comboBox2.Items.Clear();
            if (type == 0)
            {

                switch (style)
                {
                    case 0:
                        for (int i = 0; i < 7; i++) comboBox2.Items.Add(i);
                        break;
                    case 1:
                        for (int i = 0; i < 4; i++) comboBox2.Items.Add(i);
                        break;
                    case 2:
                        for (int i = 0; i < 4; i++) comboBox2.Items.Add(i);
                        break;
                    case 3:
                        for (int i = 0; i < 6; i++) comboBox2.Items.Add(i);
                        break;
                    case 4:
                        for (int i = 0; i < 7; i++) comboBox2.Items.Add(i);
                        break;
                    case 5:
                        for (int i = 0; i < 6; i++) comboBox2.Items.Add(i);
                        break;
                    case 6:
                        for (int i = 0; i < 1; i++) comboBox2.Items.Add(i);
                        break;
                    
                }
            }
            else
            {

                switch (style)
                {
                    case 0:
                        for (int i = 0; i < 2; i++) comboBox2.Items.Add(i);
                        break;
                    case 1:
                        for (int i = 0; i < 2; i++) comboBox2.Items.Add(i);
                        break;
                    case 2:
                        for (int i = 0; i < 2; i++) comboBox2.Items.Add(i);
                        break;
                    case 3:
                        for (int i = 0; i < 2; i++) comboBox2.Items.Add(i);
                        break;
                    case 4:
                        for (int i = 0; i < 2; i++) comboBox2.Items.Add(i);
                        break;
                    case 5:
                        for (int i = 0; i < 2; i++) comboBox2.Items.Add(i);
                        break;
                    case 6:
                        for (int i = 0; i < 1; i++) comboBox2.Items.Add(i);
                        break;

                }
            }
            comboBox2.SelectedIndex = 0;
            comboBox2_SelectedIndexChanged_1(sender, e);

        }

        private void comboBox2_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            num = comboBox2.SelectedIndex;
            if (type == 0)
            {
                pictureBox2.Image = data1[style, num];
            }
            else
            {
                pictureBox2.Image = data2[style, num];
            }
        }

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


    }
}
