/* Define the gradle plugins. */
plugins {
    id "java"
    id "idea"

    // Fabric Loom
    id "fabric-loom" version "1.11-SNAPSHOT"
}

/* Define the jar repositories. */
repositories {
    mavenCentral()

    // For fabric-carpet mod.
    maven {url 'https://masa.dy.fi/maven'}

    // For text-placeholder and sgui mod.
    maven { url "https://maven.nucleoid.xyz" }
}

/* Define AW files merge function. */
ext.mergedAw = project(":common").file("build/resources/main/merged.fuji.accesswidener")
def mergeAccessWidenerFiles() {
    def commonAw = project(":common").file("src/main/resources/common.fuji.accesswidener")
    def versionAw = project(":common").file("src/main/resources/${access_widener_version}.fuji.accesswidener")

    mergedAw.parentFile.mkdirs()
    def versionContent = versionAw.text
    def commonContent = commonAw.text
    mergedAw.text = versionContent + "\n" + commonContent + "\n"
}
tasks.register("mergeAccessWidener") {
    doLast {
        mergeAccessWidenerFiles()
    }
}
tasks.copyAccessWidenerResources.dependsOn(mergeAccessWidener)

/* Configure fabric-loom plugin. */
loom {
    mergeAccessWidenerFiles()
    accessWidenerPath = mergedAw

    mods {
        "${project.mod_id}" {
            sourceSet sourceSets.main
        }
    }

    // set the runServer() jvm args
    runs {
        configureEach {
            vmArgs("-Xms8G -Xmx8G -XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -XX:G1NewSizePercent=40 -XX:G1MaxNewSizePercent=50 -XX:G1HeapRegionSize=16M -XX:G1ReservePercent=15 -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4 -XX:InitiatingHeapOccupancyPercent=15 -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem -XX:MaxTenuringThreshold=1 -XX:+UseLargePages -XX:+UseTransparentHugePages -Duser.timezone=GMT+08 -Dmixin.debug.export=false".split(" "))
        }

        client {
            client()
            setConfigName("Fabric Client")
            ideConfigGenerated(true)
            runDir("../run/client")
        }

        server {
            server()
            setConfigName("Fabric Server")
            ideConfigGenerated(true)
            runDir("../run/server")
        }
    }
}

/**
 * Each time adds a new library, you should build the software, and go to the production-env to check whether the library works.
 * It's possible the necessary libraries are not packed into the release package, even you `include` the library ...
 */
dependencies {
    /* For mod loader: fabric-loader */
    minecraft "com.mojang:minecraft:${project.minecraft_version}"
    mappings "net.fabricmc:yarn:${project.yarn_mappings_version}"
    modImplementation "net.fabricmc:fabric-loader:${project.fabric_loader_version}"

    /* For fabric-carpet mod */
    modImplementation("carpet:fabric-carpet:${project.carpet_core_version}")

    /* For json parser: gson */
    implementation "com.google.code.gson:gson:${project.gson_version}"
    implementation "com.google.guava:guava:${project.guava_version}"
    include(implementation("com.jayway.jsonpath:json-path:${project.json_path_version}"))

    /* For permission system: luckperms */
    include(implementation "net.luckperms:api:${project.luckperms_api_version}")

    /* For server side gui: sgui*/
    include(modImplementation("eu.pb4:sgui:${project.sgui_version}"))
    include(modImplementation("eu.pb4:placeholder-api:${project.placeholder_api_version}"))

    /* For economy system: common economy api */
    include(modImplementation("eu.pb4:common-economy-api:${project.common_economy_api_version}"))

    /* For scheduler: quartz */
    include(implementation "org.quartz-scheduler:quartz:${project.quartz_version}")

    /* For testing: fabric-loader-junit */
    testImplementation "net.fabricmc:fabric-loader-junit:${project.fabric_loader_version}"

    /* For evaluator module. */
    include(implementation(project(":evaluator")))
}


java {
    sourceCompatibility = JavaVersion.VERSION_21
    targetCompatibility = JavaVersion.VERSION_21
}

/* Replace the version in fabric.mod.json file */
processResources {
    // NOTE The fabric.mod.json file will always be copied to fabric/build/resources/main dir.
    dependsOn(copyCommonResources)
    dependsOn(copyLanguageResources)
    dependsOn(copyAccessWidenerResources)
}

/* Validate the access widener file. */
tasks.compileJava.dependsOn(validateAccessWidener)

/* Ensure the file is not locked. */
tasks.register('deleteWorldLockFile') {
    outputs.upToDateWhen { false }

    doFirst {
        def file = project.rootProject.file("run/server/world/session.lock")
        println("Delete runtime world session.lock file: " + file)
        delete(file)
    }
}
tasks.runServer.dependsOn(deleteWorldLockFile)

/* Ensure the language files are up to date. */
tasks.register('deleteLanguageFiles') {
    outputs.upToDateWhen { false }

    doFirst {
        def file = project.rootProject.file("run/server/config/fuji/${languageDirectoryName}/")
        println("Delete runtime language files: " + file)
        delete(file)
    }
}
tasks.runServer.dependsOn(deleteLanguageFiles)

/* Re-generate graph (Actually, the graph files should be generated before process the resources) */
tasks.runServer.dependsOn(check)
