@tool
extends HBoxContainer

enum Type {
	Line,
	Text,
	Calendar,
	Time,
	ListBox
}

@export var title : String:
	set(value):
		title = value
		set_title(title)

@export var type := Type.Line:
	set(value):
		type = value
		set_type(type)

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

@onready var _label = $Container/FieldLabelControl/FieldLabel
@onready var _line = $Container/Value/Line
@onready var _calendar = $Container/Value/Calendar
@onready var _time = $Container/Value/Time
@onready var _text = $Container/Value/Text
@onready var _list_box = $Container/Value/ListBox

var _type

func _process(delta):
	_process_fonts()

func _process_fonts():
	_process_font_size(_label, 35)
	_process_font_size(_line, 35)
	_process_font_size(_text, 35)

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

func _ready():
	set_title(title)
	set_type(type)

func set_title(new_title):
	if _label == null:
		await ready

	_label.text = new_title

func get_value():
	match _type:
		Type.Line:
			return _line.text
		Type.Text:
			return _text.text
		Type.Calendar:
			return _calendar.get_date()
		Type.Time:
			return {
				"hours": _time.get_hours(),
				"minutes": _time.get_minutes()
			}
		Type.ListBox:
			return {
				"id": _list_box.get_id(),
				"title": _list_box.get_title()
			}

func set_value(value):
	match _type:
		Type.Line:
			_line.text = value
		Type.Text:
			_text.text = value
		Type.Calendar:
			_calendar.set_date(value)
		Type.Time:
			_time.set_time(value.hours, value.minutes)
		Type.ListBox:
			_list_box.set_id(value.id)
			_list_box.set_title(value.title)

func set_editable(value):
	if _line == null:
		await ready

	_line.editable = value
	_text.editable = value
	_time.editable = value

func set_type(type):
	if _line == null:
		await ready

	_line.visible = false
	_text.visible = false
	_calendar.visible = false
	_time.visible = false
	_list_box.visible = false

	_type = type

	match type:
		Type.Line:
			_line.visible = true
		Type.Text:
			_text.visible = true
		Type.Calendar:
			_calendar.visible = true
		Type.Time:
			_time.visible = true
		Type.ListBox:
			_list_box.visible = true

func clean():
	match _type:
		Type.Line:
			_line.text = ""
		Type.Text:
			_text.text = ""
		Type.Calendar:
			_calendar.clean()
		Type.Time:
			_time.clean()
		Type.ListBox:
			_list_box.clean()
