package de.kif.backend.route

import de.kif.backend.PortalSession
import de.kif.backend.UserPrinciple
import de.kif.backend.prefix
import de.kif.backend.view.respondMain
import io.ktor.application.call
import io.ktor.auth.authenticate
import io.ktor.auth.principal
import io.ktor.response.respondRedirect
import io.ktor.routing.Route
import io.ktor.routing.get
import io.ktor.routing.post
import io.ktor.routing.route
import io.ktor.sessions.clear
import io.ktor.sessions.get
import io.ktor.sessions.sessions
import io.ktor.sessions.set
import kotlinx.html.*

fun Route.login() {
    route("login") {
        authenticate {
            post {
                val principal = call.principal<UserPrinciple>() ?: return@post
                if (principal.user.id == null) return@post
                call.sessions.set(PortalSession(principal.user.id))
                call.respondRedirect(call.parameters["redirect"] ?: "$prefix/")
            }
        }

        get {
            val redirect = call.parameters["redirect"] ?: "$prefix/"
            val needLogin = call.sessions.get<PortalSession>() == null
            if (needLogin) {
                respondMain {
                    content {
                        div {
                            div {
                                br { }
                                form("$prefix/login?redirect=$redirect", method = FormMethod.post) {
                                    div("form-group") {
                                        label {
                                            htmlFor = "username"
                                            +"Name"
                                        }
                                        input(
                                            name = "username",
                                            classes = "form-control"
                                        ) {
                                            id = "username"
                                            placeholder = "Name"
                                        }
                                    }
                                    div("form-group") {
                                        label {
                                            htmlFor = "password"
                                            +"Passwort"
                                        }
                                        input(
                                            name = "password",
                                            classes = "form-control",
                                            type = InputType.password
                                        ) {
                                            id = "password"
                                            placeholder = "Passwort"
                                        }
                                    }
                                    input(
                                        name = "redirect",
                                        type = InputType.hidden
                                    ) {
                                        value = redirect
                                    }
                                    button(type = ButtonType.submit, classes = "form-btn btn-primary") {
                                        +"Einloggen"
                                    }
                                }

                                if ("onFailure" in call.parameters) {
                                    br { }
                                    div("alert alert-danger") {
                                        +"Ungültige Kombination aus Name und Passwort"
                                    }
                                }
                            }
                        }
                    }
                }
            } else {
                call.respondRedirect(redirect)
            }
        }
    }

    get("logout") {
        call.sessions.clear<PortalSession>()
        call.respondRedirect("$prefix/")
    }
}
