using System.Collections;
using Unicorn.System;
using UnityEngine;
using UnityEngine.InputSystem;

namespace Unicorn.Player
{
    public class PlayerAttack: MonoBehaviour
    {
        public GameObject attackHitbox;
        private UnicornInputSystem _unicornInput;
        private bool _attackAvailable = true;
        private void Start()
        {
            _unicornInput = UnicornInput.Unicorn;
            _unicornInput.Player.Attack.performed += Attack;
        }

        private void OnDisable()
        {
            _unicornInput.Player.Attack.performed -= Attack;
        }

        private void Attack(InputAction.CallbackContext obj)
        {
            if (!_attackAvailable) return;
            StartCoroutine(OnAttack());
        }

        private IEnumerator OnAttack()
        {
            _attackAvailable = false;
            attackHitbox.SetActive(true);
            yield return new WaitForSeconds(0.5f);
            attackHitbox.SetActive(false);
            yield return new WaitForSeconds(0.1f);
            _attackAvailable = true;
        }
    }
}