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;

namespace LD20_Game
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);

            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;
            //graphics.IsFullScreen = true;

            graphics.ApplyChanges();
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            MediaPlayer.IsRepeating = true;

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // Graphics
            TextureManager.Add("onepixel", Content.Load<Texture2D>(@"Textures\onepixel"));
            TextureManager.Add("controller", Content.Load<Texture2D>(@"FullScreens\controller"));
            TextureManager.Add("glengames", Content.Load<Texture2D>(@"FullScreens\glengames"));
            TextureManager.Add("titlescreen", Content.Load<Texture2D>(@"FullScreens\titlescreen"));
            TextureManager.Add("pressanykey", Content.Load<Texture2D>(@"Textures\pressanykey"));
            TextureManager.Add("lockerroombg", Content.Load<Texture2D>(@"FullScreens\lockerroombg"));
            TextureManager.Add("buddi", Content.Load<Texture2D>(@"Textures\buddi"));
            TextureManager.Add("playersprite", Content.Load<Texture2D>(@"Textures\playersprite"));
            TextureManager.Add("platform", Content.Load<Texture2D>(@"Textures\platform"));
            TextureManager.Add("lampoverlay", Content.Load<Texture2D>(@"Textures\lampoverlay"));
            TextureManager.Add("exitdoor", Content.Load<Texture2D>(@"Textures\exitdoor"));
            TextureManager.Add("exitsign", Content.Load<Texture2D>(@"Textures\exitsign"));
            TextureManager.Add("goalone", Content.Load<Texture2D>(@"Textures\goalone"));
            TextureManager.Add("minebg", Content.Load<Texture2D>(@"FullScreens\minebg"));
            TextureManager.Add("level18sign", Content.Load<Texture2D>(@"Textures\LevelSigns\level18"));
            TextureManager.Add("level19sign", Content.Load<Texture2D>(@"Textures\LevelSigns\level19sign"));
            TextureManager.Add("ladder", Content.Load<Texture2D>(@"Textures\ladder"));
            TextureManager.Add("spikes", Content.Load<Texture2D>(@"Textures\spikes"));
            TextureManager.Add("mudslidespawner", Content.Load<Texture2D>(@"Textures\mudslidespawner"));
            TextureManager.Add("mudslide", Content.Load<Texture2D>(@"Textures\mudslide"));
            TextureManager.Add("mousecursor", Content.Load<Texture2D>(@"Textures\mousecursor"));
            TextureManager.Add("ending", Content.Load<Texture2D>(@"FullScreens\ending"));

            TextureManager.Add("spikewarning", Content.Load<Texture2D>(@"Textures\Warnings\spikewarning"));
            TextureManager.Add("mudslidewarning", Content.Load<Texture2D>(@"Textures\Warnings\mudwarning"));

            // Sounds
            SoundManager.Add("tick", 40, Content.Load<SoundEffect>(@"Sounds\tick"));
            SoundManager.Add("press-start", 1000, Content.Load<SoundEffect>(@"Sounds\press-start"));
            SoundManager.Add("collapse", 1000, Content.Load<SoundEffect>(@"Sounds\collapsetrim"));
            SoundManager.Add("jump", Content.Load<SoundEffect>(@"Sounds\jump"));
            SoundManager.Add("doorsaysno", Content.Load<SoundEffect>(@"Sounds\doorsaysno"));
            SoundManager.Add("welcometobuddi", Content.Load<SoundEffect>(@"Sounds\welcometobuddi"));
            SoundManager.Add("alarm1", Content.Load<SoundEffect>(@"Sounds\alarm1"));
            SoundManager.Add("alarm2", Content.Load<SoundEffect>(@"Sounds\alarm2"));
            SoundManager.Add("alarm3", Content.Load<SoundEffect>(@"Sounds\alarm3"));
            SoundManager.Add("dead", Content.Load<SoundEffect>(@"Sounds\dead"));
            SoundManager.Add("leveldone", Content.Load<SoundEffect>(@"Sounds\leveldone"));


            // Music
            MusicManager.Add("intro", Content.Load<Song>(@"Music\intro"));
            MusicManager.Add("cave1", Content.Load<Song>(@"Music\cave1"));
            MusicManager.Add("cave2", Content.Load<Song>(@"Music\cave2"));
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Debug: emergency reset
            if (Keyboard.GetState().IsKeyDown(Keys.F2))
                SceneManager.SwitchTo(new ControllerAlertScreen());

            if (SceneManager.CurrentScene != null)
            {
                SceneManager.Update(gameTime);
            }
            else
            {
                /*
                // debug
                InputManager.ControlMethod = ControlMethod.Keyboard;
                InputManager.PlayerIndex = PlayerIndex.One; */
                SceneManager.SwitchTo(new ControllerAlertScreen());
            }

            //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.Black);

                 SceneManager.Draw(spriteBatch, gameTime);

            base.Draw(gameTime);
        }
    }
}
