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 class HowToPlayScreen : InformationScreen
    {
        #region Variables

        int numberOfPages = 3;
        int currentPage = 0;

        // Power up textures
        public Texture2D health100PowerUpTexture;
        public Texture2D damagePowerUpTexture;
        public Texture2D shotPowerUpTexture;
        public Texture2D shotDelayPowerUpTexture;
        public Texture2D health200PowerUpTexture;

        // Power up strings
        String damagePowerUpString = "You will inflict double damage to other combat units during 10 seconds.";
        String shotPowerUpString = "Your shots will travel double as fast during 10 seconds.";
        String shotDelayPowerUpString = "Your reload time will take half of the ordinary time during 10 seconds.";
        String health100PowerUpString = "You will gain 100 points in health if you do not not have full health already.";
        String health200PowerUpString = "You will gain 200 points in health if you do not not have full health already.";

        // Blue team textures (Left team)
        Texture2D leftInfantery;
        Texture2D leftBouncer;
        Texture2D leftTank;
        Texture2D leftPlane;
        Texture2D leftFlameThrower;
        Texture2D leftPresident;

        // Red team textures (Right team)
        Texture2D rightInfantery;
        Texture2D rightDoubleShooter;
        Texture2D rightTank;
        Texture2D rightBomberPlane;
        Texture2D rightBazookaShooter;
        Texture2D rightPresident;

        // Team strings
        String infanteryString = "Infantery soldier, used to wear out other combat units.";
        String bazookaString = "Bazooka shooter, moves slowly and have a long reload time.";
        String doubleShooterString = "Double shooter, fast shooter with large bullets.";
        String tankString = "Tank, moves slowly but have bullets that take large damage.";
        String shooterPlane = "Shooter plane, fires 3 shots at a time but can only move forward.";
        String bomberPlane = "Bomber plane, drops bombs under the plane but need 3 seconds to reload.";
        String flameThrower = "Flamethrower, run fast and can flame through walls.";
        String presidentString = "President, a fast rotating sniper with bullets that take large damage.";

        // Button textures
        Texture2D xKeyTexture;
        Texture2D xButtonTexture;

        #endregion

        #region Constructors

        /// <summary>
        /// Create a new how to play this game screen.
        /// </summary>
        public HowToPlayScreen()
            : base()
        {
            // Set the title
            this.title = Resources.AboutTheGame;

            // Set the message.
            this.message = "Strategic Combat is a game in which two different teams, USA and Russia, fights against each other. " + Environment.NewLine +
                           "The game starts on a board where each team can move a combat unit in a turn, the turn changes to  " + Environment.NewLine +
                           "the other team when a move has been made. A combat will be initiated when two combat units from  " + Environment.NewLine +
                           "different teams are on the same tile, the combat ends when one combat unit is dead. The goal of " + Environment.NewLine +
                           "the game is to eliminate all of the combat units in the other team. " + Environment.NewLine + Environment.NewLine;
         
        }

        /// <summary>
        /// Loads specific graphics content used by the screen.
        /// </summary>
        public override void LoadSpecificContent()
        {
            // Create a new content manager.
            ContentManager content = ScreenManager.Game.Content;

            // Load power up textures
            this.damagePowerUpTexture = content.Load<Texture2D>("PowerUps/damageX2");
            this.health100PowerUpTexture = content.Load<Texture2D>("PowerUps/health100");
            this.shotPowerUpTexture = content.Load<Texture2D>("PowerUps/shotSpeedX2");
            this.shotDelayPowerUpTexture = content.Load<Texture2D>("PowerUps/shotDelay50");
            this.health200PowerUpTexture = content.Load<Texture2D>("PowerUps/health200");

            // Load team member textures
            this.leftInfantery = content.Load<Texture2D>("CombatUnits/LeftCombatUnits/LeftInfanterySoldier50");
            this.leftBouncer = content.Load<Texture2D>("CombatUnits/LeftCombatUnits/LeftBouncer50");
            this.leftTank = content.Load<Texture2D>("CombatUnits/LeftCombatUnits/LeftTank50");
            this.leftPlane = content.Load<Texture2D>("CombatUnits/LeftCombatUnits/LeftPlane50");
            this.leftFlameThrower = content.Load<Texture2D>("CombatUnits/LeftCombatUnits/LeftFlameThrower50");
            this.leftPresident = content.Load<Texture2D>("CombatUnits/LeftCombatUnits/LeftPresident50");
            this.rightInfantery = content.Load<Texture2D>("CombatUnits/RightCombatUnits/RightInfanterySoldier50");
            this.rightDoubleShooter = content.Load<Texture2D>("CombatUnits/RightCombatUnits/RightDoubleShooter50");
            this.rightTank = content.Load<Texture2D>("CombatUnits/RightCombatUnits/RightTank50");
            this.rightBomberPlane = content.Load<Texture2D>("CombatUnits/RightCombatUnits/RightPlane50");
            this.rightBazookaShooter = content.Load<Texture2D>("CombatUnits/RightCombatUnits/RightBazooka50");
            this.rightPresident = content.Load<Texture2D>("CombatUnits/RightCombatUnits/RightPresident50");

            // Load button textures
            this.xKeyTexture = content.Load<Texture2D>("MenuAssets/key_x");
            this.xButtonTexture = content.Load<Texture2D>("MenuAssets/x");

        }

        #endregion

        #region Methods

        /// <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.IsMenuJoinGame(playerIndex, out unwantedOutput))
                {
                    HandleMenuChangePage();
                }
                if (input.IsMenuCancel(playerIndex, out unwantedOutput))
                {
                    HandleMenuCancel();
                }
            }

        } // End of the HandleInput method.

        /// <summary>
        /// Handle MenuChangePage inputs by changing the page.
        /// </summary>
        private void HandleMenuChangePage()
        {
            // Change the page
            this.currentPage = ((this.currentPage + 1) % this.numberOfPages);
        }

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

        /// <summary>
        /// Draw specific information on this screen.
        /// </summary>
        /// <param name="spriteBatch"></param>
        /// <param name="font"></param>
        public override void DrawSpecificInformation(SpriteBatch spriteBatch, SpriteFont font)
        {
            // Variables used to draw.
            Color shadowColor = Color.DarkGray * TransitionAlpha;
            Color foreColor = Color.White * TransitionAlpha;
            float scale = 1f;

            // Draw the first page.
            if (this.currentPage == 0)
            {
                // Draw the message.
                spriteBatch.DrawString(font, this.message, new Vector2(xOffset + 6, yOffset + 101), Color.DarkGray * TransitionAlpha, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
                spriteBatch.DrawString(font, this.message, new Vector2(xOffset + 5, yOffset + 100), Color.White * TransitionAlpha, 0, Vector2.Zero, 1, SpriteEffects.None, 0);

                // Draw damage power up
                spriteBatch.Draw(this.damagePowerUpTexture, new Vector2(xOffset + 10, yOffset + 220), foreColor);
                spriteBatch.DrawString(font, damagePowerUpString, new Vector2(xOffset + 51, yOffset + 228), shadowColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                spriteBatch.DrawString(font, damagePowerUpString, new Vector2(xOffset + 50, yOffset + 227), foreColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);

                // Draw shot speed power up
                spriteBatch.Draw(this.shotPowerUpTexture, new Vector2(xOffset + 10, yOffset + 265), foreColor);
                spriteBatch.DrawString(font, this.shotPowerUpString, new Vector2(xOffset + 51, yOffset + 273), shadowColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                spriteBatch.DrawString(font, this.shotPowerUpString, new Vector2(xOffset + 50, yOffset + 272), foreColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);

                // Draw shot delay power up
                spriteBatch.Draw(this.shotDelayPowerUpTexture, new Vector2(xOffset + 10, yOffset + 310), foreColor);
                spriteBatch.DrawString(font, this.shotDelayPowerUpString, new Vector2(xOffset + 51, yOffset + 318), shadowColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                spriteBatch.DrawString(font, this.shotDelayPowerUpString, new Vector2(xOffset + 50, yOffset + 317), foreColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);

                // Draw health 100 power up
                spriteBatch.Draw(this.health100PowerUpTexture, new Vector2(xOffset + 10, yOffset + 355), foreColor);
                spriteBatch.DrawString(font, this.health100PowerUpString, new Vector2(xOffset + 51, yOffset + 363), shadowColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                spriteBatch.DrawString(font, this.health100PowerUpString, new Vector2(xOffset + 50, yOffset + 362), foreColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);

                // Draw health 200 power up
                spriteBatch.Draw(this.health200PowerUpTexture, new Vector2(xOffset + 10, yOffset + 400), foreColor);
                spriteBatch.DrawString(font, this.health200PowerUpString, new Vector2(xOffset + 51, yOffset + 408), shadowColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                spriteBatch.DrawString(font, this.health200PowerUpString, new Vector2(xOffset + 50, yOffset + 407), foreColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
            
            }

            // Draw the second page.
            if (this.currentPage == 1)
            {
                // Draw the title.
                spriteBatch.DrawString(font, "USA", new Vector2(xOffset + 6, yOffset + 101), Color.DarkGray * TransitionAlpha, 0, Vector2.Zero, 1.25f, SpriteEffects.None, 0);
                spriteBatch.DrawString(font, "USA", new Vector2(xOffset + 5, yOffset + 100), Color.White * TransitionAlpha, 0, Vector2.Zero, 1.25f, SpriteEffects.None, 0);

                // Draw a blue infantery soldier.
                spriteBatch.Draw(this.leftInfantery, new Vector2(xOffset + 10, yOffset + 140), foreColor);
                spriteBatch.DrawString(font, this.infanteryString, new Vector2(xOffset + 71, yOffset + 156), shadowColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                spriteBatch.DrawString(font, this.infanteryString, new Vector2(xOffset + 70, yOffset + 155), foreColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);

                // Draw a blue bazooka shooter.
                spriteBatch.Draw(this.leftBouncer, new Vector2(xOffset + 10, yOffset + 200), foreColor);
                spriteBatch.DrawString(font, this.bazookaString, new Vector2(xOffset + 71, yOffset + 216), shadowColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                spriteBatch.DrawString(font, this.bazookaString, new Vector2(xOffset + 70, yOffset + 215), foreColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);

                // Draw a blue tank.
                spriteBatch.Draw(this.leftTank, new Vector2(xOffset + 10, yOffset + 260), foreColor);
                spriteBatch.DrawString(font, this.tankString, new Vector2(xOffset + 71, yOffset + 276), shadowColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                spriteBatch.DrawString(font, this.tankString, new Vector2(xOffset + 70, yOffset + 275), foreColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);

                // Draw a blue plane.
                spriteBatch.Draw(this.leftPlane, new Vector2(xOffset + 10, yOffset + 320), foreColor);
                spriteBatch.DrawString(font, this.shooterPlane, new Vector2(xOffset + 71, yOffset + 336), shadowColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                spriteBatch.DrawString(font, this.shooterPlane, new Vector2(xOffset + 70, yOffset + 335), foreColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);

                // Draw a blue flame thrower.
                spriteBatch.Draw(this.leftFlameThrower, new Vector2(xOffset + 10, yOffset + 380), foreColor);
                spriteBatch.DrawString(font, this.flameThrower, new Vector2(xOffset + 71, yOffset + 396), shadowColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                spriteBatch.DrawString(font, this.flameThrower, new Vector2(xOffset + 70, yOffset + 395), foreColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);

                // Draw a blue president.
                spriteBatch.Draw(this.leftPresident, new Vector2(xOffset + 10, yOffset + 440), foreColor);
                spriteBatch.DrawString(font, this.presidentString, new Vector2(xOffset + 71, yOffset + 456), shadowColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                spriteBatch.DrawString(font, this.presidentString, new Vector2(xOffset + 70, yOffset + 455), foreColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
            
            }

            // Draw the third page.
            if (this.currentPage == 2)
            {
                // Draw the title.
                spriteBatch.DrawString(font, "Russia", new Vector2(xOffset + 6, yOffset + 101), Color.DarkGray * TransitionAlpha, 0, Vector2.Zero, 1.25f, SpriteEffects.None, 0);
                spriteBatch.DrawString(font, "Russia", new Vector2(xOffset + 5, yOffset + 100), Color.White * TransitionAlpha, 0, Vector2.Zero, 1.25f, SpriteEffects.None, 0);

                // Draw a red infantery soldier.
                spriteBatch.Draw(this.rightInfantery, new Vector2(xOffset + 10, yOffset + 140), foreColor);
                spriteBatch.DrawString(font, this.infanteryString, new Vector2(xOffset + 71, yOffset + 156), shadowColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                spriteBatch.DrawString(font, this.infanteryString, new Vector2(xOffset + 70, yOffset + 155), foreColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);

                // Draw a red double shooter.
                spriteBatch.Draw(this.rightDoubleShooter, new Vector2(xOffset + 10, yOffset + 200), foreColor);
                spriteBatch.DrawString(font, this.doubleShooterString, new Vector2(xOffset + 71, yOffset + 216), shadowColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                spriteBatch.DrawString(font, this.doubleShooterString, new Vector2(xOffset + 70, yOffset + 215), foreColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);

                // Draw a red tank.
                spriteBatch.Draw(this.rightTank, new Vector2(xOffset + 10, yOffset + 260), foreColor);
                spriteBatch.DrawString(font, this.tankString, new Vector2(xOffset + 71, yOffset + 276), shadowColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                spriteBatch.DrawString(font, this.tankString, new Vector2(xOffset + 70, yOffset + 275), foreColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);

                // Draw a red plane.
                spriteBatch.Draw(this.rightBomberPlane, new Vector2(xOffset + 10, yOffset + 320), foreColor);
                spriteBatch.DrawString(font, this.bomberPlane, new Vector2(xOffset + 71, yOffset + 336), shadowColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                spriteBatch.DrawString(font, this.bomberPlane, new Vector2(xOffset + 70, yOffset + 335), foreColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);

                // Draw a red bazooka shooter.
                spriteBatch.Draw(this.rightBazookaShooter, new Vector2(xOffset + 10, yOffset + 380), foreColor);
                spriteBatch.DrawString(font, this.bazookaString, new Vector2(xOffset + 71, yOffset + 396), shadowColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                spriteBatch.DrawString(font, this.bazookaString, new Vector2(xOffset + 70, yOffset + 395), foreColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);

                // Draw a red president.
                spriteBatch.Draw(this.rightPresident, new Vector2(xOffset + 10, yOffset + 440), foreColor);
                spriteBatch.DrawString(font, this.presidentString, new Vector2(xOffset + 71, yOffset + 456), shadowColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                spriteBatch.DrawString(font, this.presidentString, new Vector2(xOffset + 70, yOffset + 455), foreColor, 0, Vector2.Zero, scale, SpriteEffects.None, 0);

            }

            // Button panel
            float buttonTextScale = 1.25f;
            String exitFromThisScreenText = "Exit from this screen";
            String changePageText = "Change page";
            String pageInformationText = (this.currentPage + 1) + "(" + this.numberOfPages + ")";
            float exitFromScreenStringSize = xOffset + 32 + 40 + (font.MeasureString(exitFromThisScreenText).X * buttonTextScale) + 5;
            float pageStringSize = (font.MeasureString(pageInformationText).X * buttonTextScale);

#if (!XBOX360)

            // Draw change page button with text (x button)
            spriteBatch.Draw(xKeyTexture, new Vector2(exitFromScreenStringSize, 720 - yOffset - 42), Color.White * TransitionAlpha);

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

#endif
#if (XBOX360)

            // Draw change page button with text (x button)
            spriteBatch.Draw(xButtonTexture, new Vector2(exitFromScreenStringSize, 720 - yOffset - 42), Color.White * TransitionAlpha);

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

            // Draw page information
            spriteBatch.DrawString(font, pageInformationText, new Vector2(1152 - pageStringSize + 1, 720 - yOffset - 40), Color.DarkGray * TransitionAlpha, 0,
                                   Vector2.Zero, buttonTextScale, SpriteEffects.None, 0);
            spriteBatch.DrawString(font, pageInformationText, new Vector2(1152 - pageStringSize, 720 - yOffset - 41), Color.White * TransitionAlpha, 0,
                                   Vector2.Zero, buttonTextScale, SpriteEffects.None, 0);


        } // End of the DrawSpecificInformation method.

        #endregion

    } // End of class

} // End of namespace
