using Godot;

namespace Newlon.Components;

public partial class ArmorHPObserver : Node
{
	[Export] private float _threshold = 0.5f;
	[Export] private bool _setGreater = false;
	[Export] private Armor _observedArmor;
	[Signal] public delegate void ThresholdReachedEventHandler();

	public override void _Ready()
	{
		_observedArmor.ArmorDamaged += OnHPChanged;
	}

	private void OnHPChanged(float delta)
	{
		if (_setGreater == false && _observedArmor._hp / _observedArmor.MaxHP < _threshold)
		{
			EmitSignal(SignalName.ThresholdReached);
			_observedArmor.ArmorDamaged -= OnHPChanged;
			QueueFree();
		}
		else if (_setGreater && _observedArmor._hp / _observedArmor.MaxHP > _threshold)
		{
			EmitSignal(SignalName.ThresholdReached);
			_observedArmor.ArmorDamaged -= OnHPChanged;
			QueueFree();
		}
	}
}
