using Godot;
using Newlon.Systems.Effects;
using System.Collections.Generic;
namespace Newlon.Components.Plants;

public partial class NerdusReturnAttack : Area2D
{
	private float returnAmount;
	[Export] private Effect returnEffect;
	[Export] private float bitesToPeas = 1;
	public bool triggered = false;
	private List<Entity> entities = new();
	public override void _Ready()
	{
		AreaEntered += OnAreaEntered;
		AreaExited += OnAreaExited;
	}
	private void OnAreaEntered(Area2D area)
	{
		if (area.GetParent() is Entity entity)
		{
			entities.Add(entity);
		}
	}
	private void OnAreaExited(Area2D area)
	{
		if (area.GetParent() is Entity entity && entities.Contains(entity))
		{
			entities.Remove(entity);
		}
	}
	private void OnHPChanged(float delta, Node source)
	{
		if (delta >= 0) return;
		returnAmount -= delta;
		triggered = true;
	}
	public void ReturnAllDamage()
	{
		foreach (var entity in entities)
		{
			entity.TakeDamage(returnAmount * bitesToPeas, GetParent<Entity>());
			entity.GiveEffect(returnEffect);
		}
		returnAmount = 0;
		triggered = false;
	}
}
