using UnityEngine;

public abstract class BaseUnit : MonoBehaviour, ITarget
{
    [SerializeField]
    float speed = 3.5f;
    [SerializeField]
    public int hp = 3;
    [SerializeField]
    protected int damage = 1;

    public Rigidbody rigidBody;
    protected HealthBar healthBar;

    public bool IsDead { get; private set; }

    public int currentHp;
    public int life = 3;

    protected Dash dashAbility;
    protected Attack attackAbility;

    private void Awake()
    {
        currentHp = hp;
        rigidBody = GetComponent<Rigidbody>();

        dashAbility = GetComponent<Dash>();
        attackAbility = GetComponent<Attack>();
    } 

    public void Move(float x, float y)
    {
        if(IsDead == false)
        {
            rigidBody.velocity = new Vector3(x * speed, rigidBody.velocity.y, y * speed);
        } 
    }

    public void OnHit(int damage)
    {
        currentHp -= damage;
        healthBar.AdjustHealthBar(currentHp, hp);

        if (currentHp <= 0)
        {
            OnDeath();
        }
    }

    public void OnDeath()
    {
        ToggleVisibile(false);

        life--;
        IsDead = true;

        if (life > 0)
        {
            Invoke("Spawn", 5);
        }
        else
        {
            Destroy(gameObject);
        }

    }

    void Spawn()
    {
        ToggleVisibile(true);

        transform.position = new Vector3(0, 10, 0);

        currentHp = hp;
        healthBar.AdjustHealthBar(currentHp, hp);
        IsDead = false;
    }

    public Transform GetTargetPosition()
    {
        throw new System.NotImplementedException();
    }

    public void ToggleVisibile(bool isAlive)
    {
        gameObject.SetActive(isAlive);
        healthBar.gameObject.SetActive(isAlive);
    }
}
