using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace FredriksdalAssault2
{
    /// <summary>
    /// The settings screen is brought up over the top of the lobby
    /// screen, and gives the host a chance to configure the network game.
    /// </summary>
    class NetworkSettingsScreen : MenuScreen
    {
        #region Variables

        MenuEntry gameModeMenuEntry;
        MenuEntry mapMenuEntry;
        MenuEntry gameTimeMenuEntry;
        MenuEntry maxKillsMenuEntry;
        MenuEntry saveAndExit;

        static string[] gameModes = {"Assault", "Deathmatch"};
        static int currentGameMode = 0;

        static string[] maps = { "Valley", "Hills", "Fortrest" };
        static int currentMap = 0;

        static int currentGameTime = 5; // Minutes
        static int currentMaxKills = 15;

        #endregion

        #region Constructors

        public NetworkSettingsScreen()
            : base(Resources.Settings)
        {
            // Get the current game mode, game time and number of max kills.
            currentGameMode = Program.gameMode;
            currentMap = Program.currentMap;
            currentGameTime = (Program.totalGameTimeMilliseconds / 60000);
            currentMaxKills = Program.maxKills;

            // Create our menu entries.
            gameModeMenuEntry = new MenuEntry("Game mode: " + gameModes[currentGameMode]);
            mapMenuEntry = new MenuEntry("Map: " + maps[currentMap]);
            gameTimeMenuEntry = new MenuEntry("Game time: " + currentGameTime + " minutes");
            maxKillsMenuEntry = new MenuEntry("Max kills: " + currentMaxKills);
            saveAndExit = new MenuEntry("Save and exit");

            // Hook up menu event handlers.
            gameModeMenuEntry.Selected += GameModeMenuEntrySelected;
            mapMenuEntry.Selected += MapMenuEntrySelected;
            gameTimeMenuEntry.Selected += GameTimeMenuEntrySelected;
            maxKillsMenuEntry.Selected += MaxKillsMenuEntrySelected;
            saveAndExit.Selected += SaveAndExitMenuEntrySelected;

            // Add entries to the menu.
            MenuEntries.Add(mapMenuEntry);
            MenuEntries.Add(gameModeMenuEntry);
            MenuEntries.Add(gameTimeMenuEntry);
            if(currentGameMode == 1)
                MenuEntries.Add(maxKillsMenuEntry);
            MenuEntries.Add(saveAndExit);

        }

        #endregion

        #region Methods

        /// <summary>
        /// Fills in the latest values for the menu text.
        /// </summary>
        void SetMenuEntryText()
        {
            gameModeMenuEntry.Text = "Game mode: " + gameModes[currentGameMode];
            mapMenuEntry.Text = "Map: " + maps[currentMap];
            gameTimeMenuEntry.Text = "Game time: " + currentGameTime + " minutes";
            maxKillsMenuEntry.Text = "Max kills: " + currentMaxKills;
            
        }

        #endregion

        #region Event handlers

        /// <summary>
        /// Event handler for when the Game mode menu entry is selected.
        /// </summary>
        void GameModeMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            // Calculate the current number.
            currentGameMode = (currentGameMode + 1) % gameModes.Length;

            if (currentGameMode == 1)
            {
                MenuEntries.Remove(saveAndExit);
                MenuEntries.Add(maxKillsMenuEntry);
                MenuEntries.Add(saveAndExit);
            }
            else
            {
                MenuEntries.Remove(maxKillsMenuEntry);
            }
                
            // Set the menu text.
            SetMenuEntryText();
        }

        /// <summary>
        /// Event handler for when the Map menu entry is selected.
        /// </summary>
        void MapMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            // Calculate the current number.
            currentMap = (currentMap + 1) % maps.Length;

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

        /// <summary>
        /// Event handler for when the Game time menu entry is selected.
        /// </summary>
        void GameTimeMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            // Calculate the current game time.
            if (currentGameTime >= 60)
            {
                currentGameTime = 1;
            }
            else if (currentGameTime < 10)
            {
                currentGameTime++;
            }
            else if (currentGameTime >= 10)
            {
                currentGameTime += 5;
            }

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

        /// <summary>
        /// Event handler for when the Max kills menu entry is selected.
        /// </summary>
        void MaxKillsMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            // Calculate the current number of max kills.
            if (currentMaxKills >= 60)
            {
                currentMaxKills = 1;
            }
            else if (currentMaxKills < 10)
            {
                currentMaxKills++;
            }
            else if (currentMaxKills >= 10)
            {
                currentMaxKills += 5;
            }

            // 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 the game mode in the program class.
            Program.gameMode = (byte)currentGameMode;

            // Set the map in the program class
            Program.currentMap = (byte)currentMap;

            // Set the game time in the program class.
            Program.totalGameTimeMilliseconds = currentGameTime * 60000;

            // Set the number of max kills in the program class.
            Program.maxKills = currentMaxKills;

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

        #endregion

    } // End of class

} // End of namespace
