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

namespace StrategicCombat.MenuScreens
{
    /// <summary>
    /// This class represent a screen with information about this game.
    /// </summary>
    public abstract class InformationScreen : GameScreen
    {
        #region Variables

        // Title and message.
        public string title;
        public string message;

        // Xbox icons
        public Texture2D bButtonTexture;

        // Pc icons
        public Texture2D escapeButtonTexture;

        // Constant x and y offsets
        public const int xOffset = 128;
        public const int yOffset = 72;

        #endregion

        #region Constructors

        /// <summary>
        /// Create a new how to play this game screen.
        /// </summary>
        public InformationScreen()
        {

            // Transition times.
            TransitionOnTime = TimeSpan.FromSeconds(0.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
        }

        /// <summary>
        /// Loads graphics content used by the screen.
        /// </summary>
        public override void LoadContent()
        {
            // Create a new content manager.
            ContentManager content = ScreenManager.Game.Content;
            
            // Load button textures
            this.bButtonTexture = content.Load<Texture2D>("MenuAssets/b");
            this.escapeButtonTexture = content.Load<Texture2D>("MenuAssets/escape");

            LoadSpecificContent();

        }

        public virtual void LoadSpecificContent()
        {
        }

        #endregion

        #region Methods

        /// <summary>
        /// Updates the screen.
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            // Call the update method in the base class.
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

        } // End of the Update method

        /// <summary>
        /// Handles user input for the controlling player.
        /// </summary>
        public override void HandleInput(InputState input)
        {
            if (ControllingPlayer.HasValue)
            {
                PlayerIndex playerIndex = ControllingPlayer.Value;

                PlayerIndex unwantedOutput;

                if (input.IsMenuCancel(playerIndex, out unwantedOutput))
                {
                    HandleMenuCancel();
                }
            }
          
        } // End of the HandleInput method.

        /// <summary>
        /// Handle MenuCancel inputs by removing this screen.
        /// </summary>
        private void HandleMenuCancel()
        {
            // Exit from this screen.
            ExitScreen();
        }

        /// <summary>
        /// Draw the screen.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            SpriteFont font = ScreenManager.Font;
            Texture2D frameTexture = ScreenManager.blankTexture;

            // Make the screen slide into place.
            float transitionOffset = (float)Math.Pow(TransitionPosition, 2);

            spriteBatch.Begin();

            Vector2 titlePosition = new Vector2(xOffset, yOffset + 10);
            Vector2 titleOrigin = Vector2.Zero;
            Color titleColor = Color.White * TransitionAlpha;
            float titleScale = 2f;

            titlePosition.Y -= transitionOffset * 100;

            // Draw a rectangle for the game title
            spriteBatch.Draw(frameTexture, new Rectangle(0, yOffset, 1280, 68), Color.Orange * TransitionAlpha);

            // Dark shadow
            spriteBatch.DrawString(font, this.title, new Vector2(titlePosition.X + 1, titlePosition.Y + 1), Color.DarkGray * TransitionAlpha, 0,
                                   titleOrigin, titleScale, SpriteEffects.None, 0);

            // Title text
            spriteBatch.DrawString(font, this.title, titlePosition, titleColor, 0, titleOrigin, titleScale, SpriteEffects.None, 0);

            // Draw a rectangle for buttons and keys
            spriteBatch.Draw(frameTexture, new Rectangle(0, 598, 1280, 48), Color.Orange * TransitionAlpha);

            // Button panel
            float buttonTextScale = 1.25f;
            String backText = "Exit from this screen";

#if (!XBOX360)

            // Draw select key with text (enter button)
            spriteBatch.Draw(escapeButtonTexture, new Vector2(xOffset, 720 - yOffset - 42), Color.White * TransitionAlpha);

            spriteBatch.DrawString(font, backText, new Vector2(xOffset + 41, 720 - yOffset - 40), Color.DarkGray * TransitionAlpha, 0,
                                   Vector2.Zero, buttonTextScale, SpriteEffects.None, 0);
            spriteBatch.DrawString(font, backText, new Vector2(xOffset + 40, 720 - yOffset - 41), Color.White * TransitionAlpha, 0,
                                   Vector2.Zero, buttonTextScale, SpriteEffects.None, 0);

#endif

#if (XBOX360)

            // Draw select key with text (a button)
            spriteBatch.Draw(bButtonTexture, new Vector2(xOffset, 720 - yOffset - 42), Color.White * TransitionAlpha);

            spriteBatch.DrawString(font, backText, new Vector2(xOffset + 41, 720 - yOffset - 40), Color.DarkGray * TransitionAlpha, 0,
                                   Vector2.Zero, buttonTextScale, SpriteEffects.None, 0);
            spriteBatch.DrawString(font, backText, new Vector2(xOffset + 40, 720 - yOffset - 41), Color.White * TransitionAlpha, 0,
                                   Vector2.Zero, buttonTextScale, SpriteEffects.None, 0);

#endif

            // Draw specific information on the information screen.
            DrawSpecificInformation(spriteBatch, font);

            spriteBatch.End();

        } // End of the draw method.

        /// <summary>
        /// A abstract class used to implement specific drawing.
        /// </summary>
        /// <param name="spriteBatch"></param>
        /// <param name="font"></param>
        public abstract void DrawSpecificInformation(SpriteBatch spriteBatch, SpriteFont font);

        #endregion

    } // End of class

} // End of namespace
