using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.GamerServices;
using StrategicCombat.MenuScreens;

namespace StrategicCombat
{
    /// <summary>
    /// This menu screen gives the user information about the game and
    /// about the controls in the game.
    /// </summary>
    class OptionScreen : MenuScreen
    {
        #region Variables

        MenuEntry musicOnOffMenuEntry;

        #endregion

        #region Constructors

        /// <summary>
        /// Create a new option screen.
        /// </summary>
        public OptionScreen()
            : base(Resources.Options)
        {

            // Create our menu entries.
            musicOnOffMenuEntry = new MenuEntry("Unknown");
            MenuEntry gamePadControlMenuEntry = new MenuEntry(Resources.GamePadControl);
            MenuEntry aboutTheGameMenuEntry = new MenuEntry(Resources.AboutTheGame);
            MenuEntry creditsMenuEntry = new MenuEntry(Resources.Credits);
            MenuEntry backMenuEntry = new MenuEntry(Resources.Back);

            // Hook up menu event handlers.
            musicOnOffMenuEntry.Selected += MusicOnOffMenuEntrySelected;
            gamePadControlMenuEntry.Selected += GamePadControlMenuEntrySelected;
            aboutTheGameMenuEntry.Selected += AboutTheGameMenuEntrySelected;
            creditsMenuEntry.Selected += CreditsMenuEntrySelected;
            backMenuEntry.Selected += OnCancel;

            // Add entries to the menu.
            MenuEntries.Add(musicOnOffMenuEntry);
            MenuEntries.Add(gamePadControlMenuEntry);
            MenuEntries.Add(aboutTheGameMenuEntry);
            MenuEntries.Add(creditsMenuEntry);
            MenuEntries.Add(backMenuEntry);

            // Set the menu entry text for menu music.
            SetMenuEntryText();
        }

        #endregion

        #region Methods

        /// <summary>
        /// Fills in the latest values for the menu text.
        /// </summary>
        private void SetMenuEntryText()
        {
            String onOff = Program.menuMusicOff ? "[ off ]" : "[ on ]";
            musicOnOffMenuEntry.Text = "Menu music " + onOff;
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Event handler for when the Music on/off menu entry is selected.
        /// </summary>
        private void MusicOnOffMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            // Change the menu music to on or off
            if (Program.menuMusicOff == false)
            {
                // Set the menu music variable
                Program.menuMusicOff = true;

                // Pause the menu music if it is playing
                if (Program.menuMusic != null && Program.menuMusic.IsPlaying)
                    Program.menuMusic.Pause();

            }
            else if (Program.menuMusicOff == true)
            {
                // Set the menu music variable.
                Program.menuMusicOff = false;

                // Resume the menu music if it not is playing already.
                if (Program.menuMusic != null && Program.menuMusic.IsPaused)
                    Program.menuMusic.Resume();
            }

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

        /// <summary>
        /// Event handler for when the Game pad control menu entry is selected.
        /// </summary>
        private void GamePadControlMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            // Add a game pad control screen.
            ScreenManager.AddScreen(new GamePadControlScreen(), e.PlayerIndex);
        }

        /// <summary>
        /// Event handler for when the How to play this game menu entry is selected.
        /// </summary>
        private void AboutTheGameMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            // Add a about the game screen.
            ScreenManager.AddScreen(new HowToPlayScreen(), e.PlayerIndex);
        }

        /// <summary>
        /// Event handler for when the Credits menu entry is selected.
        /// </summary>
        private void CreditsMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            // Add a about the game screen.
            ScreenManager.AddScreen(new CreditsScreen(), e.PlayerIndex);
        }

        #endregion

    } // End of class

} // End of namespace
