package de.kif.backend.view

import de.kif.backend.Resources
import io.ktor.application.ApplicationCall
import io.ktor.application.call
import io.ktor.html.*
import io.ktor.request.path
import io.ktor.response.respondRedirect
import io.ktor.util.pipeline.PipelineContext
import kotlinx.html.*

class MainTemplate(
    private val theme: Theme,
    private val noMenu: Boolean,
    private val stretch: Boolean
) : Template<HTML> {
    val content = Placeholder<HtmlBlockTag>()
    val menuTemplate = TemplatePlaceholder<MenuTemplate>()

    override fun HTML.apply() {
        head {
            meta(charset = "utf-8")
            meta(name = "viewport", content = "width=device-width, initial-scale=1.0")

            title("KIF Portal")

            link(href = "/static/external/material-icons.css", type = LinkType.textCss, rel = LinkRel.stylesheet)
            link(href = "/static/external/font/Montserrat.css", type = LinkType.textCss, rel = LinkRel.stylesheet)
            link(
                href = "https://fonts.googleapis.com/css?family=Bungee|Oswald",
                type = LinkType.textCss,
                rel = LinkRel.stylesheet
            )
            link(href = "/static/style/style.css", type = LinkType.textCss, rel = LinkRel.stylesheet)

            when (theme) {
                Theme.LIGHT -> {
                    // Ignore
                }
                Theme.DARK -> {
                    link(href = "/static/style/dark.css", type = LinkType.textCss, rel = LinkRel.stylesheet)
                }
                Theme.PRINCESS -> {
                    link(href = "/static/style/princess.css", type = LinkType.textCss, rel = LinkRel.stylesheet)
                }
            }

            script(src = "/static/require.min.js") {}

            script {
                unsafe {
                    +"require.config({baseUrl: '/static'});\n"
                    +("require([${Resources.jsModules}]);\n")
                }
            }
        }
        body {
            if (!noMenu) {
                insert(MenuTemplate(), menuTemplate)
            }

            val containerClasses = if (stretch) "container-full" else "container"
            div(containerClasses) {
                div("main") {
                    insert(content)
                }
            }

            if (!noMenu) {
                div("footer") {
                    div("container") {
                        div("footer-credit") {
                        }
                        div("footer-theme") {
                            for (it in Theme.values()) {
                                val name = it.name.toLowerCase()
                                a("?theme=${it.name}", classes = if (theme == it) "selected" else "") {
                                    id = "theme-$name"
                                    +name.capitalize()
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

enum class Theme {
    LIGHT, DARK, PRINCESS
}

private fun String?.toTheme() = this?.let { str ->
    Theme.values().find { str == it.name }
} ?: Theme.LIGHT

suspend fun PipelineContext<Unit, ApplicationCall>.respondMain(
    noMenu: Boolean = false,
    stretch: Boolean = false,
    body: MainTemplate.() -> Unit
) {
    val param = call.request.queryParameters["theme"]

    if (param != null) {
        call.response.cookies.append(
            name = "theme",
            value = param.toTheme().name,
            maxAge = Int.MAX_VALUE,
            path = "/"
        )
        call.respondRedirect(call.request.path())
    } else {
        call.respondHtmlTemplate(
            MainTemplate(
                call.request.cookies["theme"].toTheme(),
                noMenu,
                stretch
            ),
            body = body
        )
    }
}
