extends TextureButton
@onready var name_label: Label = $CenterContainer/VBoxContainer/NameLabel
@onready var price_label: Label = $CenterContainer/VBoxContainer/PriceLabel

var unlock_id = -1 # Store the unlock ID
var unlock_description_text = "" # Store the description for custom tooltip

func _ready():
	name_label.visible = false # Hide label initially
	price_label.visible = false
	adjust_label_font_size()
	# Connect the pressed signal
	pressed.connect(_on_button_pressed)
	# Connect to currency changes to update button state
	Inventory.currency_changed.connect(_on_currency_changed)
	# Connect to unlock events to update button state when items are unlocked
	Unlocks.item_unlocked.connect(_on_item_unlocked)

func setup(unlock_data):
	# Log.pr("Setting up button for unlock:", unlock_data.unlock_name)
	unlock_id = unlock_data.unlock_id # Store the ID
	if name_label and price_label:
		name_label.visible = false
		price_label.visible = false
		update_button_state(unlock_data)
		adjust_label_font_size()
	else:
		Log.pr("Warning: Label nodes not found in button.")

func update_button_state(unlock_data):
	# Store unlock description for custom tooltip
	if unlock_data.unlock_description:
		unlock_description_text = unlock_data.unlock_description
		tooltip_text = unlock_data.unlock_description

	# Check if at max rank
	if not unlock_data.can_rank_up():
		self.disabled = true
		name_label.text = unlock_data.unlock_name + " (MAX)"
		price_label.text = ""
		return

	# Build name text - only show rank if it's a scaling unlock
	if unlock_data.is_scaling:
		name_label.text = unlock_data.unlock_name + " " + str(unlock_data.get_next_rank())
	else:
		name_label.text = unlock_data.unlock_name

	# Build price text
	price_label.text = Global.currency_symbol + Global.format_number(unlock_data.get_next_cost())

	# Check if player has enough currency
	var cost = unlock_data.get_next_cost()
	var current_currency = Inventory.get_currency()
	self.disabled = current_currency < cost

func _on_currency_changed(new_amount: float):
	# Update button state when currency changes
	if unlock_id >= 0:
		var unlock_data = Unlocks.get_unlock_by_id(unlock_id)
		if unlock_data:
			update_button_state(unlock_data)

func _on_item_unlocked():
	# Update button state when any item is unlocked (in case this button reached max rank)
	if unlock_id >= 0:
		var unlock_data = Unlocks.get_unlock_by_id(unlock_id)
		if unlock_data:
			update_button_state(unlock_data)
			adjust_label_font_size()

func _on_button_pressed():
	# Log.pr("Button pressed, unlocking item:", unlock_id)
	Unlocks.unlock_item(unlock_id)

func adjust_label_font_size():
	if not name_label or not price_label:
		return
	var available_width = size.x - 10
	var available_height = size.y - 10

	# Calculate font sizes for both labels
	# Name label gets 60% of the height, price label gets 40%
	var name_height = available_height * 0.6
	var price_height = available_height * 0.4

	# Start with reasonable font sizes
	var name_font_size = 32
	var price_font_size = 24
	var min_font_size = 8

	# Get fonts
	var name_font = name_label.get_theme_font("font")
	var price_font = price_label.get_theme_font("font")

	# Calculate name label font size without applying it
	while name_font_size > min_font_size:
		var text_size = name_font.get_string_size(name_label.text, HORIZONTAL_ALIGNMENT_LEFT, -1, name_font_size)
		if text_size.x <= available_width and text_size.y <= name_height:
			break
		name_font_size -= 1

	# Calculate price label font size without applying it
	while price_font_size > min_font_size:
		var text_size = price_font.get_string_size(price_label.text, HORIZONTAL_ALIGNMENT_LEFT, -1, price_font_size)
		if text_size.x <= available_width and text_size.y <= price_height:
			break
		price_font_size -= 1

	# Apply both font sizes at once
	name_label.add_theme_font_size_override("font_size", name_font_size)
	price_label.add_theme_font_size_override("font_size", price_font_size)

	name_label.visible = true
	price_label.visible = true

# Call this function whenever you change the label text
func set_label_text(new_text: String):
	if name_label:
		name_label.visible = false # Hide while resizing
		price_label.visible = false
		name_label.text = new_text
		price_label.text = ""
		adjust_label_font_size()

# Override to create custom tooltip with larger font
func _make_custom_tooltip(for_text: String) -> Object:
	var tooltip_label = Label.new()
	tooltip_label.text = for_text
	tooltip_label.add_theme_font_size_override("font_size", 26)

	# Create a panel container for background
	var panel = PanelContainer.new()
	panel.add_child(tooltip_label)

	return panel
