package de.kif.backend.route

import de.kif.backend.PortalSession
import de.kif.backend.UserPrinciple
import de.kif.backend.view.MainTemplate
import de.kif.backend.view.respondMain
import io.ktor.application.call
import io.ktor.auth.authenticate
import io.ktor.auth.principal
import io.ktor.html.respondHtmlTemplate
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"] ?: "/")
            }
        }

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

                                if ("onFailure" in call.parameters) {
                                    br { }
                                    div("alert alert-danger") {
                                        +"Username or password incorrect!"
                                    }
                                }
                            }
                        }
                    }
                }
            } else {
                call.respondRedirect(call.parameters["redirect"] ?: "/")
            }
        }
    }

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