using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace XnaXsi
{
   public class Game1 : Microsoft.Xna.Framework.Game
   {
      GraphicsDeviceManager graphics;
      SpriteBatch spriteBatch;

      Model torus;
      Matrix[] torusFinalBones;
      
      
      public Game1()
      {
         graphics = new GraphicsDeviceManager(this);
         Content.RootDirectory = "Content";
      }


      protected override void Initialize()
      {
         base.Initialize();
      }


      protected override void LoadContent()
      {
         spriteBatch = new SpriteBatch(GraphicsDevice);

         torus = Content.Load<Model>("Models\\TorusRing");
         torusFinalBones = new Matrix[torus.Bones.Count];
      }


      protected override void UnloadContent()
      {

      }


      protected override void Update(GameTime gameTime)
      {
         if(Keyboard.GetState().IsKeyDown(Keys.Escape))
            this.Exit();

         base.Update(gameTime);
      }


      protected override void Draw(GameTime gameTime)
      {
         graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
         float aspectRatio = GraphicsDevice.Viewport.AspectRatio;

         torus.CopyAbsoluteBoneTransformsTo(torusFinalBones);
         
         foreach(ModelMesh mesh in torus.Meshes)
         {
            foreach(BasicEffect effect in mesh.Effects)
            {
               effect.World = torusFinalBones[mesh.ParentBone.Index];

               effect.View = Matrix.CreateTranslation(new Vector3(0, 0, -15));

               effect.Projection = Matrix.CreatePerspectiveFieldOfView(
                                   MathHelper.ToRadians(45.0f),
                                   aspectRatio, 0.1f, 1000);
               
               effect.EnableDefaultLighting();
               
               mesh.Draw();
            }
         }

         base.Draw(gameTime);
      }
   }
}
