using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommonAI.RTS; using CommonLang.Vector;
using CommonLang;

namespace GameEditorPlugin.Win32
{
    public partial class DisplayTerrainPanel : UserControl
    {
        private DisplayTerrain display;

        public DisplayTerrainPanel()
        {
            InitializeComponent();

            this.pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
            this.pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
            this.pictureBox1.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);
            this.pictureBox1.MouseUp += new MouseEventHandler(pictureBox1_MouseUp);
            this.pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
        }

        public event MouseEventHandler PictureBoxMouseDown { add { this.pictureBox1.MouseDown += value; } remove { this.pictureBox1.MouseDown -= value; } }
        public event MouseEventHandler PictureBoxMouseUp { add { this.pictureBox1.MouseUp += value; } remove { this.pictureBox1.MouseUp -= value; } }
        public event MouseEventHandler PictureBoxMouseMove { add { this.pictureBox1.MouseMove += value; } remove { this.pictureBox1.MouseMove -= value; } }

        public DisplayTerrain Display
        {
            get { return display; }
            set
            {
                this.display = value;
                if (display != null)
                {
                    if (display.Terrain != null)
                    {
                        float scale = Math.Max(
                               ((float)pictureBox1.Width) / display.Terrain.TotalWidth,
                               ((float)pictureBox1.Height) / display.Terrain.TotalHeight);
                        this.display.setCameraScale(scale, scale);
                        this.display.setCamera(display.Terrain.TotalWidth / 2, display.Terrain.TotalHeight / 2);
                    }
                }
            }
        }

        public int FPS
        {
            get { return 1000 / timer1.Interval; }
            set { timer1.Interval = 1000 / value; }
        }
        public PictureBox GetPictureBox()
        {
            return pictureBox1;
        }

        private void DisplayTerrainPanel_Enter(object sender, EventArgs e)
        {
            pictureBox1.Focus();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Display != null)
            {
                Display.update(timer1.Interval);
                pictureBox1.Refresh();
            }
        }

        private Vector2 pic_lastMouesDown;
        private Vector2 pic_lastCameraPos;

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            pictureBox1.Focus();
            if (Display != null)
            {
                pic_lastMouesDown = new Vector2(e.Location.X, e.Location.Y);
                pic_lastCameraPos = new Vector2(Display.CameraX, Display.CameraY);
            }
        }
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (Display != null)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    if (pic_lastMouesDown != null)
                    {
                        float x = pic_lastCameraPos.X + Display.screenToWorldSizeX(pic_lastMouesDown.X - e.X);
                        float y = pic_lastCameraPos.Y + Display.screenToWorldSizeY(pic_lastMouesDown.Y - e.Y);
                        Display.setCamera(x, y);
                    }
                }
            }
        }
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            pic_lastMouesDown = null;
            pic_lastCameraPos = null;
        }
        private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            if (Display != null)
            {
                int d = CMath.getDirect(e.Delta);
                if (d > 0)
                {
                    float newD = Display.getCameraScale() * 1.1f;
                    Display.setCameraScale(newD, newD);
                }
                else if (d < 0)
                {
                    float newD = Display.getCameraScale() / 1.1f;
                    Display.setCameraScale(newD, newD);
                }
            }
        }
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (Display != null)
            {
                try
                {
                    this.Display.setWindow(new RectangleF(0, 0, pictureBox1.Width, pictureBox1.Height));
                    this.Display.render(e.Graphics);
                }
                catch(Exception err)
                {
                    MessageBox.Show(err.Message);
                }
            }
        }


    }
}