using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StrategicCombat.Teams;
using StrategicCombat.CombatUnits;
using StrategicCombat.Maps;
using StrategicCombat.Shots;

namespace StrategicCombat.PlayerProfiles
{
    /// <summary>
    /// This class represent a player profile.
    /// </summary>
    public abstract class PlayerProfile
    {
        #region Variables

        public int playerId;
        public String playerName;
        public bool hasTurn = false;
        public Team team;

        // Variables for combat
        public CombatUnit combatUnitInCombat;

        #endregion

        #region Constructors

        /// <summary>
        /// Create a new player profile.
        /// </summary>
        /// <param name="playerId"></param>
        /// <param name="playerName"></param>
        /// <param name="hasTurn"></param>
        /// <param name="team"></param>
        public PlayerProfile(int playerId, String playerName, bool hasTurn, Team team)
        {
            // Set values for instance variables
            this.playerId = playerId;
            this.playerName = playerName;
            this.hasTurn = hasTurn;
            this.team = team;
        }

        #endregion

        #region Methods

        public abstract bool PlayerMovementOnBoard(PlayerIndex playerIndex, Map currentMap, PlayerProfile otherPlayer);
        public abstract void PlayerMovementInCombat(Viewport viewPort, PlayerIndex playerIndex, PlayerProfile otherPlayer, CombatScene currentCombatScene);

        /// <summary>
        /// Update the player profile on the board.
        /// </summary>
        /// <param name="gameTime"></param>
        /// <param name="currentMap"></param>
        public void UpdateOnBoard(GameTime gameTime, Map currentMap)
        {
            // Update the team on the board.
            this.team.UpdateTeamOnBoard(gameTime, currentMap);

        } // End of the UpdateOnBoard method

        /// <summary>
        /// Draw the player profile on the board.
        /// </summary>
        /// <param name="spriteBatch"></param>
        public virtual void DrawOnBoard(SpriteBatch spriteBatch, SpriteFont font, Map currentMap)
        {
            // Draw the team on the board
            this.team.DrawTeamOnBoard(spriteBatch);

        } // End of the DrawOnBoard method

        /// <summary>
        /// Draw the combat unit in a combat.
        /// </summary>
        /// <param name="spriteBatch"></param>
        public virtual void DrawInCombat(SpriteBatch spriteBatch)
        {
            this.combatUnitInCombat.DrawInCombat(spriteBatch);

        } // End of the DrawInCombat method

        /// <summary>
        /// Check for a collision between shots and another combat unit.
        /// </summary>
        /// <param name="otherPlayerProfile"></param>
        public virtual void CollisionCheckShotsAndPlayer(PlayerProfile otherPlayerProfile)
        {
            for(int i = 0; i < this.combatUnitInCombat.shots.Count; i++)
            {
                if(this.combatUnitInCombat.shots[i].shotCollisionRectangle.Intersects(otherPlayerProfile.combatUnitInCombat.combatCollisionRectangle))
                {
                    // Decrease the health.
                    otherPlayerProfile.combatUnitInCombat.health -= (int)this.combatUnitInCombat.healthDamage;

                    // Remove the shot.
                    this.combatUnitInCombat.shots.RemoveAt(i);
                }
            }

        } // End of the CollisionCheckShotsAndPlayer method

        /// <summary>
        /// Check for a collision between shots and another combat unit.
        /// </summary>
        /// <param name="otherPlayerProfile"></param>
        public virtual void CollisionShotsAndOtherPlayer(PlayerProfile otherPlayerProfile)
        {
            for (int i = 0; i < this.combatUnitInCombat.shots.Count; i++)
            {
                if (this.combatUnitInCombat.shots[i].shotCollisionRectangle.Intersects(otherPlayerProfile.combatUnitInCombat.combatCollisionRectangle))
                {
                    // Remove the shot.
                    this.combatUnitInCombat.shots.RemoveAt(i);
                }
            }

        } // End of the CollisionShotsAndOtherPlayer method

        /// <summary>
        /// Check for a collision between another player shots and the combat unit.
        /// </summary>
        /// <param name="otherPlayerProfile"></param>
        public virtual void CollisionPlayerAndOtherShots(PlayerProfile otherPlayerProfile)
        {
            for (int i = 0; i < otherPlayerProfile.combatUnitInCombat.shots.Count; i++)
            {
                if (otherPlayerProfile.combatUnitInCombat.shots[i].shotCollisionRectangle.Intersects(this.combatUnitInCombat.combatCollisionRectangle))
                {
                    // Decrease the health.
                    this.combatUnitInCombat.health -= (int)otherPlayerProfile.combatUnitInCombat.healthDamage;

                    // Remove the shot.
                    otherPlayerProfile.combatUnitInCombat.shots.RemoveAt(i);
                }
            }

        } // End of the CollisionPlayerAndOtherShots method

        #endregion

    } // End of class

} // End of namespace
