package de.kif.backend.route

import com.soywiz.klock.*
import com.soywiz.klock.locale.german
import de.kif.backend.Configuration
import de.kif.backend.repository.RoomRepository
import de.kif.backend.repository.ScheduleRepository
import de.kif.backend.view.respondMain
import de.kif.common.model.Room
import de.kif.common.model.Schedule
import io.ktor.routing.Route
import io.ktor.routing.get
import kotlinx.html.div
import kotlinx.html.span
import kotlin.math.max
import kotlin.math.min

data class WallData(
    val number: Int,
    val schedules: Map<Room, Map<Int, List<Schedule>>>,
    val max: Int?,
    val min: Int?
)

suspend fun genWallData(day: Int): WallData {
    val list = ScheduleRepository.getByDay(day)
    val rooms = RoomRepository.all()

    if (list.isEmpty()) return WallData(day, rooms.associateWith { emptyMap<Int, List<Schedule>>() }, null, null)

    val schedules = list.groupBy { it.room }.mapValues { (_, it) ->
        it.groupBy {
            it.time
        }
    }

    var max = 0
    var min = 24 * 60
    for (s in list) {
        max = max(max, s.time + s.workGroup.length)
        min = min(min, s.time)
    }

    if (min > max) {
        val h1 = max
        max = min
        min = h1
    }

    min = (min / 60 - 1) * 60
    max = (max / 60 + 2) * 60

    return WallData(day, schedules, max, min)
}

fun Route.wall() {
    get("/wand") {

        val wallStart = Configuration.Schedule.wallStart
        val days = listOf(
            wallStart,
            wallStart + 1,
            wallStart + 2
        ).map { genWallData(it) }

        var min = days.mapNotNull { it.min }.min() ?: 12 * 60
        val max = days.mapNotNull { it.max }.max() ?: 12 * 60

        if (min > max) {
            min = max
        }

        val refDate = DateTime(Configuration.Schedule.referenceDate.time)

        respondMain(true, true) {
            content {
                div("wall") {
                    for (day in days) {

                        val date = refDate + day.number.days
                        val dateString = DateFormat("EEEE, d. MMMM")
                            .withLocale(KlockLocale.german)
                            .format(date)


                        div("wall-box") {
                            div("wall-name") {
                                span {
                                    +dateString
                                }
                            }
                            div("wall-calendar calendar") {
                                renderCalendar(
                                    CalendarOrientation.TIME_TO_ROOM,
                                    day.number,
                                    min,
                                    max,
                                    day.schedules.keys.toList().sortedBy { it.id },
                                    day.schedules
                                )
                            }
                        }
                    }
                }
            }
        }
    }
}
