using System;
using System.Collections.Generic;
using System.Linq;
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.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using MikuMikuDance.XNA.Model;
using MikuMikuDance.XNA;
using MikuMikuDance.XNA.Motion;

namespace MikuMikuDanceXNADemo
{
    /// <summary>
    /// MikuMikuDance for XNAのデモ
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        //MikuMikuDance for XNA
        MikuMikuDanceXNA mmd;
        //MMDモデルデータ
        MMDModel model;
        //MMDモーションデータ
        MMDMotion motion;
        
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            mmd = new MikuMikuDanceXNA(this);
        }

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

        /// <summary>
        /// LoadContent
        /// </summary>
        protected override void LoadContent()
        {
            //モデルのロード（大人の事情により、モデルとモーションファイルは入れてません。必要な方はMMDより)
            model = mmd.LoadModel("Miku", this, Matrix.Identity);
            //モーションのロード
            motion = mmd.LoadMotion("TrueMyHeart");
            model.Player.SetMotion(motion, true);
        }

        /// <summary>
        /// UnloadContent
        /// </summary>
        protected override void UnloadContent()
        {
        }

        bool EnterFlag = true;
        /// <summary>
        /// アップデート
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();
            if (Keyboard.GetState().IsKeyDown(Keys.Enter) || GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed)
            {
                if (EnterFlag)
                {
                    //ダンスの再生
                    model.Player.Start(0, gameTime, false);
                    EnterFlag = false;
                }
            }
            else
            {
                if (!EnterFlag)
                    EnterFlag = true;
            }

            
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            
            base.Draw(gameTime);
        }
    }
}
