extends Control


@onready var perk_effects = get_node("/root/Game/PerkEffects")

var perks
@onready var perkCard = preload("res://scenes/PerkCard.tscn")
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	perks = perk_effects.available_perks
	get_node("/root/Game/DropManager").leveled_up.connect(show_perks)
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass

func show_perks():
	if perks.is_empty():
		return
	var tween = create_tween()
	tween.tween_property($ColorRect, "color:a", 0.6, 0.3)
	get_tree().paused = true
	var shuffled = perks.duplicate()
	shuffled.shuffle()
	var chosen = shuffled.slice(0, 3)
	for x in chosen:
		var card = perkCard.instantiate()
		$HBoxContainer.add_child(card)
		card.setup(x, func(): select_perk(x))
		

func select_perk(perk):
	perk.effect.call()
	get_tree().paused = false
	perks.erase(perk)
	for child in $HBoxContainer.get_children(): child.queue_free()
	var tween = create_tween()
	tween.tween_property($ColorRect, "color:a", 0.0, 0.3)
	await tween.finished
