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.Graphics;

namespace FredriksdalAssault2
{
    /// <summary>
    /// The spawn menu comes up over the top of the game,
    /// giving the player options to respawn or quit. Should not accept input.
    /// </summary>
    public class SpawnScreen : GameScreen
    {
        #region Instance variables

        NetworkSession networkSession;
        public int spawnCountDown;
        public String killedByName = "Unknown";

        Camera myGameCamera;

        #endregion

        #region Constructors

        /// <summary>
        /// Create a new spawn screen.
        /// </summary>
        /// <param name="networkSession"></param>
        public SpawnScreen(NetworkSession networkSession, Camera camera)
        {
            // Set the camera reference
            this.myGameCamera = camera;

            // Set the network session and the count down time.
            this.networkSession = networkSession;
            this.spawnCountDown = 10000;
           
        }

        #endregion

        #region Methods

        /// <summary>
        /// Update the spawn screen.
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime)
        {
            this.spawnCountDown -= gameTime.ElapsedGameTime.Milliseconds;

            if(this.spawnCountDown < 3000)
            {
                this.myGameCamera.cameraPosition += new Vector3(0, 100, 0);

                // Make the player go up to heaven, so that the server computer can´t kill him when he is alive.
                this.myGameCamera.playerModel.UpdateSoldierPositionRotation(this.myGameCamera.cameraPosition,
                    0, this.myGameCamera.cameraDirection);

            }

        } // End of the Update method

        /// <summary>
        /// Draw the spawn screen.
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Draw(GameTime gameTime)
        {

            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            SpriteFont font = ScreenManager.Font;
            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;

            // Draw the name of the gamer that you were killed by.
            spriteBatch.Begin();

            // Make the text 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);

            // Killed by string
            String killedByString = "You were killed by " + this.killedByName;

            // Draw the killed by name centered on the screen
            Vector2 textPosition = new Vector2(viewport.Width / 2, viewport.Height / 2);
            Vector2 textOrigin = font.MeasureString(killedByString) / 2;
            Color textColor = new Color(200, 0, 0) * TransitionAlpha;
            float textScale = 1.00f;

            // Time string
            String timeString = "Respawn in " + (this.spawnCountDown / 1000) + " seconds";

            textPosition.Y -= transitionOffset * 100;

            // Draw the killed by name
            spriteBatch.DrawString(font, killedByString, textPosition, textColor, 0,
                                   textOrigin, textScale, SpriteEffects.None, 0);


            // Draw the respawn time
            spriteBatch.DrawString(font, timeString, textPosition + new Vector2(0, 40), textColor, 0,
                                    font.MeasureString(timeString) / 2, textScale, SpriteEffects.None, 0);

            spriteBatch.End();
            
        } // End of the draw method
     
        /// <summary>
        /// A method to get a player back to an assault game.
        /// </summary>
        public void GetBackToAssaultGame()
        {
           
            // Get the current gamer.
            // Loop through each local gamer to set the start position.
            foreach (LocalNetworkGamer gamer in networkSession.LocalGamers)
            {
                NetworkPlayerProfile localPlayer = gamer.Tag as NetworkPlayerProfile;

                this.myGameCamera.cameraPosition = localPlayer.getAssaultStartPosition();
                this.myGameCamera.cameraLookUpDown = new Vector3(0, 0, 0);


                if (this.myGameCamera.isAttacking == true)
                {
                    // Reset the rotation matrix for the player soldier so that he is turned in the right direction.
                    this.myGameCamera.cameraDirection = new Vector3(0, 0, 1);
                    this.myGameCamera.playerModel.rotationMatrix = Matrix.Identity * Matrix.CreateRotationY(MathHelper.ToRadians(180));
                    Matrix.CreateRotationY(MathHelper.ToRadians(180));
                }
                else
                {
                    // Reset the rotation matrix for the player soldier so that he is turned in the right direction.
                    this.myGameCamera.cameraDirection = new Vector3(0, 0, -1);
                    this.myGameCamera.playerModel.rotationMatrix = Matrix.Identity;
                }
                
            }

            // Reset the spawn time and set the CanRespawn boolean
            this.spawnCountDown = 10000;

        } // End of the GetBackToAssaultGame method.

        /// <summary>
        /// At method to get a player back to a death match.
        /// </summary>
        public void GetBackToDeathMatch()
        {

            // Loop through each local gamer to set the start position.
            foreach (LocalNetworkGamer gamer in networkSession.LocalGamers)
            {
                NetworkPlayerProfile localPlayer = gamer.Tag as NetworkPlayerProfile;

                this.myGameCamera.cameraPosition = localPlayer.getDeathMatchStartPosition();
                this.myGameCamera.cameraLookUpDown = new Vector3(0, 0, 0);

                if (this.myGameCamera.isAttacking == true)
                {
                    // Reset the rotation matrix for the player soldier so that he is turned in the right direction.
                    this.myGameCamera.cameraDirection = new Vector3(0, 0, 1);
                    this.myGameCamera.playerModel.rotationMatrix = Matrix.Identity * Matrix.CreateRotationY(MathHelper.ToRadians(180));
                    Matrix.CreateRotationY(MathHelper.ToRadians(180));
                }
                else
                {
                    // Reset the rotation matrix for the player soldier so that he is turned in the right direction.
                    this.myGameCamera.cameraDirection = new Vector3(0, 0, -1);
                    this.myGameCamera.playerModel.rotationMatrix = Matrix.Identity;
                }

            }

            // Reset the spawn time and set the CanRespawn boolean
            this.spawnCountDown = 10000;

        } // End of the GetBackToDeathMatch method.

        #endregion

    } // End of class

} // End of namespace
