extends Node3D
class_name Level

# Note: next level now is the following level set in savegame Managers level_information dict

## Points needed to complete this level
@export var neededPoints : int = 100

## Unique ID of the current level, used for saving progress
var level_id: int = -1

func _ready():
	var path: String = get_tree().current_scene.scene_file_path
	print("path=", path)
	
	if self.name != "Level":
		printerr("THE NODE OF THE LEVEL NEEDS TO BE CALLED 'LEVEL'!!!!1!1!!!elf!!!")
		push_error("THE NODE OF THE LEVEL NEEDS TO BE CALLED 'LEVEL'!!!!1!1!!!elf!!!")
		self.name = "Level"
		#breakpoint
	
	level_id = SavegameManager.get_level_id_for_scene_path(path)
	if level_id < 0:
		push_error("Current level at path ", path, " has no entry in the level_information dictionary. Please add it in the savegame_manager.gd script.")
		#breakpoint
		
	GameConstants.neededPoints = neededPoints
	
	# Set next level
	GameConstants.nextScene = "res://ui/main_menu.tscn"
	
	var next_level_id: int = SavegameManager.get_next_level_id(level_id)
	if next_level_id >= 0:
		GameConstants.nextScene = SavegameManager.level_information[next_level_id]
		print("Next level scene set to ", GameConstants.nextScene)
	else:
		print("Next level id was >= 0, returning to main menu instead")

func on_won():
	# game has been won
	SavegameManager.level_won(level_id)
	SavegameManager.on_savegame_changed()
	
	# Triger win screen
	
	get_tree().change_scene_to_file("res://ui/WinScreen.tscn")
