plugins {
    id 'eclipse'
    id 'idea'
    id 'net.minecraftforge.gradle' version '[6.0.16,6.2)'
    // IF forge-1.16.5
//    id 'com.github.johnrengelman.shadow' version '8.1.1'
    // END IF
}

def getVersionFromFile(String fileName) {
    def versionFile = file(fileName)
    if (versionFile.exists()) {
        return versionFile.text.trim()
    } else {
        throw new Exception("File not found: ${versionFile.absolutePath}") as Throwable
    }
}

def tool_version = getVersionFromFile('../../tool_version.txt')

def mod_version = getVersionFromFile('../../version.txt')

group = mod_group_id
version = mod_version

base {
    archivesName = "${mod_name}-forge+${minecraft_version}"
}

java {
    toolchain.languageVersion = JavaLanguageVersion.of(java_version)
}

minecraft {
    mappings channel: mapping_channel, version: minecraft_version
    copyIdeResources = true
    runs {
        configureEach {
            workingDirectory project.file('run')
            property 'forge.logging.markers', 'REGISTRIES'
            property 'forge.logging.console.level', 'debug'

            mods {
                "${mod_id}" {
                    source sourceSets.main
                }
            }
        }

        client {
            property 'forge.enabledGameTestNamespaces', mod_id
        }

        server {
            property 'forge.enabledGameTestNamespaces', mod_id
            args '--nogui'
        }
        gameTestServer {
            property 'forge.enabledGameTestNamespaces', mod_id
        }

        data {
            workingDirectory project.file('run-data')

            args '--mod', mod_id, '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/')
        }
    }
}

sourceSets.main.resources { srcDir 'src/generated/resources' }

repositories {
    mavenLocal()
    mavenCentral()
    maven {
        url = uri(maven_repository_url)
        credentials {
            username = System.getenv("GH_USERNAME")
            password = System.getenv("PACKAGE_READ_ONLY_TOKEN")
        }
    }
}

// IF > forge-1.16.5
//jarJar.enable()
//tasks.named('jarJar') {
//    archiveClassifier.set('')
//    destinationDirectory = file("../../QueQiao-jar/${mod_version}")
//}
// ELSE
//shadowJar {
//    archiveBaseName.set("${mod_name}-forge+${minecraft_version}")
//    archiveClassifier.set("")
//    destinationDirectory.set(file("../../QueQiao-jar/${version}"))
//    dependencies {
//        include(dependency("com.github.theword.queqiao:queqiao-tool:${tool_version}"))
//        include(dependency("org.java-websocket:Java-WebSocket:${java_websocket_version}"))
//        include(dependency("org.yaml:snakeyaml:${snakeyaml_version}"))
//        include(dependency("org.slf4j:slf4j-api"))
//        include(dependency("org.slf4j:slf4j-simple"))
//        include(dependency("org.glavo:rcon-java:${glavoRconVersion}"))
//    }
//    mergeServiceFiles()
//    relocate 'org.slf4j', 'shadow.org.slf4j'
//}
//reobf {
//    shadowJar { }
//}
//tasks.named('shadowJar') {
//    finalizedBy 'reobfShadowJar'
//}
//tasks.named('build') {
//    dependsOn 'shadowJar'
//}
// END IF

dependencies {
    minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}"

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

    minecraftLibrary("com.github.theword.queqiao:queqiao-tool:${tool_version}") {
        exclude group: 'org.slf4j', module: 'slf4j-api'
    }

    implementation("org.java-websocket:Java-WebSocket:${java_websocket_version}") {
        exclude group: 'org.slf4j', module: 'slf4j-api'
    }
    implementation "org.yaml:snakeyaml:${snakeyaml_version}"
    implementation "org.glavo:rcon-java:${glavoRconVersion}"
    // IF > forge-1.16.5
//    jarJar("com.github.theword.queqiao:queqiao-tool:${tool_version}") {
//        jarJar.ranged(it, "[${tool_version},)")
//        exclude group: 'org.slf4j', module: 'slf4j-api'
//    }
//
//    jarJar("org.java-websocket:Java-WebSocket:${project.java_websocket_version}") {
//        exclude group: 'org.slf4j', module: 'slf4j-api'
//        jarJar.ranged(it, "[${project.java_websocket_version},2.0.0)")
//    }
//
//    jarJar("org.yaml:snakeyaml:${project.snakeyaml_version}") {
//        jarJar.ranged(it, "[${project.snakeyaml_version},3.0)")
//    }
//
//    jarJar("org.glavo:rcon-java:${glavoRconVersion}") {
//        jarJar.ranged(it, "[${project.glavoRconVersion},4.0)")
//    }
    // ELSE
//    implementation "org.slf4j:slf4j-api:${slf4j_version}"
//    implementation "org.slf4j:slf4j-simple:${slf4j_version}"
    // END IF
    testImplementation("org.junit.jupiter:junit-jupiter-api:${junit_jupiter_version}")
    testImplementation("org.junit.jupiter:junit-jupiter-engine:${junit_jupiter_version}")
}

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
    }
}

tasks.named('processResources', ProcessResources).configure {
    var replaceProperties = [
            minecraft_version   : minecraft_version, minecraft_version_range: minecraft_version_range,
            forge_version       : forge_version, forge_version_range: forge_version_range,
            loader_version_range: forge_version_range,
            mod_id              : mod_id, mod_name: mod_name, mod_license: mod_license, mod_version: mod_version,
            mod_authors         : mod_authors, mod_description: mod_description,
    ]

    inputs.properties replaceProperties

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

tasks.named('jar', Jar).configure {
    manifest {
        attributes([
                "Specification-Title"     : mod_id,
                "Specification-Vendor"    : mod_authors,
                "Specification-Version"   : "1", // We are version 1 of ourselves
                "Implementation-Title"    : project.name,
                "Implementation-Version"  : project.jar.archiveVersion,
                "Implementation-Vendor"   : mod_authors,
                "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ")
        ])
    }
    finalizedBy 'reobfJar'
}

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