using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Net;

namespace StrategicCombat.GameScreens
{
    /// <summary>
    /// This class represent a end game screen.
    /// </summary>
    class EndGameScreen : MenuScreen
    {

        #region Variables

        private NetworkSession networkSession;
        private String winningPlayer;

        #endregion

        #region Constructors

        /// <summary>
        /// Create a new endgame screen
        /// </summary>
        /// <param name="networkSession"></param>
        /// <param name="winningPlayer"></param>
        public EndGameScreen(NetworkSession networkSession, String winningPlayer)
            : base(Resources.EndOfGame)
        {
            // Set the network session and the name for the winning player.
            this.networkSession = networkSession;
            this.winningPlayer = winningPlayer;

            // Add menu entries
            if (networkSession == null)
            {
                // If this is a single player game, add the Quit menu entry.
                MenuEntry quitGameMenuEntry = new MenuEntry(Resources.QuitGame);
                quitGameMenuEntry.Selected += QuitGameMenuEntrySelected;
                MenuEntries.Add(quitGameMenuEntry);
            }
            else
            {
                // If we are hosting a network game, add the Return to Lobby menu entry.
                if (networkSession.IsHost)
                {
                    MenuEntry lobbyMenuEntry = new MenuEntry(Resources.ReturnToLobby);
                    lobbyMenuEntry.Selected += ReturnToLobbyMenuEntrySelected;
                    MenuEntries.Add(lobbyMenuEntry);
                }

                // Add the End/Leave Session menu entry.
                string leaveEntryText = networkSession.IsHost ? Resources.EndSession :
                                                                Resources.LeaveSession;

                MenuEntry leaveSessionMenuEntry = new MenuEntry(leaveEntryText);
                leaveSessionMenuEntry.Selected += LeaveSessionMenuEntrySelected;
                MenuEntries.Add(leaveSessionMenuEntry);
            }

            // Play a victory sound.
            Program.soundBank.PlayCue("victory");
        }

        #endregion

        #region Handle Input

        /// <summary>
        /// Handler for when the user has cancelled the menu.
        /// </summary>
        protected override void OnCancel(PlayerIndex playerIndex)
        {
            // Do nothing to avoid Code 4 error.
            //ExitScreen();
        }

        #endregion

        #region Methods

        /// <summary>
        /// Draw on the end game screen.
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Draw(GameTime gameTime)
        {
            // Call the draw method in the base class.
            base.Draw(gameTime);

            // Create reference variables.
            GraphicsDevice graphicsDevice = ScreenManager.GraphicsDevice;
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            SpriteFont font = ScreenManager.Font;

            // Start draw in 2D
            spriteBatch.Begin();

            // Properties for text on the end game screen
            String winningPlayerString = this.winningPlayer + " won the game!";
            Color textColor = Color.White * TransitionAlpha;

            // Draw the winning team.
            spriteBatch.DrawString(font, winningPlayerString, new Vector2(130, 72 + 12), Color.DarkGray * TransitionAlpha, 0, Vector2.Zero, 2f, SpriteEffects.None, 0);
            spriteBatch.DrawString(font, winningPlayerString, new Vector2(128, 72 + 10), textColor, 0, Vector2.Zero, 2f, SpriteEffects.None, 0);

            // End drawing in 2D
            spriteBatch.End();
            
        } // End of the draw method.

        #endregion

        #region EventHandlers

        /// <summary>
        /// Event handler for when the Quit Game menu entry is selected.
        /// </summary>
        private void QuitGameMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            MessageBoxScreen confirmQuitMessageBox =
                                    new MessageBoxScreen(Resources.ConfirmQuitGame);

            confirmQuitMessageBox.Accepted += ConfirmQuitMessageBoxAccepted;

            ScreenManager.AddScreen(confirmQuitMessageBox, ControllingPlayer);
        }

        /// <summary>
        /// Event handler for when the user selects ok on the "are you sure
        /// you want to quit" message box. This uses the loading screen to
        /// transition from the game back to the main menu screen.
        /// </summary>
        private void ConfirmQuitMessageBoxAccepted(object sender, PlayerIndexEventArgs e)
        {
            LoadingScreen.Load(ScreenManager, false, null, new BackgroundScreen(),
                                                           new MainMenuScreen());
        }

        /// <summary>
        /// Event handler for when the Return to Lobby menu entry is selected.
        /// </summary>
        private void ReturnToLobbyMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            if (networkSession.SessionState == NetworkSessionState.Playing)
            {
                networkSession.EndGame();
            }
        }

        /// <summary>
        /// Event handler for when the End/Leave Session menu entry is selected.
        /// </summary>
        private void LeaveSessionMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            NetworkSessionComponent.LeaveSession(ScreenManager, e.PlayerIndex);
        }

        #endregion

    } // End of class

} // End of namespace
