extends ScrollContainer

onready var LEVELS_PATH = "res://src/Levels/"
onready var BUTTON_GROUP : ButtonGroup = load("res://src/UserInterface/Screens/LevelSelectButtonGroup.tres")
onready var vbox: VBoxContainer = $VBoxContainer

func _ready() -> void:
	BUTTON_GROUP.connect("pressed", get_node('../Buttons/PlayButton'), "_level_selection_changed")
	_fill_level_list()

func _fill_level_list():
	var levels = []
	var dir = Directory.new()
	if dir.open(LEVELS_PATH) == OK:
		dir.list_dir_begin()
		while true:
			var file_name = dir.get_next()
			if file_name == "":
				break
			else:
				if(file_name.ends_with(".tscn")):
					levels.append(file_name)
	if levels.size() != 0:
		_spawn_level_options(levels)

func _spawn_level_options(levels: Array):
	for level in levels:
		var check_box = CheckBox.new()
		check_box.text = level.trim_suffix(".tscn")
		check_box.set_button_group(BUTTON_GROUP);
		vbox.add_child(check_box)
	return