plugins {
    id "org.jetbrains.kotlin.jvm"
    id "org.jetbrains.kotlin.plugin.serialization"
    id "application"
}

version = null

application {
    mainClass = 'dev.inmo.postssystem.server.EntranceKt'
}

dependencies {
    api project(":postssystem.features.common.server")
    api project(":postssystem.features.status.server")
    api project(":postssystem.features.files.server")
    api project(":postssystem.features.users.server")
    api project(":postssystem.features.auth.server")
    api project(":postssystem.features.roles.server")
    api project(":postssystem.features.roles.manager.server")
    api project(":postssystem.features.content.server")
    api project(":postssystem.features.content.text.server")
    api project(":postssystem.features.content.binary.server")
    api project(":postssystem.features.publication.server")

    api project(":postssystem.services.posts.server")

    api project(":postssystem.targets.telegram.publication.server")

    api libs.ktor.server.netty
    api libs.ktor.websockets
    api libs.exposed.jdbs
    api libs.postgresql
}


task copyClient(type: Copy) {
    dependsOn ":postssystem.client:jsBrowserDistribution"
    File clientSources = project(":postssystem.client").file("build/distributions")
    SourceDirectorySet resources = sourceSets.main.resources
    File webFolderPath = new File(resources.getSrcDirs()[0].toString(), "web")
    exclude("*.map")
    from clientSources.absolutePath
    into webFolderPath.absolutePath
}


tasks.getByName("compileKotlin")
        .dependsOn(project(":postssystem.client").jsBrowserDistribution)
        .dependsOn(copyClient)
tasks.getByName("compileKotlin").configure {
    mustRunAfter project(":postssystem.client").jsBrowserDistribution
    mustRunAfter copyClient
}

tasks.getByName("run")
        .dependsOn(project(":postssystem.client").jsBrowserDistribution)
        .dependsOn(copyClient)
tasks.getByName("run").configure {
    mustRunAfter project(":postssystem.client").jsBrowserDistribution
    mustRunAfter copyClient
}

task copyUIKitCSS(type: Copy) {
    dependsOn ":postssystem.client.uikit:build"
    File dist = project(":postssystem.client.uikit").file("dist")
    SourceDirectorySet resources = sourceSets.main.resources
    File webFolderPath = new File(resources.getSrcDirs()[0].toString(), "web")
    File cssFilesFolder = new File(dist, "css")
    from cssFilesFolder.absolutePath
    include "uikit.min.css"
    into new File(webFolderPath, "css").absolutePath
}

task copyUIKitJS(type: Copy) {
    dependsOn ":postssystem.client.uikit:build"
    File dist = project(":postssystem.client.uikit").file("dist")
    SourceDirectorySet resources = sourceSets.main.resources
    File webFolderPath = new File(resources.getSrcDirs()[0].toString(), "web")
    File jsFilesFolder = new File(dist, "js")
    from jsFilesFolder.absolutePath
    include "uikit.min.js"
    include "uikit-icons.min.js"
    into new File(webFolderPath, "js").absolutePath
}

tasks.getByName("processResources")
        .dependsOn(copyUIKitJS)
        .dependsOn(copyUIKitCSS)
tasks.getByName("processResources").configure {
    mustRunAfter copyUIKitJS
    mustRunAfter copyUIKitCSS
}
