using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

using EVE.EVEObjects;
using EVE.EVECameras;
using EVE.EVELights;

namespace EVE.EVEShaders
{
    public class EVEEffect : EVEShader
    {
        internal EVEEffect() : base("Microsoft.Xna.Framework.Graphics.BasicEffect") { }

        public EVEEffect(string asset) : base(asset) { }

        internal override void LoadContent(GraphicsDevice myDevice, ContentManager myLoader)
        {
            if (myAsset == "Microsoft.Xna.Framework.Graphics.BasicEffect")
            {
                myEffect = new BasicEffect(myDevice, null);
            }
            else
            {
                base.LoadContent(myDevice, myLoader);
            }
        }

        internal override void SetParameters(EVEObject myObject)
        {
            /*
            Matrix World =
                EVECameraManager.ActiveCamera.World *
                Matrix.CreateScale(myObject.Scaling) *
                Matrix.CreateFromQuaternion(myObject.Rotation) *
                Matrix.CreateTranslation(myObject.Position);
            */

            Matrix World = 
                EVECameraManager.ActiveCamera.World * 
                myObject.WorldMatrix;

            if (null != myEffect.Parameters["World"]) myEffect.Parameters["World"].SetValue(World);
            if (null != myEffect.Parameters["View"]) myEffect.Parameters["View"].SetValue(EVECameraManager.ActiveCamera.View);
            if (null != myEffect.Parameters["Project"]) myEffect.Parameters["Project"].SetValue(EVECameraManager.ActiveCamera.Projection);

            if (myEffect.Parameters["EyePosition"] != null)
                myEffect.Parameters["EyePosition"].SetValue(EVECameraManager.ActiveCamera.Position);
            

            if (myEffect.Parameters["DiffuseColor"] != null)
                myEffect.Parameters["DiffuseColor"].SetValue(myObject.DiffuseColor);

            if (myEffect.Parameters["SpecularPower"] != null)
                myEffect.Parameters["SpecularPower"].SetValue(myObject.SpecularPower);

            if (myEffect.Parameters["LightDirection"] != null)
                myEffect.Parameters["LightDirection"].SetValue(Vector3.Normalize(EVELightManager.ActiveLight.Position));

            if (myEffect.Parameters["LightDiffuseColor"] != null)
                myEffect.Parameters["LightDiffuseColor"].SetValue(EVELightManager.ActiveLight.DiffuseColor);

            if (myEffect.Parameters["LightSpecularColor"] != null)
                myEffect.Parameters["LightSpecularColor"].SetValue(EVELightManager.ActiveLight.SpecularColor);

            base.SetParameters(myObject);
        }
    }
}