using Godot;

namespace Cirno.Scripts.Components.Actors;

public partial class ActorResourceProvider : Node2D
{
    [Export]
    public string ResourceName { get; private set; }
    
    public float MaxResource
    {
        get => _maxResource;
        set
        {
            if (_maxResource == value) return;
            
            if (value > _currentResource)
            {
                EmitSignal(SignalName.ResourceIncreased, _currentResource, _currentResource, value);
            }
            else if (value < _currentResource)
            {
                EmitSignal(SignalName.ResourceDecreased, _currentResource, _currentResource, value);
            }

            _maxResource = value;
            
            EmitSignal(SignalName.ResourceChanged, _currentResource, value);
        }
    }

    private float _currentResource = 0f;
    
    [Export]
    private float _maxResource = 10f;

    [Signal]
    public delegate void ResourceChangedEventHandler(float newValue, float maxValue);
    
    [Signal]
    public delegate void ResourceIncreasedEventHandler(float oldValue, float newValue, float maxValue);
    
    [Signal]
    public delegate void ResourceDecreasedEventHandler(float oldValue, float newValue, float maxValue);
    
    [Signal]
    public delegate void ResourceMaxedEventHandler(float maxValue);
    
    [Signal]
    public delegate void ResourceDepletedEventHandler();
    
    public float CurrentResource
    {
        get => _currentResource;
        set
        {
            if (_currentResource == value) return;
            
            if (value > _currentResource)
            {
                EmitSignal(SignalName.ResourceIncreased, _currentResource, value, MaxResource);
            }
            else if (value < _currentResource)
            {
                EmitSignal(SignalName.ResourceDecreased, _currentResource, value, MaxResource);
            }
            
            _currentResource = value;

            if (_currentResource >= MaxResource) {
                _currentResource = MaxResource;
                EmitSignal(SignalName.ResourceMaxed, MaxResource);
            }
            else if (_currentResource <= 0)
            {
                EmitSignal(SignalName.ResourceDepleted);
                _currentResource = 0;
            }
				
            EmitSignal(SignalName.ResourceChanged, _currentResource, MaxResource);
        }
    }

    public void FillResource()
    {
        CurrentResource = MaxResource;
    }
}