extends MenuBase

var can_close_menu: bool = false

@onready var idle_timer: Timer = $IdleTimer
@onready var saw_artwork: Control = $Saw


func _ready() -> void:
	close_menu()
	GameGlobals.player_died.connect(_on_player_died)


func _input(event: InputEvent) -> void:
	if can_close_menu and is_visible_in_tree() and event.is_pressed():
		can_close_menu = false

		await SceneFader.fade_in()
		await get_tree().create_timer(0.5).timeout

		close_menu()

		GameGlobals.reset_game()
		GameGlobals.load_from_save()
		SceneFader.fade_out.call_deferred()
		get_tree().paused = false
		get_tree().reload_current_scene()


func display_death_background(death_type: GameGlobals.DeathTypes) -> void:
	match death_type:
		GameGlobals.DeathTypes.SAW:
			saw_artwork.show()


func _on_player_died(death_type: GameGlobals.DeathTypes) -> void:
	can_close_menu = false

	for child: Node in get_children():
		if child.has_method(&"hide"):
			child.hide()

	display_death_background(death_type)

	open_menu()
	Input.mouse_mode = Input.MOUSE_MODE_VISIBLE

	idle_timer.start()
	await idle_timer.timeout

	can_close_menu = true
