using Godot;

namespace Newlon.Components;

[GlobalClass]
public partial class Armor : Node
{
	[Signal]
	public delegate void ArmorDamagedEventHandler(float hp);
	[Signal]
	public delegate void ArmorLostEventHandler();

	[Export]
	public float MaxHP { get; private set; }
	public float _hp;
	private bool _lost = false;

    public override void _Ready()
    {
        _hp = MaxHP;
    }

	public float RecieveDamage(float damage)
	{
		if(_lost)
			return damage;
		float returnAmount = 0;
		_hp -= damage;
		if(_hp <= 0)
		{
			returnAmount = -_hp;
			_hp = 0;
			EmitSignal(SignalName.ArmorLost);
			_lost = true;
		}
		EmitSignal(SignalName.ArmorDamaged,_hp);
		return returnAmount;
	}

	public float Heal(float amount)
	{
		if(_lost)
			return amount;
		float returnAmount = 0;
		_hp += amount;
		if (_hp >= MaxHP)
		{
			returnAmount = _hp-MaxHP;
			_hp = MaxHP;
		}
		EmitSignal(SignalName.ArmorDamaged,_hp);
		return returnAmount;
	}
}
