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 FredriksdalAssault2
{
    /// <summary>
    /// A popup message box screen, used to display the controls for 
    /// a keyboard.
    /// </summary>
    class KeyboardControlScreen : GameScreen
    {
        #region Variables

        string title;
        string message;
        Texture2D gradientTexture;

        #endregion

        #region Events

        public event EventHandler<PlayerIndexEventArgs> Cancelled;

        #endregion

        #region Constructors

        /// <summary>
        /// Create a new gamepad control screen.
        /// </summary>
        public KeyboardControlScreen()
        {
            this.title = Resources.KeyboardControl;

            this.message = "Move: W (up), A (left), S (down), D (right)\n" + "Run: Left shift\n" + "Shoot: Left mouse button\n" + "Aim/look: Mouse movement\n"
                + "Zoom: Right mouse button\n" + "Jump: Space\n" + "Pause: Escape (Esc)";

            IsPopup = true;

            TransitionOnTime = TimeSpan.FromSeconds(0.2);
            TransitionOffTime = TimeSpan.FromSeconds(0.2);
        }

        #endregion

        #region Initialization

        /// <summary>
        /// Loads graphics content for this screen. This uses the shared ContentManager
        /// provided by the Game class, so the content will remain loaded forever.
        /// Whenever a subsequent MessageBoxScreen tries to load this same content,
        /// it will just get back another reference to the already loaded data.
        /// </summary>
        public override void LoadContent()
        {
            ContentManager content = ScreenManager.Game.Content;

            gradientTexture = content.Load<Texture2D>("Menus/gradient");
        }

        #endregion

        #region Handle Input


        /// <summary>
        /// Responds to user input, accepting or cancelling the message box.
        /// </summary>
        public override void HandleInput(InputState input)
        {
            PlayerIndex playerIndex;

            // We pass in our ControllingPlayer, which may either be null (to
            // accept input from any player) or a specific index. If we pass a null
            // controlling player, the InputState helper returns to us which player
            // actually provided the input. We pass that through to our Cancelled events, 
            // so they can tell which player triggered them.
            if (input.IsMenuCancel(ControllingPlayer, out playerIndex))
            {
                // Raise the cancelled event, then exit the message box.
                if (Cancelled != null)
                    Cancelled(this, new PlayerIndexEventArgs(playerIndex));

                ExitScreen();
            }
        }

        #endregion

        #region Draw

        /// <summary>
        /// Draws the message box.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            SpriteFont font = ScreenManager.Font;

            // Darken down any other screens that were drawn beneath the popup.
            ScreenManager.FadeBackBufferToBlack(TransitionAlpha / 3);

            // Center the message text in the viewport.
            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
            int safeAreaTop = viewport.TitleSafeArea.Top;
            int safeAreaBottom = viewport.TitleSafeArea.Bottom;

            // The background includes a border somewhat larger than the text itself.
            Rectangle backgroundRectangle = new Rectangle(viewport.X + 200, safeAreaTop,
                viewport.Width - 400, safeAreaBottom);

            // Fade the popup alpha during transitions.
            Color color = Color.White * TransitionAlpha;

            spriteBatch.Begin();

            // Draw the background rectangle.
            spriteBatch.Draw(gradientTexture, backgroundRectangle, color);

            // Draw the title
            spriteBatch.DrawString(font, title, new Vector2(220, safeAreaTop + 10), Color.White, 0, new Vector2(0, 0), 1.25f, SpriteEffects.None, 0);

            // Draw the text
            spriteBatch.DrawString(font, message, new Vector2(220, safeAreaTop + 65), Color.White, 0, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0);

#if (!XBOX360)

            // Draw the exit screen text
            spriteBatch.DrawString(font, "Press Escape (Esc) to exit from this screen", new Vector2(220, safeAreaBottom - 40), Color.White, 0, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0);
#endif
#if XBOX360
            // Draw the exit screen text
            spriteBatch.DrawString(font, "Press button B to exit from this screen", new Vector2(220, safeAreaBottom - 40), Color.White, 0, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0);
#endif

            spriteBatch.End();
        }


        #endregion
    }
}
