using System;
using UnityEngine;

public class Player : MonoBehaviour, ICharacter
{
    [HideInInspector]
    public Character PlayerCharacter;
    public CharacterCondition Condition;

    public Character GetCharacter => PlayerCharacter;

    private void Awake()
    {
        PlayerCharacter = new Character();
        Condition = PlayerCharacter.Condition;
    }

    private void OnDestroy()
    {
        Debug.LogWarning("Pooled object was destroyed");
    }

    public event Action<bool> OnDeathEvent;
    public void GetDamage(int damage)
    {
        PlayerCharacter.LastTimeHit = TimeManager.Instance.CurrentTime;
        Condition.GiveHealth(-Mathf.RoundToInt(damage * (1 - Condition.ArmourPoints * 0.5f)));
        Condition.GiveArmour(-Mathf.RoundToInt(Mathf.Sqrt(damage) * 5));
        print(Condition.HealthPoints);
        if (Condition.HealthPoints < 10)
            Die();
    }

    private void Die()
    {
        print("dead");
        OnDeathEvent?.Invoke(true);
        CharacterFactory.Instance.ReSpawn(this, gameObject);
    }

    public void ResetCharacter()
    {
        Condition = new CharacterCondition();
    }
}
