extends Node2D

@onready var animations = $"animations"

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

@onready var restart_button = $"widgets/restart_button"
@onready var main_menu_button = $"widgets/main_menu_button"
@onready var back_button = $"widgets/back_button"
@onready var back_button_label = $"widgets/back_button/label"

var parent = null




func _ready():
	self.connect_buttons()
	
	self.set_process_input(false)


func execute_restart():
	if parent.has_method("restart"):
		parent.restart()
	execute_back()
	
func execute_main_menu():
	get_tree().paused = false
	self.get_tree().change_scene_to_file("res://scenes/ui/main_menu.tscn")
	
func execute_back():
	hide_pause_menu()

func show_pause_menu():
	self.animations.play("show")
	get_tree().paused = true
	self.set_process_input(true)

	await self.get_tree().create_timer(0.1).timeout


	self.back_button.grab_focus()



func hide_pause_menu():
	
	self.animations.play("hide")
	get_tree().paused = false
	if parent != null:
		#get the editor back in normal state, as showing the pause menu hides the selection box
		parent.on_pause_menu_closed()
	self.set_process_input(false)


	
func set_mode_game(_game):
	parent = _game
	back_button_label.text = "resume"
	
func set_mode_editor(_editor):
	parent = _editor
	hide_restart_button()
	
func hide_restart_button():
	restart_button.hide()
	
func connect_buttons():
	self.restart_button.connect("pressed", Callable(self, "execute_restart"))

	self.main_menu_button.connect("pressed", Callable(self, "execute_main_menu"))
	self.back_button.connect("pressed", Callable(self, "execute_back"))







