extends Control
class_name PauseUI

@onready var button_resume: Button = $MarginContainer/VBoxContainer/ButtonResume
@onready var settings: SettingsUI = $SettingsUI
@onready var base_pause_container: Control = $MarginContainer


func _ready():
	self.hide()

func _process(_delta):
	if(Input.is_action_just_pressed("pause")):
		if not settings.visible and self.visible:
			_on_button_resume_pressed()
		else:
			pause()
		
func pause():
	get_tree().paused = true
	base_pause_container.show()
	self.show()
	
func _on_button_resume_pressed():
	if not self.visible:
		return
	get_tree().paused = false
	self.hide()

func _on_button_restart_pressed():
	restart_level()
	
func restart_level():
	get_tree().paused = false
	get_tree().reload_current_scene()

func _on_visibility_changed():
	if not self.visible:
		return
	button_resume.grab_focus()

func _on_button_settings_pressed():
	base_pause_container.hide()
	settings.show()

func _on_hide_settings():
	self.show()
	base_pause_container.show()
	settings.hide()

func _on_settings_ui_back_button_pressed():
	_on_hide_settings()

func _on_button_back_to_main_menu_pressed():
	# TODO maybe add confirm dialogue
	get_tree().paused = false
	get_tree().change_scene_to_file("res://ui/main_menu.tscn")

