extends Control

var credits_shown : bool = false

@export var level_node : Control
@export var credits_node : Control
@export var credits_button : Button
@export var levels_vbox : VBoxContainer
@export var exit_button : Button

# Called when the node enters the scene tree for the first time.
func _ready():
	if OS.get_name() == "Web":
		exit_button.hide()
	Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
	for level_num in GameManager.levels.size():
		var new_button : Button = Button.new()
		new_button.text = "Level " + str(level_num+1) + " : " + GameManager.levels[level_num].level_name
		new_button.pressed.connect(func(): pick_level(GameManager.levels[level_num].level_name))
		levels_vbox.add_child(new_button)
		
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	if credits_shown:
		credits_node.visible = true
		level_node.visible = false
		credits_button.text = "Back to Level Select"
	else:
		credits_node.visible = false
		level_node.visible = true
		credits_button.text = "Credits"


func pick_level(picked_level:String):
	for level_num in GameManager.levels.size():
		if GameManager.levels[level_num].level_name == picked_level:
			get_tree().change_scene_to_file(GameManager.levels[level_num].level_path)
			GameManager.current_level = level_num

func _on_credits_button_pressed():
	credits_shown = !credits_shown


func _on_credits_page_meta_clicked(meta):
	OS.shell_open(str(meta))


func _on_exit_button_pressed():
	get_tree().quit()
