extends HBoxContainer

@export var currency: Resource

@onready var _label_start = $LabelStart
@onready var _label_end = $LabelEnd
@onready var _progress_bar = $ProgressBar

var _start: BigNumber
var _end: BigNumber
var _current: BigNumber
var _currency_id: StringName = &""

func set_limits(start: BigNumber, end: BigNumber) -> void:
	_start = start
	_end = end
	
	_label_start.text = _start.to_string_suffix(2)
	_label_end.text = _end.to_string_suffix(2)
	
	_progress_bar.min_value = _start.get_ratio(_end)
	_progress_bar.max_value = _end.get_ratio(_start)

func _ready() -> void:
	if currency == null:
		currency = CurrencyDatabase.get_currency_resource(GameState.GOLD_CURRENCY_ID)
	if currency == null:
		push_warning("BigNumberProgressBar '%s' has no currency configured." % String(name))
		return
	_currency_id = GameState.get_currency_id(currency)

	GameState.currency_changed.connect(_on_currency_changed)
	_current = GameState.get_currency_amount_by_id(_currency_id)

	set_limits(_current, _current.add(BigNumber.new(1, 1)))

func _on_currency_changed(changed_currency_id: StringName, amount: BigNumber) -> void:
	if changed_currency_id != _currency_id:
		return
	_current = amount
	_progress_bar.value = amount.get_ratio(_end)
