using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace LD20_Game
{
    class TitleScene : BaseScene
    {
        Vector2 pressAnyKeyPos = new Vector2(190, 380);

        float FadeAmountPerSecond = 2.0f;
        const float MaxFadeAmount = 0.4f;
        float transparency = 1.0f;
        bool fading = true;

        TimeSpan exitScreenAt = TimeSpan.MaxValue;

        public override void UpdateScene(GameTime gameTime)
        {
            if (exitScreenAt < gameTime.TotalGameTime)
            {
                SceneManager.SwitchTo(new IntroLevel());          
            }

            CheckGamepadsForStartButton(gameTime);
            CheckKeyboardForAnyKey(gameTime);

            float amountToFade = GetAmountToFade(gameTime);
            if (fading)
                transparency -= amountToFade;
            else
                transparency += amountToFade;

            if (transparency < MaxFadeAmount)
            {
                transparency = MaxFadeAmount;
                fading = false;
            }

            if (transparency > 1.0f)
            {
                transparency = 1.0f;
                fading = true;
            }


            base.UpdateScene(gameTime);
        }

        private void CheckKeyboardForAnyKey(GameTime gameTime)
        {
            if (Keyboard.GetState().GetPressedKeys().Length > 0)
            {
                InputManager.ControlMethod = ControlMethod.Keyboard;
                ExitScene(gameTime);
            }
        }

        private void CheckGamepadsForStartButton(GameTime gameTime)
        {
            for (int i = (int)PlayerIndex.One; i < (int)PlayerIndex.Four; i++)
            {
                GamePadState gs = GamePad.GetState((PlayerIndex)i);

                if (gs.Buttons.A == ButtonState.Pressed  ||
                    gs.Buttons.Start == ButtonState.Pressed)
                {
                    InputManager.ControlMethod = ControlMethod.Gamepad;
                    InputManager.PlayerIndex = (PlayerIndex)i;
                    ExitScene(gameTime);
                }
            }
        }

        private void ExitScene(GameTime gameTime)
        {
            FadeAmountPerSecond = 6.0f;
            exitScreenAt = gameTime.TotalGameTime + SoundManager.Sounds["press-start"].Duration + TimeSpan.FromMilliseconds(500);
            SoundManager.Play("press-start");
        }

        private float GetAmountToFade(GameTime gameTime)
        {
            double scaleFactor = gameTime.ElapsedGameTime.TotalMilliseconds / 1000;
            double amountToFade = FadeAmountPerSecond * scaleFactor;

            return (float)amountToFade;
        }

        public override void DrawScene(SpriteBatch spriteBatch, GameTime gameTime)
        {
            
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied);
            spriteBatch.Draw(TextureManager.Textures["titlescreen"], Vector2.Zero, Color.White);

            spriteBatch.Draw(TextureManager.Textures["pressanykey"], pressAnyKeyPos, new Color(255.0f, 255.0f, 255.0f, transparency));

            spriteBatch.End();

     	    base.DrawScene(spriteBatch, gameTime);
        }
    }
}
