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 _hours := 0
var _minutes := 0
var _time_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.TimeSetting:
		return

	_time_setting_page = _main.load_page(Main.Pages.TimeSetting)
	_time_setting_page.time_is_set.connect(self._on_time_is_set)

func _on_time_is_set(hours: int, minutes: int):
	_time_setting_page.time_is_set.disconnect(_on_time_is_set)
	set_time(hours, minutes)

func set_time(hours: int, minutes: int):
	_edit.text = str(hours) + ":" + _correct_minutes(str(minutes))
	_hours = hours
	_minutes = minutes

func _correct_minutes(value):
	return "0" + value if len(value) == 1 else value

func get_hours():
	return _hours

func get_minutes():
	return _minutes

func set_editable(value):
	_edit.editable = value

func clean():
	_hours = 0
	_minutes = 0
	_edit.text = ""
