using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;
using ChtiCanon;

namespace Canon
{
    public partial class GameControl : UserControl
    {
        // Drawing device
        private Device DX_Device = null;
        private Game game;
        public bool Rendering = false;
        private Program.GameThread gt;
        public Program.GameThread GameThread { get { return gt; } }

        public GameControl()
        {
            InitializeComponent();
        }

        public bool init(Program.GameThread gt)
        {
            this.gt = gt;
            try
            {
                PresentParameters pps = new PresentParameters();
                pps.Windowed = true; // Specify that it will be in a window
                pps.SwapEffect = SwapEffect.Discard; // After the current screen is drawn, it will be automatically // deleted from memory
                DX_Device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, pps); // Put everything into the device
                if (DX_Device.DeviceCaps.MaxTextureWidth < 4096 || DX_Device.DeviceCaps.MaxTextureHeight < 4096)
                    throw new ApplicationException("La carte vidéo doit supporter des textures d'au moins 4096 pixels au carré.");
                game = new Game(DX_Device, this);
                Rendering = true;
                return true;
            }
            catch (DirectXException e)
            {
                MessageBox.Show(e.ToString(), "Error"); // Handle all the exceptions
            }
            catch (ApplicationException s)
            {
                MessageBox.Show(s.ToString(), "Votre carte vidéo ne présente pas la configuration minimale requise");
            }
            return false;
        }

        public void render()
        {
            if (Rendering)
            {
                Canon.Game.Drawer.beginScene();
                game.draw();
                Canon.Game.Drawer.endScene();
            }
        }

        public void handle()
        {
            game.handle();
        }

        private void GameControl_MouseMove(object sender, MouseEventArgs e)
        {
            game.mouse.setPosition((float)e.X / Width, (float)e.Y / Height);
        }

        private void GameControl_MouseDown(object sender, MouseEventArgs e)
        {
            game.mouse.setDown(e.Button);
            game.mouse.setClick(e.Button);
        }

        private void GameControl_MouseUp(object sender, MouseEventArgs e)
        {
            game.mouse.setUp(e.Button);
            game.mouse.setReleased(e.Button);
        }

        private void GameControl_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            game.mouse.setDoubleClick(e.Button);
        }

        private void GameControl_MouseLeave(object sender, EventArgs e)
        {
            game.mouse.setLeft();
        }

        public void close()
        {
            DX_Device.Dispose();
        }
    }
}
