using UnityEngine;

namespace ScrambleKit.Hits
{
    public class InvulnerableRecover : MonoBehaviour
    {
        public Transform HurtBox;
        public float InvulnerableTime = 0.3f;

        private float invulnUntil;
        private Collider2D hurtCollider;

        void TakeHit(HitInfo hit)
        {
            invulnUntil = Time.fixedTime + InvulnerableTime;
        }

        void Start()
        {
            hurtCollider = HurtBox.GetComponent<Collider2D>();
        }

        void Update()
        {
            if (invulnUntil >= Time.fixedTime)
                MakeInvulnerable();
            else
                ClearInvulnerable();
            
        }

        private void MakeInvulnerable()
        {
            if (hurtCollider.enabled)
                hurtCollider.enabled = false;            
        }

        private void ClearInvulnerable()
        {
            if (!hurtCollider.enabled)
                hurtCollider.enabled = true;
        }
    }
}