plugins {
    id "java"
    id "java-library"
    id "net.neoforged.gradle.userdev" version '7.0.57'
    id "com.diffplug.spotless" version "6.1.2"
    id "com.github.node-gradle.node" version "3.0.1"
    id "com.modrinth.minotaur" version "2.+"
    id "com.matthewprenger.cursegradle" version "1.4.0"
    id "com.github.johnrengelman.shadow" version "7.1.2"
}

group = "de.bluecolored.bluemap.forge"
version = System.getProperty("bluemap.version") ?: "?" // set by BlueMapCore

repositories {
    mavenLocal()
    mavenCentral()
    maven {
        setUrl("https://libraries.minecraft.net")
    }
    maven {
        setUrl("https://jitpack.io")
    }
}

base {
    archivesName = "bluemap"
}

java.toolchain.languageVersion = JavaLanguageVersion.of(17)
sourceSets.main.resources { srcDir 'src/generated/resources' }

configurations {
    implementation.extendsFrom(shadowInclude)
}

dependencies {
    implementation "net.neoforged:neoforge:20.2.86"

    shadowInclude ("de.bluecolored.bluemap.common:BlueMapCommon") {
        //exclude dependencies provided by forge
        exclude (group: "com.google.guava", module: "guava")
        exclude (group: "com.google.code.gson", module: "gson")
        exclude (group: "org.apache.commons", module: "commons-lang3")
        exclude (group: "commons-io", module: "commons-io")
        exclude (group: "com.mojang", module: "brigadier")
    }

    testImplementation ("org.junit.jupiter:junit-jupiter:5.8.2")
    testRuntimeOnly ("org.junit.jupiter:junit-jupiter-engine:5.8.2")
}

tasks.withType(ProcessResources).configureEach {
    var replaceProperties = [
            minecraft_version: minecraft_version,
            minecraft_version_range: minecraft_version_range,
            neo_version: neo_version,
            neo_version_range: neo_version_range,
            loader_version_range: loader_version_range,
            mod_id: mod_id,
            mod_name: mod_name,
            mod_license: mod_license,
            mod_version: version,
            mod_description: mod_description,
            pack_format_number: pack_format_number,
    ]
    inputs.properties replaceProperties

    filesMatching(['META-INF/mods.toml', 'pack.mcmeta']) {
        expand replaceProperties + [project: project]
    }
}

tasks.withType(JavaCompile).configureEach {
    options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation
}

tasks.withType(AbstractArchiveTask).configureEach {
    setReproducibleFileOrder(true)
    setPreserveFileTimestamps(false)
}

spotless {
    java {
        target ("src/*/java/**/*.java")

        licenseHeaderFile("../../HEADER")
        indentWithSpaces()
        trimTrailingWhitespace()
    }
}

test {
    useJUnitPlatform()
}

shadowJar {
    destinationDirectory.set(file("../../build/release"))
    archiveFileName.set("BlueMap-${project.version}-${project.name}.jar")

    configurations = [project.configurations.shadowInclude]

    //relocate ("com.flowpowered.math", "de.bluecolored.shadow.flowpowered.math") //DON"T relocate this, because the API depends on it
    relocate ("com.typesafe.config", "de.bluecolored.shadow.typesafe.config")
    relocate ("de.bluecolored.bluenbt", "de.bluecolored.shadow.bluenbt")
    relocate ("org.spongepowered.configurate", "de.bluecolored.shadow.configurate")
    relocate ("com.github.benmanes.caffeine", "de.bluecolored.shadow.benmanes.caffeine")
    relocate ("org.aopalliance", "de.bluecolored.shadow.aopalliance")
    relocate ("javax.inject", "de.bluecolored.shadow.javax.inject")
    relocate ("org.checkerframework", "de.bluecolored.shadow.checkerframework")
    relocate ("org.codehaus", "de.bluecolored.shadow.codehaus")
    relocate ("io.leangen.geantyref", "de.bluecolored.shadow.geantyref")
    relocate ("io.airlift", "de.bluecolored.shadow.airlift")
    relocate ("net.jpountz", "de.bluecolored.shadow.jpountz")

    relocate ("com.google.errorprone", "de.bluecolored.shadow.google.errorprone")
    relocate ("com.google.inject", "de.bluecolored.shadow.google.inject")

    relocate ("org.apache.commons.dbcp2", "de.bluecolored.shadow.apache.commons.dbcp2")
    relocate ("org.apache.commons.logging", "de.bluecolored.shadow.apache.commons.logging")
    relocate ("org.apache.commons.pool2", "de.bluecolored.shadow.apache.commons.pool2")
}

task release {
    dependsOn(shadowJar)
}

modrinth {
    token = System.getenv("MODRINTH_TOKEN")
    projectId = "swbUV1cr"
    versionNumber = "${project.version}-${project.name}"
    changelog = file("../../release.md")
            .getText()
            .replace("{version}", project.version.toString())
    uploadFile = shadowJar
    loaders = ["neoforge"]
    gameVersions = ["1.20.2", "1.20.3", "1.20.4"]
}

curseforge {
    apiKey = System.getenv("CURSEFORGE_TOKEN") ?: ""
    project {
        id = "406463"
        changelogType = "markdown"
        changelog = file("../../release.md")
                .getText()
                .replace("{version}", project.version.toString())
        releaseType = "release"

        addGameVersion "NeoForge"

        addGameVersion "Java 18"
        addGameVersion "Java 17"

        addGameVersion "1.20.2"
        addGameVersion "1.20.3"
        addGameVersion "1.20.4"

        mainArtifact shadowJar
    }
    options {
        javaVersionAutoDetect = false
        javaIntegration = false
        forgeGradleIntegration = false
    }
}

task publish {
    dependsOn(tasks.findByName("modrinth"))
    dependsOn(tasks.findByName("curseforge"))
}
