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 FightSkillPicEditer : Form
    {
        //328*320  2*8 656*2560
        private const int SKILL_COL = 2;
        private const int SKILL_ROW = 8;
        private const int ALL_WIDTH = 656;
        private const int ALL_HEIGHT = 2560;
        private const int SKILL_ONE_WIDTH = 328;
        private const int SKILL_ONE_HEIGHT = 320;


        private int style;//技能种类
        private int num;//选择值

        private String filePath;
        private String path1;

        private String tempPath1;

        Bitmap bitmap1;


        Bitmap[,] data1 = new Bitmap[SKILL_ROW, SKILL_COL];


        Bitmap[,] backdata1 = new Bitmap[SKILL_ROW, SKILL_COL];


        public FightSkillPicEditer()
        {
            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 != SKILL_ONE_WIDTH || pictureBox1.Image.Height != SKILL_ONE_HEIGHT)
                {
                    MessageBox.Show("错误的图片大小！", "错误");
                    return;
                }
                

                data1[style, num] = new Bitmap(pictureBox1.Image);
                       

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

        private void SaveBmpToFile(int row, int col)
        {
            
                    for (int i = 0; i < SKILL_ONE_WIDTH; i++)
                    {
                        for (int j = 0; j < SKILL_ONE_HEIGHT; j++)
                        {
                            bitmap1.SetPixel(col * SKILL_ONE_WIDTH + i, row * SKILL_ONE_HEIGHT + j, data1[row, col].GetPixel(i, j));
                        }
                    }

                    bitmap1.Save(path1);

        }

        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_skill.sbp";


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

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



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


            bitmap1 = new Bitmap(tempPath1);

            for (int i = 0; i < SKILL_ROW; i++)
            {
                for (int j = 0; j <SKILL_COL; j++)
                {
                    data1[i, j] = new Bitmap(SKILL_ONE_WIDTH, SKILL_ONE_HEIGHT);

                    for (int offsetX = 0; offsetX < SKILL_ONE_WIDTH; offsetX++)
                    {
                        for (int offsetY = 0; offsetY < SKILL_ONE_HEIGHT; offsetY++)
                        {
                            if (((j * SKILL_ONE_WIDTH + offsetX) < bitmap1.Width) && ((i * SKILL_ONE_HEIGHT + offsetY) < bitmap1.Height))
                            {
                                data1[i, j].SetPixel(offsetX, offsetY, bitmap1.GetPixel((int)(j * SKILL_ONE_WIDTH + offsetX), (int)(i * SKILL_ONE_HEIGHT + offsetY)));

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

        }


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

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

                }
            }
        }
            


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

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


                }
            }
           
        }



        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            style = comboBox1.SelectedIndex;
            comboBox2.SelectedIndex = 0;
            comboBox2_SelectedIndexChanged_1(sender, e);

        }

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

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


    }
}
