package de.kif.backend.route

import de.kif.backend.Configuration
import de.kif.backend.repository.PostRepository
import de.kif.backend.repository.ScheduleRepository
import de.kif.backend.view.respondMain
import de.kif.common.formatDateTime
import de.kif.common.model.Schedule
import io.ktor.routing.Route
import io.ktor.routing.get
import kotlinx.css.CSSBuilder
import kotlinx.css.Color
import kotlinx.html.div
import kotlinx.html.span
import java.util.*

fun Route.board() {
    get("/board") {
        val postList = PostRepository.all().asReversed()
        val scheduleList = ScheduleRepository.all().map {
            it to it.getAbsoluteStartTime() * 60
        }.sortedBy { it.second }

        val referenceTime = Configuration.Schedule.referenceDate.time / 1000
        val now = referenceTime - (Date().time / 1000)

        respondMain(true, true) {
            content {
                div("board-header") {
                    div {
                        +"KIF 47.0"
                    }
                    div("board-header-date") {
                        +formatDateTime(Date().time)
                    }
                }
                div("board") {
                    div("board-schedules") {
                        attributes["data-reference"] = referenceTime.toString()

                        for ((schedule, time) in scheduleList) {
                            div("board-schedule") {
                                attributes["data-id"] = schedule.id.toString()

                                span("board-schedule-room") {
                                    +schedule.room.name
                                }
                                span("board-schedule-time") {
                                    attributes["data-time"] = time.toString()
                                    attributes["data-duration"] = schedule.workGroup.length.toString()

                                    +Schedule.timeDifferenceToString(time + now)
                                }
                                span("board-schedule-color") {
                                    attributes["style"] = CSSBuilder().apply {
                                        val c = schedule.workGroup.track?.color
                                        if (c != null) {
                                            backgroundColor = Color(c.toString())
                                        }
                                    }.toString()
                                }
                                span("board-schedule-name") {
                                    +schedule.workGroup.name
                                }
                            }
                        }
                    }

                    div("board-posts") {
                        for (post in postList) {
                            createPost(post, false, "board-post overview-post")
                        }
                    }

                    div("board-twitter") {

                    }
                }
            }
        }
    }
}
