plugins {
    id 'fabric-loom' version '1.10-SNAPSHOT'
    id 'maven-publish'
}

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')

version = mod_version
group = project.maven_group

base {
    archivesName = project.mod_name
}

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

dependencies {
    // To change the versions see the gradle.properties file
    minecraft "com.mojang:minecraft:${project.minecraft_version}"
    mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
    modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

    // Fabric API. This is technically optional, but you probably want it anyway.
    modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

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

    include modImplementation("com.github.theword.queqiao:queqiao-tool:${tool_version}")
    include modImplementation("org.java-websocket:Java-WebSocket:${project.java_websocket_version}")
    include modImplementation("org.yaml:snakeyaml:${project.snakeyaml_version}")
    include modImplementation("org.glavo:rcon-java:${project.glavoRconVersion}")
    // IF <= fabric-1.16.5
//    include modImplementation("org.slf4j:slf4j-api:${slf4j_version}")
//    include modImplementation("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"
    }
}

processResources {
    inputs.property "version", project.version
    inputs.property "minecraft_version", project.minecraft_version
    inputs.property "loader_version", project.loader_version
    filteringCharset "UTF-8"

    filesMatching("fabric.mod.json") {
        expand "version": project.version,
                "minecraft_version": project.minecraft_version,
                "loader_version": project.loader_version
    }
}

def targetJavaVersion = Integer.parseInt(project.java_version)
tasks.withType(JavaCompile).configureEach {
    it.options.encoding = "UTF-8"
    if (targetJavaVersion >= 8 || JavaVersion.current().isJava8Compatible()) {
        it.options.release.set(targetJavaVersion)
    }
}

java {
    def javaVersion = JavaVersion.toVersion(targetJavaVersion)
    if (JavaVersion.current() < javaVersion) {
        toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion)
    }
    withSourcesJar()
}

jar {
    from("LICENSE") {
        rename { "${it}_${project.archivesBaseName}" }
    }
}

remapJar {
    destinationDirectory  = file("../../QueQiao-jar/${mod_version}")
    archiveBaseName = "${mod_name}-fabric+${minecraft_version}"
}