using UnityEngine;
using UnityEngine.Events;

/// <summary>
/// GAME 1303 Tier 3 — A tank that drains while you use it. Hot Air's fuel; also fits
/// Detective Liz's flashlight battery or a jetpack.
///
/// SETUP
///  - Add to the player/balloon. Set Max Fuel. BalloonController calls Consume() while
///    the burner is on.
///  - OnFuelChanged(0-1) -> HUD Slider. OnEmpty -> warning sound / lose condition.
///  - Refuel pickups: make a Collectible with an ItemType, then on the Collectible's
///    OnCollected event call FuelSystem.Refill(amount) — or just use a FuelPickup
///    trigger (see below) for simplicity.
/// </summary>
public class FuelSystem : MonoBehaviour
{
    public float maxFuel = 100f;
    public float startFuel = 100f;

    [Header("Events")]
    public UnityEvent<float> OnFuelChanged;   // normalized 0-1
    public UnityEvent OnEmpty;
    public UnityEvent OnRefueled;

    public float Fuel { get; private set; }
    public bool IsEmpty => Fuel <= 0f;
    public float Normalized => maxFuel > 0f ? Fuel / maxFuel : 0f;

    void Awake() { Fuel = Mathf.Clamp(startFuel, 0f, maxFuel); }
    void Start() { OnFuelChanged?.Invoke(Normalized); }

    /// <summary>Try to burn some fuel. Returns false if the tank is empty.</summary>
    public bool Consume(float amount)
    {
        if (IsEmpty) return false;
        Fuel = Mathf.Max(0f, Fuel - amount);
        OnFuelChanged?.Invoke(Normalized);
        if (Fuel <= 0f) OnEmpty?.Invoke();
        return true;
    }

    public void Refill(float amount)
    {
        Fuel = Mathf.Min(maxFuel, Fuel + amount);
        OnFuelChanged?.Invoke(Normalized);
        OnRefueled?.Invoke();
    }

    public void RefillFull() => Refill(maxFuel);
}