extends Panel

signal key_selected(scancode)
var prev_focus

func _ready() -> void:
	set_process_input(false)
	
func _process(delta: float) -> void:
	$Prompt2.text = ("(ESC to cancel or wait: %s" % round($Timer.time_left)) + " sec)"

func _input(event: InputEvent) -> void:
	if !self.visible || !event.is_pressed():
		return
	# TODO Static quit button esc
	if "scancode" in event && event.scancode == 16777217:
		event = null
	accept_event()
	get_tree().set_input_as_handled()
	emit_signal("key_selected", event)
	close()

func open():
	show()
	$Timer.start()
	prev_focus = get_focus_owner()
	self.grab_focus()
	set_process_input(true)

func close():
	hide()
	$Timer.stop()
	self.release_focus()
	prev_focus.grab_focus()
	set_process_input(false)
	
func close_on_timeout():
	hide()
	self.release_focus()
	prev_focus.grab_focus()
	set_process_input(false)
	emit_signal("key_selected", null)
