package de.kif.backend

import com.fasterxml.jackson.databind.SerializationFeature
import de.kif.backend.route.*
import de.kif.backend.route.api.*
import de.kif.backend.util.pushService
import io.ktor.application.Application
import io.ktor.application.call
import io.ktor.application.install
import io.ktor.features.*
import io.ktor.http.HttpStatusCode
import io.ktor.http.content.files
import io.ktor.http.content.static
import io.ktor.jackson.jackson
import io.ktor.response.respond
import io.ktor.routing.routing
import io.ktor.websocket.WebSockets
import java.nio.file.Paths

fun Application.main() {
    install(DefaultHeaders)
    install(CallLogging)
    install(ConditionalHeaders)
    install(Compression)
    install(DataConversion)
    install(WebSockets)

    install(StatusPages) {
        exception<Throwable> {
            it.printStackTrace()
            call.respond(HttpStatusCode.InternalServerError)
        }
        statusFile(HttpStatusCode.NotFound, HttpStatusCode.InternalServerError, filePattern = "error#.html")
    }

    install(ContentNegotiation) {
        jackson {
            enable(SerializationFeature.INDENT_OUTPUT)
        }
    }

    security()

    routing {
        static("/static") {
            files(Configuration.Path.webPath.toFile())
        }

        static("/images") {
            files(Configuration.Path.uploadsPath.toFile())
        }

        val srcFile = Paths.get("src")?.toFile()
        if (srcFile != null && srcFile.exists() && srcFile.isDirectory) {
            static("/src") {
                files("src")
            }
        }

        // UI routes
        overview()
        calendar()
        login()
        account()

        board()

        workGroup()
        track()
        room()
        user()

        // RESTful routes
        authenticateApi()

        roomApi()
        scheduleApi()
        trackApi()
        userApi()
        workGroupApi()
        constraintsApi()
        postApi()

        // Web socket push notifications
        pushService()
    }
}
