using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class QueenCorrosion : MonoBehaviour
{
    [SerializeField] float Damage = 1;
    [SerializeReference] float AttackCadence = 1;

    float TimeToNextAttack = 0;
    List<TargetBase> CurrentTargets;

    private void Awake()
    {
        CurrentTargets = new List<TargetBase>();
    }

    void Update()
    {
        if (CurrentTargets.Count > 0)
        {
            TimeToNextAttack -= Time.deltaTime;
            TimeToNextAttack = TimeToNextAttack > 0f ? TimeToNextAttack : 0f;

            if (TimeToNextAttack <= 0)
            {
                TimeToNextAttack = 1f / AttackCadence;

                foreach (TargetBase target in CurrentTargets)
                {
                    target.NotifySwing(Damage);
                }

                Debug.Log("Ataco a plantiki:(");
            }
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Plant"))
        {
            TargetBase target = collision.GetComponentInChildren<TargetBase>();
            if (target)
            {
                CurrentTargets.Add(target);
            }
        }
    }

    private void OnTriggerStay2D(Collider2D collision)
    {

    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.CompareTag("Plant"))
        {
            TargetBase target = collision.GetComponentInChildren<TargetBase>();
            if (target && CurrentTargets.Contains(target))
            {
                CurrentTargets.Remove(target);
            }
        }
    }

}
