extends Control

@onready var choice = preload("res://scenes/upgradeChooser/choice/choice.tscn")

@onready var choiceContainer = $CenterContainer/VBoxContainer/ChoiceContainer

@onready var select = $select

func _ready() -> void:
	randomize()


func showUpgrades():
	show()
	
	for i in choiceContainer.get_children():
		i.queue_free()
	var selectableUpgrades = getRandomUpgrades(3)
	for i in selectableUpgrades:
		addChoice(i)
	
	
func getRandomUpgrades(amount: int) -> Array[int]:
	var selectableUpgrades: Array[int]
	var rng = RandomNumberGenerator.new()
	for i in range(0, amount):
		var choice: int = rng.randi_range(0, Globals.getUpgrades().size()-1)
		if choice in selectableUpgrades:
			choice = rng.randi_range(0, Globals.getUpgrades().size()-1)
		selectableUpgrades.append(choice)
	return selectableUpgrades
	
func addChoice(option: int):
	var _choice: Choice = choice.instantiate()
	choiceContainer.add_child(_choice)
	_choice.setValues(Globals.getUpgrade(option).upgradeName, Globals.getUpgrade(option).description, option)
	_choice.selected.connect(choose)

func choose(option: int):
	select.play()
	var test = StrafeUpgrade.new()
	Globals.getUpgrade(option).select()
	if Globals.getUpgrade(option) is DashUpgrade or Globals.getUpgrade(option) is StrafeUpgrade or Globals.getUpgrade(option) is SelfHealUpgrade:
		Globals.removeUpgrade(option)
	hide()
