extends Node3D

class_name UIManager

@onready var ui_map = {
	"profile" : $ProfileScreen,
	"hud" : $HudScreen,
	"status" : $StatusScreen,
	"menu" : $MenuScreen
}

var pause_count = 0
var top_ui_screen : Node

func _ready():
	Global.ui_mgr = self
	close_all()
	open("hud")

func input_action(key:String,is_pressed:bool):
	if top_ui_screen:
		top_ui_screen.call("input_action",key,is_pressed)
	
func input_dir(dir:Vector2):
	if top_ui_screen:
		top_ui_screen.call("input_dir",dir)

func open(ui_name:String):
	set_ui_visible(ui_name,true)

func close(ui_name:String):
	set_ui_visible(ui_name,false)

func close_all():
	for ui_name in ui_map:
		close(ui_name)

func pause_open(ui_name:String):
	open(ui_name)
	set_pause(true)

func pause_close(ui_name:String):
	close(ui_name)
	set_pause(false)

func set_ui_visible(ui_name:String,is_visible:bool):
	if not ui_name in ui_map:
		return
	var ui_screen = ui_map[ui_name]
	ui_screen.visible = is_visible
	if is_visible:
		top_ui_screen = ui_screen
		print("top_ui_screen:",ui_name)
	
func set_pause(is_pause:bool):
	pause_count += 1 if is_pause else -1
	is_pause = pause_count > 0
	Global.is_control_pause = is_pause
	get_tree().paused = is_pause
