using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponHitbox : MonoBehaviour
{
    private BoxCollider2D col;
    private WeaponStat _stat;

    private string atktype;

    //이번 공격에서 이미 때린 적들의 ID를 저장하는 리스트
    private HashSet<int> hitEnemies = new HashSet<int>();
    private PlayerController playerController;

    public float gageAmount = 20;

    void Awake()
    {
        playerController = GetComponentInParent<PlayerController>();
        col = GetComponent<BoxCollider2D>();
        gameObject.SetActive(false);
    }

    private void OnEnable()
    {
        hitEnemies.Clear();
    }

    public void Setup(WeaponStat stat, string atkType)
    {
        _stat = stat;

        if (atkType == "SAtk") // 약공격
        {
            col.size = stat.data.sboxSize;
            col.offset = stat.data.sboxOffset;
        }
        else if (atkType == "AAtk") // 강공격
        {
            col.size = stat.data.aboxSize;
            col.offset = stat.data.aboxOffset;
        }
        atktype = atkType;
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Enemy"))
        {
            int enemyID = other.gameObject.GetInstanceID();
            if (hitEnemies.Contains(enemyID)) return;

            hitEnemies.Add(enemyID);

            Mob enemy = other.GetComponent<Mob>();
            
            if (enemy != null && atktype == "SAtk")
            {
                playerController.ChargeTagGauge(20);
                enemy.TakeDamage(_stat.data.sDamage, _stat.data.hitStunTime, transform.position);
            }

            else if (enemy != null && atktype == "AAtk")
            {
                playerController.ChargeTagGauge(30);
                enemy.TakeDamage(_stat.data.aDamage, _stat.data.hitStunTime, transform.position);
            }
        }
    }
}