package de.kif.backend.view

import de.kif.common.model.Permission
import de.kif.common.model.User
import io.ktor.html.Template
import kotlinx.html.*

class MenuTemplate() : Template<FlowContent> {

    var active: Tab = Tab.BOARD
    var user: User? = null

    override fun FlowContent.apply() {
        nav("menu") {
            div("container") {
                div("menu-left") {
                    a("/", classes = if (active == Tab.BOARD) "active" else null) {
                        +"Dashboard"
                    }
                    a("/calendar", classes = if (active == Tab.CALENDAR) "active" else null) {
                        +"Calendar"
                    }
                }
                div("menu-right") {
                    span("menu-icon") {
                        i("material-icons") { +"menu" }
                    }
                    val user = user
                    div("menu-content") {
                        if (user == null) {
                            a("/account", classes = if (active == Tab.ACCOUNT) "active" else null) {
                                +"Login"
                            }
                        } else {
                            if (user.checkPermission(Permission.WORK_GROUP)) {
                                a("/workgroups", classes = if (active == Tab.WORK_GROUP) "active" else null) {
                                    +"Work groups"
                                }
                            }
                            if (user.checkPermission(Permission.ROOM)) {
                                a("/rooms", classes = if (active == Tab.ROOM) "active" else null) {
                                    +"Rooms"
                                }
                            }
                            if (user.checkPermission(Permission.USER)) {
                                a("/users", classes = if (active == Tab.USER) "active" else null) {
                                    +"Users"
                                }
                            }
                            a("/account", classes = if (active == Tab.ACCOUNT) "active" else null) {
                                +user.username
                            }
                        }
                    }
                }
            }
        }
    }

    enum class Tab {
        BOARD, CALENDAR, ACCOUNT, WORK_GROUP, ROOM, PERSON, USER
    }
}
