class_name GeneratorBuffTile
extends HBoxContainer

signal buy_pressed(buff_id: StringName)

@onready var _icon: TextureRect = $Icon
@onready var _text_label: Label = $TextLabel
@onready var _level_label: Label = $LevelLabel
@onready var _effect_label: Label = $EffectLabel
@onready var _cost_label: Label = $CostLabel
@onready var _buy_button: Button = $BuyButton

var _buff_id: StringName = &""

func set_buff(buff: GeneratorBuffData) -> void:
	if buff == null:
		_buff_id = &""
		_icon.texture = null
		_icon.visible = false
		_text_label.text = "Unknown Buff"
		return

	var buff_id_text: String = String(buff.id).strip_edges()
	_buff_id = StringName(buff_id_text)

	_icon.texture = buff.icon
	_icon.visible = buff.icon != null
	_text_label.text = buff.text if not buff.text.strip_edges().is_empty() else buff_id_text

func set_runtime(level: int, effect_text: String, cost_text: String, can_buy: bool, is_maxed: bool) -> void:
	_level_label.text = "Lv %d" % maxi(level, 0)
	_effect_label.text = effect_text

	if is_maxed:
		_cost_label.text = "Max level"
		_buy_button.text = "Max"
		_buy_button.disabled = true
		return

	_cost_label.text = cost_text
	_buy_button.text = "Buy"
	_buy_button.disabled = not can_buy

func _on_buy_button_pressed() -> void:
	if _buff_id == &"":
		return

	buy_pressed.emit(_buff_id)
