package de.kif.frontend.views.calendar

import de.kif.common.model.Room
import de.kif.frontend.repository.RoomRepository
import de.westermann.kwebview.ViewCollection
import de.westermann.kwebview.createHtmlView
import org.w3c.dom.HTMLDivElement
import org.w3c.dom.HTMLElement
import org.w3c.dom.set

class CalendarCell(row: CalendarRow, view: HTMLElement) : ViewCollection<CalendarEntry>(view) {
    val day = row.day
    val time = row.time

    val roomId = dataset["room"]?.toLongOrNull() ?: 0

    private lateinit var room: Room

    suspend fun getRoom(): Room {
        if (this::room.isInitialized) {
            return room
        }

        room = RoomRepository.get(roomId) ?: throw NoSuchElementException()
        return room
    }

    companion object {
        fun create(row: CalendarRow, roomId: Long): CalendarCell {
            val view = createHtmlView<HTMLDivElement>()
            view.classList.add("calendar-cell")
            view.dataset["room"] = roomId.toString()

            return CalendarCell(row, view)
        }
    }
}