using System.Collections;
using System.Collections.Generic;
using Assets._game.scripts;
using UnityEngine;

[System.Serializable]
public class Player {

	public GameObject Prefab { get; private set; }
    public PlayerData Data { get; private set; }
    public int PlayerId { get; private set; }
    public int HitPoints { get; private set; }
    public PlayerController Controller { get; set; }
    public bool IsKilled { get; private set; }

    public float TimeKilled { get; private set; }

    public delegate void PlayerHitHandler(Player player, int amount, GameEntity source);
    public delegate void PlayerFireHandler(Player player);
    public delegate void PlayerKilledHandler(Player player, GameEntity killer);

    public event PlayerHitHandler PlayerHit;
    public event PlayerKilledHandler PlayerKilled;
    public event PlayerFireHandler PlayerFire;

    public Player(PlayerData data, int playerId)
    {
        Data = data;
        PlayerId = playerId;
        HitPoints = data.HitPoints;
    }

    public void Fire()
    {
        if (PlayerFire != null)
        {
            PlayerFire(this);
        }
    }

    public void Damage(int amount, GameEntity source)
    {
        if (IsKilled)
        {
            return;
        }
        HitPoints -= amount;

        if (PlayerHit != null)
        {
            PlayerHit(this, amount, source);
        }
        if (HitPoints <= 0)
        {
            TimeKilled = Time.timeSinceLevelLoad;

            if (PlayerKilled != null)
            {
                PlayerKilled(this, source);
            }
            IsKilled = true;
        }
    }
}
