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

namespace Obligatorisk
{
    public class ModelComponent
    {
        #region  3D-models (Frank & Kasper) 3. Maj
        private Mesh mesh;

        public Mesh Mesh
        {
            get { return mesh; }
            set { mesh = value; }
        }
        private Material[] meshMaterials; // Materials for the mesh

        public Material[] MeshMaterials
        {
            get { return meshMaterials; }
            set { meshMaterials = value; }
        }
        private Texture[] meshTextures; // Textures for the mesh

        public Texture[] MeshTextures
        {
            get { return meshTextures; }
            set { meshTextures = value; }
        }
        private Device theDevice;

        public Device TheDevice
        {
            get { return theDevice; }
            set { theDevice = value; }
        }
        private Model theModel;

        public Model TheModel
        {
            get { return theModel; }
            set { theModel = value; }
        }
        #endregion

        private Vector3 position;
        private Matrix world;
        private float pitch;
        private float yaw;
        private float roll;
        #region  3D-models (Frank & Kasper) 3. Maj
        private BoundingBox boundingBox;
        private Mesh boundingMesh;

        public Mesh BoundingMesh
        {
            get { return boundingMesh; }
            set { boundingMesh = value; }
        }

        public BoundingBox BoundingBox
        {
            get { return boundingBox; }
            set { boundingBox = value; }
        }
        #endregion

        #region  3D-models (Frank & Kasper) 3. Maj
        public ModelComponent(Model aModel, Device aDevice)
        {
            this.theDevice = aDevice;
            this.theModel = aModel;
            position = new Vector3(0, 0, 0);
            world = Matrix.Identity;
            pitch = 0.0F;
            yaw = 0.0F;
            roll = 0.0F;
            #region  3D-models (Frank & Kasper) 3. Maj
            this.boundingBox = new BoundingBox(3.0, 3.0);
            this.boundingMesh = Mesh.Box(this.theDevice, (float)this.boundingBox.Length, 2f, (float)this.boundingBox.Width);

            #endregion

        }
        

        public ModelComponent(Model aModel, Device aDevice, Vector3 aPosition)
        {
            theDevice = aDevice;
            theModel = aModel;
            position = aPosition;
            world = Matrix.Identity;
            pitch = 0.0F;
            yaw = 0.0F;
            roll = 0.0F;
            this.boundingBox = new BoundingBox(5.0, 5.0);
            this.boundingMesh = Mesh.Box(this.theDevice, 1f, 1f, 1f);
        }

        public ModelComponent(Model aModel, Device aDevice, Vector3 aPosition, BoundingBox boundingBox)
        {
            theDevice = aDevice;
            theModel = aModel;
            position = aPosition;
            world = Matrix.Identity;
            pitch = 0.0F;
            yaw = 0.0F;
            roll = 0.0F;
            this.boundingBox = boundingBox;
            this.boundingMesh = Mesh.Box(this.theDevice, (float)this.boundingBox.Length, 2f, (float)this.boundingBox.Width);
        }
        #endregion

        // Properties
        public Vector3 Position
        {
            get { return position; }
            set { position = value; }
        }

        public Matrix World
        {
            get { return world; }
            set { world = value; }
        }

        public float Pitch
        {
            get { return pitch; }
            set { pitch = value; }
        }

        public float Yaw
        {
            get { return yaw; }
            set { yaw = value; }
        }

        public float Roll
        {
            get { return roll; }
            set { roll = value; }
        }

        #region  3D-models (Frank & Kasper) 9. Maj
        // Implementeret virtual render metode for at kunne kalde Render() på alle objekter i theModel.
        public virtual void Render()
        {

        }

        public void DrawBoundingBox()
        {
            //Console.WriteLine("DrawBoundingBox called");
            Material boundingMeshMaterial = new Material();
            boundingMeshMaterial.AmbientColor = new ColorValue(Color.DarkGray.R, Color.DarkGray.G, Color.DarkGray.B);
            boundingMeshMaterial.DiffuseColor = new ColorValue(Color.DarkGray.R, Color.DarkGray.G, Color.DarkGray.B);
            //TODO: boundingboxen skal roteres i forhold til objectet
            this.theDevice.Transform.World = Matrix.Scaling((float)this.boundingBox.Length, 2f, (float)this.boundingBox.Width) * Matrix.Translation(this.position);
            this.theDevice.Material = boundingMeshMaterial;
            this.theDevice.SetTexture(0, null);
            boundingMesh.DrawSubset(0);
            

                
        }

        // Implementeret virtual render metode for at kunne kalde Update() på alle objekter i theModel.
        public virtual void Update()
        {
            //Console.WriteLine(this + " Test");
        }

        // Implementeret virtual CollisionDetect metode for at kunne kalde CollisionDetect() på alle objekter i theModel.
        public virtual bool CollisionDetect()
        {
            //Console.WriteLine(this + " Test");
            return false;
        }

        public void SetNewBoundingMeshWidth(float width)
        {
            this.boundingBox.Width = width;
        }
        public void SetNewBoundingMeshLength(float length)
        {
            this.boundingBox.Length = length;
        }
        #endregion
    }
}
