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

namespace FredriksdalAssault2
{
    class ControllerSettingsScreen : MenuScreen
    {

        #region Variables

        byte playerIndex;

        MenuEntry playerIndexMenuEntry;
        MenuEntry invertedXaxisMenuEntry;
        MenuEntry invertedYaxisMenuEntry;

        Texture2D gradientTexture;

        static bool[] currentXaxisValue;
        static bool[] currentYaxisValue;

        #endregion

        #region Constructors

        /// <summary>
        /// Create a new controller settings screen.
        /// </summary>
        public ControllerSettingsScreen(PlayerIndex playerIndex)
            : base("")
        {
            // Set the playerindex variable.
            this.playerIndex = (byte)playerIndex;

            // Get the current values for inverted x-axis and y-axis.
            currentXaxisValue = Program.invertedAxisX;
            currentYaxisValue = Program.invertedAxisY;

            // Create our menu entries.
            playerIndexMenuEntry = new MenuEntry("Playerindex: " + (this.playerIndex + 1));
            invertedXaxisMenuEntry = new MenuEntry("Inverted X-axis: " + currentXaxisValue[this.playerIndex]);
            invertedYaxisMenuEntry = new MenuEntry("Inverted Y-axis: " + currentYaxisValue[this.playerIndex]);
            MenuEntry saveAndExit = new MenuEntry("Save and exit");

            // Hook up menu event handlers.
            playerIndexMenuEntry.Selected += PlayerIndexMenuEntrySelected;
            invertedXaxisMenuEntry.Selected += invertedXaxisMenuEntrySelected;
            invertedYaxisMenuEntry.Selected += invertedYaxisMenuEntrySelected;
            saveAndExit.Selected += SaveAndExitMenuEntrySelected;

            // Add entries to the menu.
            MenuEntries.Add(playerIndexMenuEntry);
            MenuEntries.Add(invertedXaxisMenuEntry);
            MenuEntries.Add(invertedYaxisMenuEntry);
            MenuEntries.Add(saveAndExit);

        }

        #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 Methods

        /// <summary>
        /// Fills in the latest values for the menu text.
        /// </summary>
        void SetMenuEntryText()
        {
            playerIndexMenuEntry.Text = "Playerindex: " + (this.playerIndex + 1);
            invertedXaxisMenuEntry.Text = "Inverted X-axis: " + currentXaxisValue[this.playerIndex];
            invertedYaxisMenuEntry.Text = "Inverted Y-axis: " + currentYaxisValue[this.playerIndex];
            
        }

        /// <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);

            // Make the menu slide into place during transitions, using a
            // power curve to make things look more interesting (this makes
            // the movement slow down as it nears the end).
            //float transitionOffset = (float)Math.Pow(TransitionPosition, 2);

            Vector2 titlePosition = new Vector2(viewport.Width / 2, safeAreaTop + 10);
            Vector2 titleOrigin = new Vector2(font.MeasureString(Resources.ControllerSettings).X / 2, 0);

            //titlePosition.Y -= transitionOffset * 100;

            // Draw the title
            spriteBatch.DrawString(font, Resources.ControllerSettings, titlePosition, Color.White * TransitionAlpha, 0, 
                titleOrigin, 1.25f, SpriteEffects.None, 0);

#if (!XBOX360)

            // Draw the exit screen text
            spriteBatch.DrawString(font, "Press Escape (Esc) to exit without saving", 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 without saving", new Vector2(220, safeAreaBottom - 40), Color.White, 0, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0);
#endif

            spriteBatch.End();

            base.Draw(gameTime);
        }

        #endregion

        #region Event handlers

        /// <summary>
        /// Event handler for when the playerIndex menu entry is selected.
        /// </summary>
        void PlayerIndexMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            // Set the value to true or false.
            if (this.playerIndex >= 3)
                this.playerIndex = 0;
            else
                this.playerIndex++;

            // Set the menu text.
            SetMenuEntryText();
        }

        /// <summary>
        /// Event handler for when the Inverted X-axis menu entry is selected.
        /// </summary>
        void invertedXaxisMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            // Set the value to true or false.
            if(currentXaxisValue[this.playerIndex] == true)
                currentXaxisValue[this.playerIndex] = false;
            else
                currentXaxisValue[this.playerIndex] = true;

            // Set the menu text.
            SetMenuEntryText();
        }

        /// <summary>
        /// Event handler for when the Inverted X-axis menu entry is selected.
        /// </summary>
        void invertedYaxisMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            // Set the value to true or false.
            if (currentYaxisValue[this.playerIndex] == true)
                currentYaxisValue[this.playerIndex] = false;
            else
                currentYaxisValue[this.playerIndex] = true;

            // Set the menu text.
            SetMenuEntryText();
        }

        /// <summary>
        /// Event handler for when the Save and exit menu entry is selected.
        /// </summary>
        void SaveAndExitMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            // Set inverted x and y values.
            Program.invertedAxisX = currentXaxisValue;
            Program.invertedAxisY = currentYaxisValue;

            // Exit from the menu.
            OnCancel(e.PlayerIndex);
        }

        #endregion
    }
}
