extends Control

# Smart ist es die notwendigen Resourcen vor dem Skriptstart zu laden
onready var signal_manager := get_tree().root.get_child(4).get_node("%SignalManager")
onready var level_state := get_tree().root.get_child(4).get_node("%LevelState")
onready var current_scene := get_tree().get_current_scene()
onready var pause_overlay := get_node("PauseOverlay")
onready var pause_title: Label = get_node("PauseOverlay/Title")

var paused := false setget set_paused
var block_ui_cancel = false


func _ready():
	#signal_manager.connect("player_died", self, "_on_GlobalState_player_died")
	$ControlsMenu.visible = false
	$ControlsMenu.set_process_input(false)
	$AudioMenu.set_process_input(false)
	signal_manager.connect("game_paused", self, "set_paused")
	pass


func open_audio_menu():
	block_ui_cancel = true
	pause_overlay.visible = false
	$AudioMenu.visible = true
	$AudioMenu.set_process_input(true)
	$"%MasterSlider".grab_focus()


func close_audio_menu():
	$AudioMenu.visible = false
	pause_overlay.visible = true
	$AudioMenu.set_process_input(false)
	$"%Continue".grab_focus()
	get_tree().set_input_as_handled()


func _on_GlobalState_player_died() -> void:
	self.paused = true
	pause_title.text = "You lost"


func _input(event: InputEvent) -> void:
	if !event.is_action("pause"):
		return
	if block_ui_cancel || $"%ControlsMenu".visible:
		block_ui_cancel = false
		get_tree().set_input_as_handled()
		return
	#not oder ! schaltet einen boolean um
	#Ist self hier notwendig?

	self.paused = not paused
	block_ui_cancel = true


func set_paused(value: bool) -> void:
	paused = value
	get_tree().paused = value
	pause_overlay.visible = value
	if value == true:
		$"%Continue".grab_focus()


func _on_Controls_button_up() -> void:
	$ControlsMenu.visible = true
	$ControlsMenu.set_process_input(true)
	block_ui_cancel = true
	pause_overlay.visible = false
	$"%ProfilesMenu".grab_focus()
