using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Tao.OpenGl;

namespace Log740
{
    class ExempleKeyboard : AbstractExemple
    {
        static private int ROTATE_ZONE = 250;
        static private double VITESSE = 1;

        private double rotation_x = -Math.PI / 2;
        private double rotation_z = 0;

        private Quaternion quatz = Quaternion.Identity();
        private Quaternion quatx = Quaternion.FromAxisAngle(1, 0, 0, -Math.PI / 2);

        public ExempleKeyboard() : base(10)
        {
            this.MouseMove += new MouseEventHandler(this.Mouse_Move);
            this.MouseClick += new MouseEventHandler(this.Mouse_Click);
        }

        protected override void AnimateScene()
        {
            base.AnimateScene();

            if (crosshair.Location.X < ROTATE_ZONE)
            {
                double blend = 1 - (crosshair.Location.X / ROTATE_ZONE);
                rotation_z -= 0.1 * (blend * blend);
                quatz = Quaternion.FromAxisAngle(0, 0, 1, rotation_z);
            }
            else if (crosshair.Location.X > DisplayRectangle.Width - ROTATE_ZONE)
            {
                double blend = (crosshair.Location.X - (DisplayRectangle.Width - ROTATE_ZONE)) / ROTATE_ZONE;
                rotation_z += 0.1 * (blend * blend);
                quatz = Quaternion.FromAxisAngle(0, 0, 1, rotation_z);
            }

            if (crosshair.Location.Y < ROTATE_ZONE)
            {
                double blend = 1 - (crosshair.Location.Y / ROTATE_ZONE);
                rotation_x += 0.1 * (blend * blend);

                rotation_x = Math.Min(rotation_x, 0);

                quatx = Quaternion.FromAxisAngle(1, 0, 0, rotation_x);
            }
            else if (crosshair.Location.Y > DisplayRectangle.Height - ROTATE_ZONE)
            {
                double blend = (crosshair.Location.Y - (DisplayRectangle.Height - ROTATE_ZONE)) / ROTATE_ZONE;
                rotation_x -= 0.1 * (blend * blend);

                rotation_x = Math.Max(rotation_x, -Math.PI);
                    
                quatx = Quaternion.FromAxisAngle(1, 0, 0, rotation_x);
            }

            Vecteur3F mouvement = new Vecteur3F();

            if (IsKeyPressed(Keys.Up) || IsKeyPressed(Keys.W))
            {
                mouvement.Y += VITESSE;
            }
            if (IsKeyPressed(Keys.Down) || IsKeyPressed(Keys.S))
            {
                mouvement.Y -= VITESSE;
            }
            if (IsKeyPressed(Keys.Left) || IsKeyPressed(Keys.A))
            {
                mouvement.X -= VITESSE;
            }
            if (IsKeyPressed(Keys.Right) || IsKeyPressed(Keys.D))
            {
                mouvement.X += VITESSE;
            }

            position = position + (-quatz * mouvement);
        }

        override protected void RefreshScene()
        {
            base.RefreshScene(); /* pour Esc -> Close */

            CameraRotation = quatx * quatz;

            CameraPosition = position + ground_offset;
        }

        private void Mouse_Move(object sender, MouseEventArgs e)
        {
            this.crosshair.Location.X = e.X;
            this.crosshair.Location.Y = this.DisplayRectangle.Height - e.Y;
        }

        private void Mouse_Click(object sender, MouseEventArgs e)
        {
            this.crosshair.Location.X = e.X;
            this.crosshair.Location.Y = this.DisplayRectangle.Height - e.Y;
            if (e.Button == MouseButtons.Left)
            {
                Shoot();
            }
            else if (e.Button == MouseButtons.Right)
            {
                if (selected_enemy != null)
                {
                    selected_enemy.Color = new Vecteur3F(1, 0, 0);
                }

                selected_enemy = SelectedEnemy();

                if (selected_enemy != null)
                {
                    selected_enemy.Color = new Vecteur3F(1, 1, 1);
                }
            }
        }
    }
}
