extends Control

@onready var _main: Main = get_tree().get_current_scene()
@onready var _edit := $Edit

@export var editable : bool:
	set(value):
		editable = value
		await ready
		set_editable(value)

var _title := ""
var _id := ""
var _room_setting_page

func _process(delta):
	_process_font_size()

func _process_font_size():
	var font_size = _edit.get_theme_default_font_size()
	var new_font_size = get_viewport_rect().size.y/35
	if font_size != new_font_size:
		_edit.add_theme_font_size_override("font_size", new_font_size)

func _ready():
	initialize_signals()

func initialize_signals():
	_edit.focus_entered.connect(_on_click)

func _on_click():
	if _main.get_current_page() == Main.Pages.RoomSelection:
		return

	_room_setting_page = _main.load_page(Main.Pages.RoomSelection)
	_room_setting_page.room_selected.connect(_on_room_is_set)

func _on_room_is_set(title: String, id: String):
	_room_setting_page.room_selected.disconnect(_on_room_is_set)
	set_title(title)
	set_id(id)

func set_title(title: String):
	_title = title
	_edit.text = title

func get_title() -> String:
	return _title

func set_id(id: String):
	_id = id

func get_id() -> String:
	return _id

func set_editable(value):
	_edit.editable = value

func clean():
	_title = ""
	_id = ""
	_edit.text = ""
