class_name CurrencyTile
extends HBoxContainer

@export var currency: Resource

@onready var _icon: TextureRect = $CurrencyIcon
@onready var _label: Label = $Label
@onready var _currency_label: Label = $CurrencyValueLabel
@onready var _debug_button: Button = $DebugIncomeButton

var _currency_id: StringName = &""

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

	_currency_id = GameState.get_currency_id(currency)
	_label.text = "%s:" % GameState.get_currency_name(_currency_id)
	_icon.texture = GameState.get_currency_icon(_currency_id)
	_debug_button.text = "+100 %s" % GameState.get_currency_name(_currency_id)

	GameState.currency_changed.connect(_on_currency_changed)
	_on_currency_changed(_currency_id, GameState.get_currency_amount_by_id(_currency_id))

func _on_currency_changed(changed_currency_id: StringName, amount: BigNumber) -> void:
	if changed_currency_id != _currency_id:
		return
	_currency_label.text = amount.to_string_suffix(2)

func _on_debug_income_button_pressed() -> void:
	if currency == null:
		return
	GameState.add_currency(currency, BigNumber.from_float(100))
