extends Control

var is_paused = false setget set_is_paused

func _unhandled_input(event):
	if event.is_action_pressed("game_pause"):
		self.is_paused = !is_paused
		Input.start_joy_vibration(0, 1, 1, 0.1)
		Input.vibrate_handheld(100)
		var player = AudioStreamPlayer.new()
		self.add_child(player)
		player.stream = load("res://assets/sounds/click.wav")
		player.play()
		$Control/ResumeButton.grab_focus()
	if event.is_action_pressed("game_resume"):
		Input.start_joy_vibration(0, 1, 1, 0.1)
		Input.vibrate_handheld(100)
		var player = AudioStreamPlayer.new()
		self.add_child(player)
		player.stream = load("res://assets/sounds/click.wav")
		player.play()
		self.is_paused = false
	#if event.is_action_pressed("settings"):
	#	Input.start_joy_vibration(0, 1, 1, 0.1)
	#	Input.vibrate_handheld(100)
	#	var player = AudioStreamPlayer.new()
	#	self.add_child(player)
	#	player.stream = load("res://assets/sounds/click.wav")
	#	player.play()
	#	$"../../../../WindowDialog".grab_focus

		
	if event.is_action_pressed("game_return_to_menu"):
		self.is_paused = !is_paused
		Input.start_joy_vibration(0, 1, 1, 0.1)
		Input.vibrate_handheld(100)
		var player = AudioStreamPlayer.new()
		self.add_child(player)
		player.stream = load("res://assets/sounds/click.wav")
		player.play()
		get_tree().change_scene("res://Menu.tscn")

func set_is_paused(value):
	is_paused = value
	get_tree().paused = is_paused
	visible = is_paused
	$Control/CenterContainer/VBoxContainer/ResumeGameButton.grab_focus()

func _on_ResumeGameButton_pressed():
	Input.start_joy_vibration(0, 1, 1, 0.1)
	Input.vibrate_handheld(100)
	var player = AudioStreamPlayer.new()
	self.add_child(player)
	player.stream = load("res://assets/sounds/click.wav")
	player.play()
	self.is_paused = false


func _on_MainMenuButton_pressed():
	self.is_paused = !is_paused
	Input.start_joy_vibration(0, 1, 1, 0.1)
	Input.vibrate_handheld(100)
	var player = AudioStreamPlayer.new()
	self.add_child(player)
	player.stream = load("res://assets/sounds/click.wav")
	player.play()
	get_tree().change_scene("res://Menu.tscn")

func _notification(what):
	if (what == MainLoop.NOTIFICATION_WM_GO_BACK_REQUEST):
		self.is_paused = !is_paused
		$Control/CenterContainer/VBoxContainer/ResumeGameButton.grab_focus()
#
#func _on_buttontitlesettings_pressed():
#	$"../../../../WindowDialog".grab_focus


func _on_ResumeButton_pressed():
	Input.start_joy_vibration(0, 1, 1, 0.1)
	Input.vibrate_handheld(100)
	var player = AudioStreamPlayer.new()
	self.add_child(player)
	player.stream = load("res://assets/sounds/click.wav")
	player.play()
	self.is_paused = false
