package de.kif.frontend.views.calendar

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

class CalendarCell(view: HTMLElement) : ViewCollection<CalendarEntry>(view) {
    val day = dataset["day"]?.toIntOrNull() ?: 0
    val time = dataset["time"]?.toIntOrNull() ?: 0
    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
    }

    init {
        (view.getElementsByClassName("calendar-link")[0] as? HTMLElement)?.remove()
    }
}