using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using ECOT.Engine;
using Microsoft.Xna.Framework.Input;
using ECOT.Engine.Graphics;
using System.Collections.Generic;
using ECOT.Game.Buildings;
using Microsoft.Xna.Framework.Audio;
using System.Diagnostics;
using System.IO;

namespace ECOT.Game.States
{
    public class GameplayState : GameState
    {
        #region Membres

        /// <summary>
        /// 
        /// </summary>
        private float currentAlpha = 0.0f;
        /// <summary>
        /// 
        /// </summary>
        private bool fadeOut = false;
        /// <summary>
        /// 
        /// </summary>
        private bool fadeIn = false;

        #endregion

        public GameplayState()
        {
            // Transition de l'état de jeu
            this.TransitionOnTime = this.TransitionOffTime = TimeSpan.FromSeconds(0.5);
#if XBOX
            this.PresenceMode = Microsoft.Xna.Framework.GamerServices.GamerPresenceMode.FreePlay;
#endif


        }

        public override string ToString()
        {
            return "Gameplay";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="loadAllContent"></param>
        public override void LoadGraphicsContent(bool loadAllContent)
        {
                new GameController("Gameplay", "France").Initialize();
            
                // Crée le renderer
                if (GameRenderer.Instance == null)
                    new GameRenderer(GameController.Instance.Map);
            
            

            
            
            if (this.loaded) return;

            base.LoadGraphicsContent(loadAllContent);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="unloadAllContent"></param>
        public override void UnloadGraphicsContent(bool unloadAllContent)
        {
        }


        /// <summary>
        /// Updates the background screen. Unlike most screens, this should not
        /// transition off even if it has been covered by another screen: it is
        /// supposed to be covered, after all! This overload forces the
        /// coveredByOtherScreen parameter to false in order to stop the base
        /// Update method wanting to transition off.
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, false);

            if (GameRenderer.Instance != null && GameController.Instance != null)
                GameRenderer.Instance.ShowCursor = !coveredByOtherScreen;

            //if (GameController.Instance != null) GameController.Instance.Enabled = !otherScreenHasFocus;
            if (otherScreenHasFocus) return;

            if (GameController.Instance != null) 
                GameController.Instance.Update(gameTime);
            Kernel.Instance.Camera.Update(gameTime);
        }

        /// <summary>
        /// Draws the background screen.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            if(GameRenderer.Instance != null)
                GameRenderer.Instance.Render(gameTime, 255);

            if (this.currentAlpha > 0.0f)
            {
                Kernel.Instance.SpriteBatch.Begin();
                Kernel.Instance.SpriteBatch.Draw(
                    Kernel.Instance.Medias.Get<Texture2D>(@"Bootstrap\Blank"),
                    new Rectangle(0, 0, Kernel.Instance.Width, Kernel.Instance.Height),
                    new Color(0, 0, 0, this.currentAlpha));
                Kernel.Instance.SpriteBatch.End();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        InputState input = new InputState();
    }
}
