using System;
using System.Collections.Generic;
using System.Text;
using ECOT.Engine;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using ECOT.Game.Buildings;
using ECOT.Game.States;

namespace ECOT.Game
{
    public class GameRenderer : IUpdatable
    {
        private Map map = null;

        private bool showCursor = true;
        private bool drawCityAreas = false;
        private bool drawCityGrids = false;
        private bool enabled = true;

        private DepthStencilBuffer stencilBuffer = null;
        /// <summary>
        /// 
        /// </summary>
        private Texture2D gamePaused = null;

        private static GameRenderer instance = null;

        public GameRenderer(Map map)
        {
            GameRenderer.instance = this;

            this.map = map;
            this.CreateErrorBox();

            this.stencilBuffer = new DepthStencilBuffer(Kernel.Instance.GraphicsDevice, 1024, 1024, DepthFormat.Depth24Stencil8, MultiSampleType.None, 0);
            this.gamePaused = Kernel.Instance.Medias.Get<Texture2D>(@"Textures\InGameMenu\GamePause");
        }

        /// <summary>
        /// Affiche un message d'erreur en cas d'impossibilité d'ouvrir une texture
        /// </summary>
        private void CreateErrorBox()
        {
            // Disponible lors d'un numéro consacré à la GUI
            /*int width = 200;
            int height = 150;

            this.errorBox = new Form(
                new Vector2((Kernel.Instance.Width - width) / 2, (Kernel.Instance.Height - height) / 2),
                new Vector2(width, height), "Error", null, Color.White, Color.Aqua, 1);
            this.errorBox.Add(new Label("error_message", new Vector2(10, 50), "", Label.Alignment.Left, 380, Color.White,
                Kernel.Instance.Medias.Get<SpriteFont>(@"fonts\arial")));
            */
        }

        /// <summary>
        /// Display or not Cursor
        /// </summary>
        public bool ShowCursor
        {
            get { return this.showCursor; }
            set { this.showCursor = value; }
        }

        #region IUpdatable Membres

        public bool Enabled
        {
            get
            {
                return this.enabled; 
            }
            set
            {
                this.enabled = value; 
            }
        }

        public void Update(GameTime gameTime)
        {
        }

        #endregion

        public void Unload()
        {
            this.stencilBuffer.Dispose();
        }

        public void Render(GameTime gameTime, byte alpha)
        {
            if (this.map != null)
                this.map.Render(gameTime, alpha);

            if (this.showCursor)
                Kernel.Instance.Cursor.Present();

            if (GameController.Instance.Speed == GameSpeed.Pause)
            {
                Kernel.Instance.SpriteBatch.Begin();
                Kernel.Instance.SpriteBatch.Draw(
                this.gamePaused,
                new Vector2(Kernel.Instance.Width / 2 - this.gamePaused.Width, (Kernel.Instance.Height - 64) / 2),
                new Rectangle(0, Kernel.Instance.LocaleIndex * 64, this.gamePaused.Width, 64),
                Color.White);
                Kernel.Instance.SpriteBatch.End();
            }
        }


        /// <summary>
        /// Affiche la zone d'influence des villes
        /// </summary>
        public bool DrawCityAreas
        {
            get { return this.drawCityAreas; }
            set
            {
                foreach (City city in this.map.Cities)
                    city.RenderState.DrawArea = value;
                this.drawCityAreas = value;
            }
        }

        /// <summary>
        /// Affiche la grille des villes
        /// </summary>
        public bool DrawCityGrids
        {
            get { return this.drawCityGrids; }
            set
            {
                foreach (City city in this.map.Cities)
                    city.RenderState.DrawGrid = value;
                this.drawCityGrids = value;
            }
        }

        public static GameRenderer Instance
        {
            get { return GameRenderer.instance; }
            set { GameRenderer.instance = value; }
        }
    }
}
