extends Node3D

@onready var level = $"level"
@onready var level_manager = $"/root/LevelManager"

@onready var ui = $"ui"

var is_finished = false


func _ready():
	self.level.game = self
	level_manager.exec_context = "game"
	level_manager.game = self
	
	if level_manager.selected_level == null or level_manager.selected_level == "":
		return
	self.level.loader.load_level_file(level_manager.selected_level)
	self.level.camera_rig.set_active_camera("game")
	
	self.ui.initialize(self)
	
	
	
func _process(_delta):
	_handle_ui_input()

func _handle_ui_input():
	if Input.is_action_just_pressed("ui_cancel"):
		if not self.ui.pause_menu.is_visible():
			open_pause_menu()
	if Input.is_action_just_pressed("restart"):
		restart()

func open_pause_menu():
	self.ui.show_pause_menu()
	
#do nothing right now, gets called when the pause menu is closed, just to inform the game scene if needed
func on_pause_menu_closed():
	pass

func restart():
	if level_manager.selected_level == null or level_manager.selected_level == "":
		return
	self.level.loader.load_level_file(level_manager.selected_level)
	self.level._initialize_level()
	
func on_level_finished():
	is_finished = true
	
	
	self.ui.show_level_done_screen()
	

#func check_all_goals():
#	
#	if screws_finished:
#		print ("level finished")
#		emit_signal("level_finished")
