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.Content;
using Audio3D;

namespace FredriksdalAssault2
{
    /// <summary>
    /// This class is used to store data about each player in a split screen game so that we can
    /// draw a soldier for each player and more. Implements IComparable so that we can sort player data
    /// on number of kills.
    /// </summary>
    public class LocalPlayerProfile : PlayerProfile
    {
        #region Instance variables

        // Camera, viewport, dynamic resource manager and blood effects.
        public Camera playerCamera;
        public Viewport playerViewPort;
        public DynamicResourcesManager playerShots;
        public BloodParticles bloodEffects;

        // Blood effects
        public int bloodEffectTime = 5000;
        public int bloodEffectCounter = -1;
        public Vector3 bloodEffectStartPosition;

        // Name of the killer
        public String killedByGamerTag;

        // Shot delay and shot countdown to slow shooting sound. 
        int shotDelay = 100;
        int shotCountdown = 0;

        #endregion

        #region Constructors

        /// <summary>
        /// Create a new local player profile for split screen games.
        /// </summary>
        /// <param name="content"></param>
        /// <param name="gamerId"></param>
        /// <param name="gamerName"></param>
        /// <param name="attacker"></param>
        /// <param name="camera"></param>
        /// <param name="viewport"></param>
        public LocalPlayerProfile(ContentManager content, byte gamerId, String gamerName, bool attacker, GraphicsDevice graphicsDevice, 
            Camera camera, Viewport viewport) 
            : base(content, gamerId, gamerName, attacker, graphicsDevice)
        {
            // Set the camera and the viewport
            this.playerCamera = camera;
            this.playerViewPort = viewport;

        }

        #endregion

        #region Methods

        /// <summary>
        /// Update the player profile.
        /// </summary>
        /// <param name="gameTime"></param>
        public override void UpdatePlayerProfile(GameTime gameTime, AudioManager audioManager)
        {
            // Update the simple soldier.
            if (this.isDead == false)
            {
                // A living soldier
                this.soldier.UpdateSimpleSoldier(gameTime, playerCamera.playerModel.position, playerCamera.playerModel.rotationQuaternion, playerCamera.playerModel.clipNumber);

                // Update audio emitter options
                this.Position = this.playerCamera.cameraPosition;
                this.Forward = this.playerCamera.cameraDirection;
                this.Up = Vector3.Up;
                this.Velocity = this.playerCamera.cameraDirection * this.playerCamera.cameraSpeed;

                // Play the walking sound.
                if (this.playerCamera.playerModel.clipNumber == 1)
                {
                    if (this.walkCountdown <= 0)
                    {
                        // For variety, choose between (footstep0 or footstep1).
                        string soundName;
                        if (this.walkSoundIndex == 0)
                        {
                            soundName = "footstep" + this.walkSoundIndex++;
                        }
                        else
                        {
                            soundName = "footstep" + this.walkSoundIndex--;
                        }

                        // Play the sound.
                        audioManager.Play3DSound(soundName, false, this);

                        // Reset the walking counter.
                        this.walkCountdown = this.walkDelay;
                    }
                    else
                    {
                        // Decrease the counter.
                        this.walkCountdown -= gameTime.ElapsedGameTime.Milliseconds;
                    }
                }

                // Play the sound, when the shot delay has been running out.
                if (shotCountdown <= 0)
                {
                    // Play the shooting sound.
                    if (this.isShooting == true)
                    {
                        audioManager.Play3DSound("fire", false, this);

                        // Reset the shot countdown
                        shotCountdown = shotDelay;
                    }     
                }
                else
                {
                    // Subract game time from the shotCountdown counter
                    shotCountdown -= gameTime.ElapsedGameTime.Milliseconds;
                }
            }
            else
            {
                // A dead soldier.
                this.soldier.UpdateDeadSoldier(gameTime, playerCamera.playerModel.position, playerCamera.playerModel.rotationQuaternion);
            }

        } // End of the UpdatePlayerProfile method

        /// <summary>
        /// Draw the enemy shot effect.
        /// </summary>
        /// <param name="gameTime"></param>
        /// <param name="view"></param>
        /// <param name="projection"></param>
        public override void DrawShotEffect(GameTime gameTime, Matrix view, Matrix projection)
        {
            // Update the enemy shot effect
            this.enemyShotParticles.UpdateEnemyShotEffect(gameTime);

            // If the player not is dead.
            if (this.isDead == false)
            {
                // Add a particle to the enemy shot effect if the player is shooting
                if (this.isShooting == true)
                {
                    Vector3 position = this.playerCamera.playerModel.position + new Vector3(0, 57.5f, 0) + this.playerCamera.cameraDirection * 40;
                    this.enemyShotParticles.AddParticles(1, position, this.playerCamera.cameraDirection);
                }

                // Draw the shot effect
                this.enemyShotParticles.DrawEnemyShotEffect(gameTime, view, projection);
            }

        } // End of the DrawShotEffect method

        #endregion

    } // End of class

} // End of namespace
