extends VBoxContainer

@onready var _main: Main = get_tree().get_current_scene()
@onready var _back_button := $TopBar/BackButton
@onready var _apply_button := $TopBar/ApplyButton

@onready var _title_field := $TitleField
@onready var _date_field := $DateField
@onready var _start_time_field := $StartTimeField
@onready var _finish_time_field := $FinishTimeField
@onready var _creator_field := $CreatorField
@onready var _room_field := $RoomField
@onready var _description_field := $DescriptionField

func _ready():
	initialize_signals()

func initialize_signals():
	_back_button.pressed.connect(_on_back_button_pressed)
	_apply_button.pressed.connect(_on_apply_button_pressed)

func _on_back_button_pressed():
	_main.load_page(Main.Pages.Board)

func _on_apply_button_pressed():
	_create_reservation()

func _create_reservation():
	if not _fields_are_correct():
		return

	var dto = CreateReservationDTO.new()
	dto.title = _title_field.get_value()
	dto.date = _date_field.get_value()
	dto.start_time = _start_time_field.get_value()
	dto.finish_time = _finish_time_field.get_value()
	dto.creator = _creator_field.get_value()
	dto.room_id = _room_field.get_value() # TODO: make it listbox
	dto.description = _description_field.get_value()
	dto.color = randi_range(1, 3)

	var repo = _main.get_reservation_repo()
	repo.create_reservation(dto)

	dto.queue_free()

	_main.load_page(Main.Pages.Board)
	clean()

func _fields_are_correct():
	var successful := true

	if len(_title_field.get_value()) < 1:
		print("The title is not entered.")
		successful = false

	if not _time_is_correct():
		successful = false

	return successful

func _time_is_correct():
	var start_time = _start_time_field.get_value()
	var finish_time = _finish_time_field.get_value()

	var start_time_is_after_finish_time = \
		start_time["hours"] > finish_time["hours"] or \
		(start_time["hours"] == finish_time["hours"] and \
		start_time["minutes"] >= finish_time["minutes"])

	if start_time_is_after_finish_time:
		print("Start time should not be more than or equal to finish time.")
		return false

	var repo = _main.get_reservation_repo()
	var reservations = repo.list_reservations()

	var new_start_time_minutes = start_time["hours"]*60 + start_time["minutes"]
	var new_finish_time_minutes = finish_time["hours"]*60 + finish_time["minutes"]

	for reservation in reservations:
		var other_start_time = reservation.start_time
		var other_finish_time = reservation.finish_time
		var other_start_time_minutes = other_start_time["hours"]*60 + other_start_time["minutes"]
		var other_finish_time_minutes = other_finish_time["hours"]*60 + other_finish_time["minutes"]

		var is_busy = (
			(
				new_start_time_minutes >= other_start_time_minutes and
				new_start_time_minutes < other_finish_time_minutes
			) or (
				new_finish_time_minutes > other_start_time_minutes and 
				new_finish_time_minutes <= other_finish_time_minutes
			) or (
				new_start_time_minutes >= other_start_time_minutes and 
				new_finish_time_minutes <= other_finish_time_minutes
			) or (
				new_start_time_minutes <= other_start_time_minutes and 
				new_finish_time_minutes >= other_finish_time_minutes
			)
 		)

		if is_busy:
			print("The selected time slot is busy.")
			return false

	return true

func update():
	_ready()

func clean():
	_title_field.clean()
	_start_time_field.clean()
	_finish_time_field.clean()