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


namespace Obligatorisk
{
    class Airtower : ModelComponent
    {
        private Mesh mesh;
        private Material[] meshMaterials; // Materials for the mesh
        private Texture[] meshTextures; // Textures for the mesh
        private Device theDevice;
        private Model theModel;


        public Airtower(Model aModel, Device aDevice, Vector3 aPosition)
            : base(aPosition)
        {
            theDevice = aDevice;
            theModel = aModel;
            CreateMesh();
                
        }


        public void CreateMesh()
        {

            ExtendedMaterial[] materials = null;
            String path = @"..\..\";
            mesh = Mesh.FromFile(path + @"\3D-Models\airtow.x", MeshFlags.SystemMemory, theDevice, out materials);
            
            if (meshTextures == null)
            {
                // Egenskaber for materialer og texture udtrękkes 
                meshTextures = new Texture[materials.Length];
                meshMaterials = new Material[materials.Length];
                for (int i = 0; i < materials.Length; i++)
                {
                    meshMaterials[i] = materials[i].Material3D;
                    // Set the ambient color for the material (D3DX does not do this)
                    meshMaterials[i].Ambient = meshMaterials[i].Diffuse;
                    meshTextures[i] = TextureLoader.FromFile(theDevice, path + @"\3D-Models\KATow2.dds");
                }
            }


        }

        

        public void Render()
        {
            // Kamera
            theDevice.RenderState.Lighting = true;
            theDevice.Transform.World = (Matrix.RotationYawPitchRoll(Yaw, Pitch, Roll) *
                Matrix.Translation(Position)) * Matrix.Scaling(4.0f, 4.0f, 4.0f);
            for (int i = 0; i < meshMaterials.Length; i++)
            {
                theDevice.Material = meshMaterials[i];
                theDevice.SetTexture(0, meshTextures[i]);
                mesh.DrawSubset(i);
            }
        }

        
    }
}
