extends Control

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

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

var _date := ""
var _date_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.CalendarSetting:
		return

	_date_setting_page = _main.load_page(Main.Pages.CalendarSetting)
	_date_setting_page.date_selected.connect(self._on_date_is_set)

func _on_date_is_set(date: Calendar.DateObj):
	_date_setting_page.date_selected.disconnect(_on_date_is_set)
	set_date("%02d.%02d.%04d" % [date.day, date.month, date.year])

func set_date(date: String):
	_date = date
	_edit.text = get_date()

func get_date():
	return _date

func set_editable(value):
	_edit.editable = value

func clean():
	_date = ""
	_edit.text = ""
