using UnityEngine;
using ScrambleKit.FX;

namespace ScrambleKit.Hits
{
    public class HitPoints : MonoBehaviour
    {
        public int MaxHP = 3;
        public int HP;

        public FXTrigger fxTrigger;

        void Awake()
        {
            Debug.Log("HitPoints awake, setting to max for " + transform.name);
            HP = MaxHP;
        }

        void TakeHit(HitInfo hit)
        {
            if (HP <= 0)
                return;

            HitFX(hit.Direction);
            HP = Mathf.Max(0, HP - hit.Damage);
            if (HP <= 0)
                Invoke("Die", hit.StunTime);
        }

        private void Die()
        {
            transform.root.SendMessage("Dying", SendMessageOptions.DontRequireReceiver);
            Destroy(gameObject);
        }

        private void HitFX(Vector2 dir)
        {
            fxTrigger.Trigger(transform);
        }
    }
}
