plugins {
    id 'java'
    id 'com.gradleup.shadow' version '9.2.2'
    id 'com.github.gmazzo.buildconfig' version '5.7.0'
    id 'xyz.wagyourtail.jvmdowngrader' version '1.3.3'
    id 'com.vanniktech.maven.publish' version "0.34.0"
}

group = "net.flectone.pulse"
version = project_version

subprojects {
    apply plugin: 'java'
    apply plugin: 'com.github.gmazzo.buildconfig'
    apply plugin: 'com.gradleup.shadow'
    apply plugin: 'xyz.wagyourtail.jvmdowngrader'
    apply plugin: 'com.vanniktech.maven.publish'

    compileJava.options.encoding = 'UTF-8'

    tasks.withType(JavaCompile).configureEach {
        options.encoding = 'UTF-8'
    }

    java {
        sourceCompatibility = java_version
        targetCompatibility = java_version
        withSourcesJar()
    }

    repositories {
        mavenCentral()

        maven { url = "https://jitpack.io" }
        maven { url = "https://hub.spigotmc.org/nexus/content/repositories/snapshots/" }
        maven { url = "https://oss.sonatype.org/content/groups/public/" }
        maven { url = "https://repo.aikar.co/content/groups/aikar/" }
        maven { url = "https://repo.codemc.io/repository/maven-releases/" }
        maven { url = "https://repo.minebench.de" }
        maven { url = "https://repo.alessiodp.com/snapshots" }
        maven { url = "https://libraries.minecraft.net" }
        maven { url = "https://maven.maxhenkel.de/repository/public" }
        maven { url = "https://repo.extendedclip.com/content/repositories/placeholderapi/" }
        maven { url = "https://repo.plasmoverse.com/releases" }
        maven { url = "https://repo.plasmoverse.com/snapshots" }
        maven { url = "https://repo.loohpjames.com/repository" }
        maven { url = "https://repo.diogotc.com/releases" }
        maven { url = "https://repo.codemc.io/repository/maven-snapshots/" }
        maven { url = "https://maven.devs.beer/" }
        maven { url = "https://maven.nucleoid.xyz/" }
        maven { url = "https://api.modrinth.com/maven" }
    }

    dependencies {
        compileOnly "org.projectlombok:lombok:$lombok_version"
        compileOnly "org.jetbrains:annotations:$jetbrains_annotations_version"
        compileOnly "com.google.inject:guice:$guice_version"
        compileOnly "tools.jackson.dataformat:jackson-dataformat-yaml:$jackson_dataformat_version"
        compileOnly "net.kyori:adventure-api:$adventure_api"
        compileOnly "net.kyori:adventure-text-serializer-legacy:$adventure_api"
        compileOnly "net.kyori:adventure-text-minimessage:$adventure_api"
        compileOnly "net.kyori:adventure-text-serializer-gson:$adventure_api"
        compileOnly "net.kyori:adventure-text-serializer-plain:$adventure_api"
        compileOnly "org.apache.commons:commons-text:$apache_commons_text_version"

        annotationProcessor "org.projectlombok:lombok:$lombok_version"
    }

    buildConfig {
        className("BuildConfig")
        packageName("net.flectone.pulse")
        rootProject.ext.properties.findAll { key, value ->
            !key.contains('.')
        }.each { key, value ->
            buildConfigField(String, key.toString().toUpperCase(), value)
        }
    }

    downgradeJar {
        quiet = true
        downgradeTo = JavaVersion.VERSION_17
    }

    tasks.withType(Javadoc).configureEach {
        enabled = false
    }

    tasks.register("buildFinalJar", Jar) {
        archiveFileName = "FlectonePulse-" + project.name + "-" + project_version + ".jar"
        destinationDirectory = file("$rootDir/build/libs")
    }

    shadowJar {
        relocate("com.zaxxer.hikari", "net.flectone.pulse.library.hikari")
        relocate("org.jdbi", "net.flectone.pulse.library.jdbi3")
        relocate("com.alessiodp.libby", "net.flectone.pulse.library.libby")
        relocate("spark.", "net.flectone.pulse.library.spark.")
        relocate("org.apache.commons", "net.flectone.pulse.library.apache")
        relocate("com.fasterxml.jackson", "net.flectone.pulse.library.jackson")
        relocate("tools.jackson", "net.flectone.pulse.library.jackson")
        relocate("org.snakeyaml.engine", "net.flectone.pulse.library.snakeyaml")

        from(rootProject.file('LICENSE')) {
            into('/')
        }

        if (!project.name.is("fabric")) {
            relocate("com.github.retrooper.packetevents", "net.flectone.pulse.library.packetevents")
            relocate("io.github.retrooper.packetevents", "net.flectone.pulse.library.packetevents.impl")
            relocate("org.incendo", "net.flectone.pulse.library.cloud")
        }

        if (!project.name.is("paper") && !project.name.is("fabric")) {
            relocate("net.kyori.adventure", "net.flectone.pulse.library.adventure")
        }

        if (!project.name.is("velocity")) {
            relocate("com.google.inject", "net.flectone.pulse.library.guice")
            relocate("com.google.common", "net.flectone.pulse.library.guava")
        }

        outputs.cacheIf { true }

        minimize()
    }

    mavenPublishing {
        publishToMavenCentral(true)
        coordinates("net.flectone", "flectonepulse-${project.name}", project_version)

        pom {
            name = "FlectonePulse ${project.name.capitalize()}"
            description = "Every Message Under Your Control!"
            url = 'https://github.com/Flectone/FlectonePulse'
            licenses {
                license {
                    name = 'GNU General Public License v3.0'
                    url = 'https://www.gnu.org/licenses/gpl-3.0'
                }
            }

            developers {
                developer {
                    id = 'TheFaser'
                    name = 'TheFaser'
                    email = 'artem.mochalin11@gmail.com'
                }
            }

            scm {
                connection = 'scm:git:https://github.com/Flectone/FlectonePulse.git'
                developerConnection = 'scm:git:ssh://github.com/Flectone/FlectonePulse.git'
                url = 'https://github.com/Flectone/FlectonePulse'
            }
        }

        signAllPublications()
    }

}