using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using GraphiXNA.Cameras;

namespace GraphiXNA.Utils
{
    public static class ModelHelper
    {
        public static void DrawModel(Model model, ref Matrix world)
        {
            DrawModel(model, Camera._ActiveCamera, ref world);
        }

        public static void DrawModel(Model model, Camera camera, ref Matrix world)
        {
            Matrix[] transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);

            for (int i = 0; i < model.Meshes.Count; i++)
            {
                for (int j = 0; j < model.Meshes[i].Effects.Count; j++)
                {
                    BasicEffect b = model.Meshes[i].Effects[j] as BasicEffect;
                    if (b != null)
                    {
                        b.World = transforms[model.Meshes[i].ParentBone.Index] * world;
                        b.View = camera._View;
                        b.Projection = camera._Projection;
                    }
                    model.Meshes[i].Draw();
                }
            }
        }

        public static void DrawModel(Model model, Camera camera, ref Matrix world, Action<Effect> setupEffectParameters)
        {
            Matrix[] transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);

            for (int i = 0; i < model.Meshes.Count; i++)
            {
                for (int j = 0; j < model.Meshes[i].Effects.Count; j++)
                {
                    BasicEffect b = model.Meshes[i].Effects[j] as BasicEffect;
                    if (b != null)
                    {
                        b.World = transforms[model.Meshes[i].ParentBone.Index] * world;
                        b.View = camera._View;
                        b.Projection = camera._Projection;
                    }
                    setupEffectParameters(model.Meshes[i].Effects[j]);
                    model.Meshes[i].Draw();
                }
            }
        }


    }
}
