using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace LD20_Game
{
    public enum TransitionState
    {
        None,
        FadingOut,
        FadingIn
    }

    static class SceneManager
    {
        const float FadeSpeed = 2.0f;

        static TransitionState fadeState = TransitionState.None;
        static BaseScene nextScene;
        static BaseScene currentScene;
        static public BaseScene CurrentScene
        {
            get
            {
                return currentScene;
            }
        }

        static public void SwitchTo(BaseScene newScene)
        {
            transparency = 0;
            currentScene = newScene;
        }

        static float transparency;
        static public void TransitionTo(BaseScene newScene)
        {
            if (fadeState == TransitionState.None)
            {
                transparency = 0;

                nextScene = newScene;
                fadeState = TransitionState.FadingOut;
            }
        }

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

            return (float)amountToFade;
        }

        static public void Update(GameTime gameTime)
        {
            if (fadeState == TransitionState.FadingOut)
            {
                transparency += GetAmountToFade(gameTime);

                if (transparency >= 1.0f)
                {
                    fadeState = TransitionState.None;
                    SwitchTo(nextScene);
                }
            }
            else
            {
                currentScene.UpdateScene(gameTime);
            }
        }

        static public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            currentScene.DrawScene(spriteBatch, gameTime);

            if (fadeState == TransitionState.FadingOut)
            {
                spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied);
                spriteBatch.Draw(TextureManager.Textures["onepixel"],
                    spriteBatch.GraphicsDevice.Viewport.Bounds,
                    new Color(0.0f, 0.0f, 0.0f, transparency));
                spriteBatch.End();
            }
        }
    }
}
