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

namespace DirectXEksempel05
{
    class Model
    {
        private Engine engine; // Associeret Engine
        private Brick b1;
        private Brick b2;
        private Brick b3;
        private Point mouseLocation = new Point(0, 0);
        private char mouseButton;

        public Point MouseLocation
        {
            set { mouseLocation = value; }
            get { return mouseLocation; }
        }

        public char MouseButton
        {
            set { mouseButton = value; }
            get { return mouseButton; }
        }

        public Model(Engine anEngine)
        {
            engine = anEngine;
            MouseButton = ' ';
            b1 = new Brick(engine.GetDevice(), new Vector3(0, 0, 0), Color.Aquamarine);
            b2 = new Brick(engine.GetDevice(), new Vector3(5, 3, 0), Color.Yellow);
            b3 = new Brick(engine.GetDevice(), new Vector3(9, 8, 0), Color.Red);
        }

        public void Render()
        {
            b1.Render(engine.GetDevice());
            b2.Render(engine.GetDevice());
            b3.Render(engine.GetDevice());
        }

        public void UpdateModel()
        {
            // Omregn musens placering til world koordinater
            Vector3 nearVector = new Vector3(MouseLocation.X, MouseLocation.Y, 0);
            Vector3 farVector = new Vector3(MouseLocation.X, MouseLocation.Y, 1);
            Vector3 lower;
            Vector3 upper;
            // Create ray.
            nearVector.Unproject(engine.GetDevice().Viewport,
                engine.Projection, engine.View,
                Matrix.Translation(b1.Position) * Matrix.Scaling(0.5f, 0.5f, 0.5f));
            farVector.Unproject(engine.GetDevice().Viewport,
                engine.Projection, engine.View,
                Matrix.Translation(b1.Position) * Matrix.Scaling(0.5f, 0.5f, 0.5f));
            farVector.Subtract(nearVector);
            // Perform intersection test for the bounding box and ray.
            lower = b1.Position - new Vector3(0.5f, 0.5f, 0.5f);
            upper = b1.Position + new Vector3(0.5f, 0.5f, 0.5f);
            if (Geometry.BoxBoundProbe(lower, upper, nearVector, farVector))
            {
                // Er boksen fundet?
                MessageBox.Show("Brick 1");
            }
            // Omregn musens placering til world koordinater
            nearVector = new Vector3(MouseLocation.X, MouseLocation.Y, 0);
            farVector = new Vector3(MouseLocation.X, MouseLocation.Y, 1);
            // Create ray.
            nearVector.Unproject(engine.GetDevice().Viewport,
                engine.GetDevice().Transform.Projection,
                engine.GetDevice().Transform.View,
                Matrix.Translation(b2.Position) * Matrix.Scaling(0.5f, 0.5f, 0.5f));
            farVector.Unproject(engine.GetDevice().Viewport,
                engine.GetDevice().Transform.Projection,
                engine.GetDevice().Transform.View,
                Matrix.Translation(b2.Position) * Matrix.Scaling(0.5f, 0.5f, 0.5f));
            farVector.Subtract(nearVector);
            // Perform intersection test for the bounding box and ray.
            lower = upper = b2.Position;
            lower.Subtract(new Vector3(0.5f, 0.5f, 0.5f));
            upper.Add(new Vector3(0.5f, 0.5f, 0.5f));
            if (Geometry.BoxBoundProbe(lower, upper, nearVector, farVector))
            {
                // Er boksen fundet?
                MessageBox.Show("Brick 2");
            }
            // Omregn musens placering til world koordinater
            nearVector = new Vector3(MouseLocation.X, MouseLocation.Y, 0);
            farVector = new Vector3(MouseLocation.X, MouseLocation.Y, 1);
            // Create ray.
            nearVector.Unproject(engine.GetDevice().Viewport,
                engine.GetDevice().Transform.Projection,
                engine.GetDevice().Transform.View,
                Matrix.Translation(b3.Position) * Matrix.Scaling(0.5f, 0.5f, 0.5f));
            farVector.Unproject(engine.GetDevice().Viewport,
                engine.GetDevice().Transform.Projection,
                engine.GetDevice().Transform.View,
                Matrix.Translation(b3.Position) * Matrix.Scaling(0.5f, 0.5f, 0.5f));
            farVector.Subtract(nearVector);
            // Perform intersection test for the bounding box and ray.
            lower = b3.Position - new Vector3(0.5f, 0.5f, 0.5f);
            upper = b3.Position + new Vector3(0.5f, 0.5f, 0.5f);
            if (Geometry.BoxBoundProbe(lower, upper, nearVector, farVector))
            {
                // Er boksen fundet?
                MessageBox.Show("Brick 3");
            }
        }
    }
}
