extends MarginContainer


@onready var button_level: Button = $VBoxContainer/ButtonLevel
var key: int = -1

func initialize(level_key: int):
	key = level_key
	
	# Set level number
	var level_number: int = 0
	var done: bool = false
	var playable: bool = false
	var previous_done: bool = false
	for i in range(0, len(SavegameManager.level_information)):
		var i_key: int = SavegameManager.level_information.keys()[i]
		if i_key == key:
			if i == 0 or previous_done:
				playable = true
			level_number = (i + 1)
			if SavegameManager.get_level_completed(i_key) == true:
				done = true
			break
		previous_done = SavegameManager.get_level_completed(i_key)
	button_level.text = "Level " + str(level_number) + (" - Done" if done else "")
	button_level.disabled = not playable and not done

func _on_button_level_pressed() -> void:
	if key < 0:
		printerr("key < 0")
		breakpoint
	else:
		if SavegameManager.level_information.has(key):
			var path: String = SavegameManager.level_information[key] 
			get_tree().change_scene_to_file(path)
	
	
