using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using BulletX.BulletDynamics.Dynamics;
using BulletX.BulletCollision.CollisionShapes;
using Microsoft.Xna.Framework.Graphics;
using MikuMikuDance.XNA.Stages;
using BulletX.LinerMath;
using System.Diagnostics;

namespace BulletTest
{
    class PhysicsObj : DrawableGameComponent
    {
        protected RigidBody body;
		protected CollisionShape collision;

		protected Model model;
		protected Vector3 color;
        public bool ShowPosition = false;
		
        public RigidBody PhysicsBody { get { return body; } }
        public CollisionShape PhysicsSkin { get { return collision; } }

		IMMDCamera camera;
        public IMMDCamera Context { get { return camera; } }

        
		protected static Random random = new Random();
        protected static int objCount = 0;
        
        public PhysicsObj(Game game, IMMDCamera context, Model model)
			: base(game) {
			this.model = model;
			this.camera = context;
			color = new Vector3(random.Next(255), random.Next(255), random.Next(255));
			color /= 255.0f;
		}

        public PhysicsObj(Game game, MMDCamera context)
			: base(game) {
			this.model = null;
			this.camera = context;
			color = new Vector3(random.Next(255), random.Next(255), random.Next(255));
			color /= 255.0f;
		}

		Matrix[] boneTransforms = null;
		int boneCount = 0;

		public override void Draw(GameTime gameTime) {
			if (model != null) {
				if (boneTransforms == null || boneCount != model.Bones.Count) {
					boneTransforms = new Matrix[model.Bones.Count];
					boneCount = model.Bones.Count;
				}

				model.CopyAbsoluteBoneTransformsTo(boneTransforms);

				//Camera camera = context.Camera;
				foreach (ModelMesh mesh in model.Meshes) {
					foreach (BasicEffect effect in mesh.Effects) {

						// the body has an orientation but also the primitives in the collision skin
						// owned by the body can be rotated!
                        //if (body.CollisionShape != null)
                        //    effect.World = boneTransforms[mesh.ParentBone.Index] * Matrix.CreateScale(scale) * body.CollisionShape.GetPrimitiveLocal(0).Transform.Orientation * body.Orientation * Matrix.CreateTranslation(body.Position);
                        //else
                        //    effect.World = boneTransforms[mesh.ParentBone.Index] * Matrix.CreateScale(scale) * body.Orientation * Matrix.CreateTranslation(body.Position);
                        Matrix trans;
                        ((DefaultMotionState)body.MotionState).m_graphicsWorldTrans.getXNAMatrix(out trans);
                        effect.World = /*boneTransforms[mesh.ParentBone.Index] */ trans;//BulletX.MathHelper.GetDisplayMatrix(((DefaultMotionState)body.MotionState).GraphicsWorldTransform);
                        if (ShowPosition)
                            Game.Window.Title = effect.World.Translation.ToString();
                        effect.View = camera.GetViewMatrix();
                        effect.Projection = camera.GetProjectionMatrix(Game.GraphicsDevice);

						effect.DiffuseColor = color;
                        
						//if (!this.PhysicsBody.IsActive)
						//    effect.Alpha = 0.4f;
						//else
						//    effect.Alpha = 1.0f;


						effect.EnableDefaultLighting();
						//effect.PreferPerPixelLighting = true;
					}
					mesh.Draw();
				}

			}

			
		}

		
    }
}
