import java.nio.file.Paths

plugins {
    id 'multiloader-loader'
    id 'fabric-loom'
}

repositories {
    maven { url 'https://maven.aliyun.com/repository/central' }

    if (System.getenv().containsKey("SR_DEV")) {
        maven {
            name "minecraftdevRepository"
            url "http://127.0.0.1:8080/releases"
        }
    }
    maven {
        url "https://cursemaven.com"
        content {
            includeGroup "curse.maven"
        }
    }
    maven {
        name = "Terraformers"
        url = "https://maven.terraformersmc.com/"
    }
    maven {
        name = "Modrinth"
        url = "https://api.modrinth.com/maven"
        content {
            includeGroup "maven.modrinth"
        }
    }
}
String renderdocPath = Paths.get(rootProject.projectDir.getAbsolutePath(), "renderdoc").toString();

dependencies {

    minecraft "com.mojang:minecraft:${versionConfig.common.minecraftVersion}"
    mappings loom.layered {
        officialMojangMappings()
        parchment("org.parchmentmc.data:parchment-${versionConfig.common.parchmentVersion}@zip")
    }
    modImplementation "net.fabricmc:fabric-loader:${versionConfig.fabric.loaderVersion}"
    modImplementation "net.fabricmc.fabric-api:fabric-api:${versionConfig.fabric.apiVersion}"
    //依赖的mod
    modImplementation "dev.architectury:architectury-fabric:$versionConfig.common.architecturyApiVersion"
    modImplementation "me.shedaniel.cloth:cloth-config-fabric:$versionConfig.common.clothConfigVersion"
    modCompileOnly("com.terraformersmc:modmenu:$versionConfig.fabric.modmenuVersion")

    //依赖库
    implementation "io.github.spair:imgui-java-app:1.87.5"
    implementation "io.github.spair:imgui-java-binding:1.87.5"
    implementation "io.github.spair:imgui-java-lwjgl3:1.87.5"
    implementation "org.anarres:jcpp:1.4.14"
    implementation "org.antlr:antlr4-runtime:4.13.1"
    implementation "io.github.douira:glsl-transformer:2.0.1"
    implementation("org.lwjgl:lwjgl-vulkan:$versionConfig.common.lwjglVersion")
    include("org.lwjgl:lwjgl-vulkan:$versionConfig.common.lwjglVersion")
    include(implementation('com.electronwill.night-config:toml:3.6.0'))
    include(implementation('com.electronwill.night-config:core:3.6.0'))

    implementation(files(Paths.get(renderdocPath, "render-nurse.jar").toString()))

    //额外库/模组
    for (lib in versionConfig.fabric.dependencies.modrinth) {
        def depName = "maven.modrinth:${lib.name}:${lib.version}-fabric,${versionConfig.common.minecraftVersion}"
        if (lib.compileOnly) {
            modCompileOnly(depName)
        } else {
            modImplementation(depName)
        }
    }
    for (lib in versionConfig.fabric.dependencies.local) {
        if (lib.isMod) {
            if (lib.compileOnly) {
                modCompileOnly(files("../libs/" + lib.name + ".jar"))
            } else {
                modImplementation(files("../libs/" + lib.name + ".jar"))
            }
        } else {
            if (lib.compileOnly) {
                compileOnly(files("../libs/" + lib.name + ".jar"))
            } else {
                implementation(files("../libs/" + lib.name + ".jar"))
            }
        }
    }
}

loom {
    mixin {
        defaultRefmapName.set("${mod_id}.refmap.json")
    }
    runs {
        client {
            client()
            environmentVariables.put("ENABLE_VULKAN_RENDERDOC_CAPTURE", "1")
            vmArgs.add("-XX:+CreateMinidumpOnCrash")
            //vmArgs.add("-javaagent:" + Paths.get(renderdocPath, "render-nurse.jar"))
            vmArgs.add("--enable-preview")
            vmArgs.add("--enable-native-access=ALL-UNNAMED")

            vmArgs.add("-Dneoforge.rendernurse.renderdoc.library=" + Paths.get(renderdocPath, "renderdoc.dll"))
            setConfigName('Fabric Client')
            ideConfigGenerated(true)
            runDir('../runs/fabric')
        }
    }
}

processResources {
    inputs.property 'version', project.version
    filesMatching('fabric.mod.json') {
        expand(
                version: project.version,
                javaVersion: versionConfig.common.javaVersion
        )
        filter { String line ->
            return line.replace('"{versionRange}"', versionConfig.common.fabricVersionRange.size() == 1 ?
                    '"' + versionConfig.common.fabricVersionRange.get(0) + '"' :
                    '[' + versionConfig.common.fabricVersionRange.stream().map {
                        return '"' + it + '"'
                    }.toList().join(",") + ']'
            )
        }
    }
}