using System;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Bil_LD
{
    public class GameObject : IRenderable
    {
        private int _ID;
        private Vector3 _Position;
        private float _Roll;
        private float _Pitch;
        private float _Yaw;
        private Vector3 _Scale;

        private Mesh _Mesh;
        private Texture[] _Textures;
        private Material[] _Materials;
        private string _Filename;

        private static int ID_Current = 0;

        public GameObject(Device device, string filename, Vector3 position)
        {
            _Position = position;
            _Roll = 0;
            _Pitch = 0;
            _Yaw = 0;

            _Filename = filename;

            _ID = ID_Current;
            ID_Current++;
        }

        public bool CreateMesh(Device device)
        {
            try
            {
                ExtendedMaterial[] materials;

                _Mesh = Mesh.FromFile(Settings.GetSingleton().MediaPath + _Filename, MeshFlags.SystemMemory, device, out materials);

                _Textures = new Texture[materials.Length];
                _Materials = new Material[materials.Length];

                for (int i = 0; i < materials.Length; i++)
                {
                    if (materials[i].TextureFilename != null)
                    {
                        _Textures[i] = TextureLoader.FromFile(device, Settings.GetSingleton().MediaPath + materials[i].TextureFilename);
                    }
                    _Materials[i] = materials[i].Material3D;
                    _Materials[i].Ambient = _Materials[i].Diffuse;
                }
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show("Error: " + e.ToString());
                return false;
            }

            return true;
        }

        public void Render(Device device)
        {
            device.RenderState.Lighting = true;
            device.Transform.World = Matrix.Identity;
            device.Transform.World = Matrix.RotationYawPitchRoll(Yaw, Pitch, Roll) * Matrix.Translation(Position);

            for (int i = 0; i < _Materials.Length; i++)
            {
                // Set the material and texture for this subset
                device.Material = _Materials[i];
                device.SetTexture(0, _Textures[i]);

                // Draw the mesh subset
                _Mesh.DrawSubset(i);
            }
        }

        #region Properties
        public Vector3 Position
        {
            get
            {
                return _Position;
            }
            set
            {
                _Position = value;
            }
        }

        public Vector3 Scale
        {
            get
            {
                return _Scale;
            }
            set
            {
                _Scale = value;
            }
        }

        public int ID
        {
            get { return _ID; }
        }

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

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

        public float Yaw
        {
            get
            {
                return _Yaw;
            }
            set
            {
                _Yaw = value;
            }
        }
        #endregion

        public override string ToString()
        {
            return "ID: " + _ID + " Pos: " + _Position.X + ", " + _Position.Y + ", " + _Position.Z;
        }
    }
}
