extends Control

@onready var currency_label: Label = %CurrencyLabel
@onready var wood_label: Label = %WoodLabel
@onready var stock_label: Label = %StockLabel

@onready var modifiers_label: Label = %ModifiersLabel

func _ready():
	populate_modifiers_display()
	populate_unlock_buttons()
	update_currency_label()
	update_wood_label()
	update_stock_label()

	currency_label.add_theme_color_override("font_color", Global.money_color)
	wood_label.add_theme_color_override("font_color", Global.wood_color)
	stock_label.add_theme_color_override("font_color", Global.stock_color)

	Inventory.currency_changed.connect(_on_currency_changed)
	Inventory.currency_added.connect(spawn_currency_increase)
	Inventory.wood_changed.connect(_on_currency_changed)
	Inventory.wood_added.connect(spawn_wood_increase)
	Inventory.stock_added.connect(spawn_stock_increase)
	Inventory.stock_changed.connect(_on_currency_changed)
	Unlocks.item_unlocked.connect(populate_unlock_buttons)

func update_currency_label():
	currency_label.text = Global.currency_symbol + " " + str(int(Inventory.get_currency()))

func update_wood_label():
	wood_label.text = "W: " + str(int(Inventory.get_wood()))

func update_stock_label():
	stock_label.text = "S: " + str(int(Inventory.get_stock()))

func spawn_currency_increase(value, _total):
	spawn_inventory_change_value(value, _total, "+", Global.currency_symbol, Global.money_color)

func spawn_wood_increase(value, _total):
	spawn_inventory_change_value(value, _total, "+", "", Global.wood_color)

func spawn_stock_increase(value, _total):
	spawn_inventory_change_value(value, _total, "+", "", Global.stock_color)

func spawn_inventory_change_value(value, _total, display_sign: String = "+", symbol: String = "", label_color: Color = Color.WHITE):
	var float_label = Label.new()
	float_label.text = display_sign + symbol + str(int(abs(value)))
	float_label.add_theme_font_size_override("font_size", 16)
	float_label.modulate = label_color
	
	# Add random offset around center
	var random_x = randf_range(-60, 30)
	var random_y = randf_range(-40, 20)
	float_label.position = Vector2(random_x, random_y)
	add_child(float_label)
	
	# Animate the label
	var tween = create_tween()
	tween.set_parallel(true) # Run both animations simultaneously
	# Move up
	tween.tween_property(float_label, "position:y", float_label.position.y - 50, 1.0)
	# Fade out
	tween.tween_property(float_label, "modulate:a", 0.0, 1.0)
	# Remove from scene when done
	tween.chain().tween_callback(float_label.queue_free)


func populate_unlock_buttons():
	var unlocks_container = $UnlockContainer
	for child in unlocks_container.get_children():
		child.free()

	for unlock_data in Unlocks.unlocks.unlocks:
		var unlock_button_scene = load("res://scenes/button.tscn")
		var unlock_button = unlock_button_scene.instantiate()
		unlocks_container.add_child(unlock_button)
		unlock_button.setup(unlock_data)

func populate_modifiers_display():
	var modifiers_text = ""

	modifiers_text = modifiers_text + "Sale Price: " + Global.currency_symbol + str(Unlocks.get_sale_price_per_item()) + "\n"
	modifiers_text = modifiers_text + "Items Produced Per Tick: " + str(Unlocks.get_items_produced_per_tick()) + "\n"
	modifiers_text = modifiers_text + "Wood per Click: " + str(Unlocks.get_wood_per_click()) + "\n\n"
	modifiers_text = modifiers_text + "Demand: " + str(int(Unlocks.get_sale_demand())) + "\n\n"

	modifiers_text = modifiers_text + "Current Modifiers:\n"
	for key in Unlocks.current_modifiers.keys():
		var display_name = key.replace("_modifier", "").replace("_", " ").capitalize()
		var percentage = int((Unlocks.current_modifiers[key] - 1.0) * 100)
		modifiers_text += "%s: %s%%\n" % [display_name, str(percentage)]

	modifiers_label.text = modifiers_text
	

func _on_currency_changed(_value):
	populate_modifiers_display()
	update_currency_label()
	update_wood_label()
	update_stock_label()
