import multiversion.BasePlatformConfig
import multiversion.Dependency

plugins {
}

def isNewVersion = versionConfig.common.minecraftVersion > '1.20.1'
if (isNewVersion) {
    plugins.apply('net.neoforged.moddev')
} else {
    plugins.apply('net.neoforged.moddev.legacyforge')
}

if (isNewVersion) {
    neoForge {
        neoFormVersion = versionConfig.common.neoFormVersion == null ? getCurrentNeoFormVersion() : versionConfig.common.neoFormVersion
        parchment {
            minecraftVersion = getCurrentVersionConfig().common.parchmentVersion.split(":")[0]
            mappingsVersion = getCurrentVersionConfig().common.parchmentVersion.split(":")[1]
        }
    }
} else {
    legacyForge {
        mcpVersion = "${versionConfig.common.minecraftVersion}"
        parchment {
            minecraftVersion = getCurrentVersionConfig().common.parchmentVersion.split(":")[0]
            mappingsVersion = getCurrentVersionConfig().common.parchmentVersion.split(":")[1]
        }
    }
}

repositories {
    if (System.getenv().containsKey("SR_DEV")) {
        maven {
            name "minecraftdevRepository"
            url "http://127.0.0.1:8080/releases"
        }
    }
    mavenCentral()
    maven { url 'https://maven.neoforged.net' }
    maven { url 'https://maven.aliyun.com/repository/central' }
    maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
    maven { url "https://maven.architectury.dev/" }
    maven { url "https://maven.nucleoid.xyz/" }
    maven { url "https://maven.shedaniel.me/" }
    maven { url "https://maven.neoforged.net/releases" }
    maven { url "https://libraries.minecraft.net" }
    maven { url "https://maven.fabricmc.net/" }
    maven { url 'https://repo.spongepowered.org/repository/maven-public' }
    maven { url 'https://maven.parchmentmc.org/' }
    maven { url 'https://maven.blamejared.com' }

    maven {
        name = "Modrinth"
        url = "https://api.modrinth.com/maven"
        content {
            includeGroup "maven.modrinth"
        }
    }
}

static Dependency findIris(BasePlatformConfig config) {
    if (config.dependencies) {
        if (config.dependencies.modrinth) {
            for (var dep : config.dependencies.modrinth) {
                if (dep.name.trim() == "iris" || dep.name.trim() == "oculus") {
                    return dep
                }
            }
        }
    }
    return null;
}

dependencies {
    compileOnly group: 'org.spongepowered', name: 'mixin', version: '0.8.5'
    compileOnly "io.github.spair:imgui-java-app:1.87.5"
    compileOnly "io.github.spair:imgui-java-binding:1.87.5"
    compileOnly "io.github.spair:imgui-java-lwjgl3:1.87.5"
    compileOnly "org.lwjgl:lwjgl-vulkan:$versionConfig.common.lwjglVersion"
    compileOnly 'com.electronwill.night-config:toml:3.6.0'
    compileOnly 'com.electronwill.night-config:core:3.6.0'
    // 没这玩意MixinPlugin会报错找不到ClassNode
    if (versionConfig.common.minecraftVersion <= "1.21.1") {
        compileOnly 'org.ow2.asm:asm:9.7.1'
        compileOnly 'org.ow2.asm:asm-tree:9.7.1'
    } else {
        compileOnly 'org.ow2.asm:asm:9.6'
        compileOnly 'org.ow2.asm:asm-tree:9.6'
    }


    if (isNewVersion) {
        compileOnly "dev.architectury:architectury-neoforge:$versionConfig.common.architecturyApiVersion"
        compileOnly "me.shedaniel.cloth:cloth-config-neoforge:$versionConfig.common.clothConfigVersion"
    } else {
        modCompileOnly "dev.architectury:architectury-forge:$versionConfig.common.architecturyApiVersion"
        modCompileOnly "me.shedaniel.cloth:cloth-config-forge:$versionConfig.common.clothConfigVersion"
    }

    //搜索Iris依赖
    Dependency irisDependency = null;
    String irisPlatform = null;

    if (versionConfig.neoforge && irisDependency == null) {
        irisDependency = findIris(versionConfig.neoforge);
        irisPlatform = "neoforge"
    }

    if (versionConfig.forge && irisDependency == null) {
        irisDependency = findIris(versionConfig.forge);
        irisPlatform = "forge"
    }

    if (versionConfig.fabric && irisDependency == null) {
        irisDependency = findIris(versionConfig.fabric);
        irisPlatform = "fabric"
    }

    if (irisDependency != null) {
        compileOnly "maven.modrinth:${irisDependency.name}:${irisDependency.version}-${irisPlatform},${versionConfig.common.minecraftVersion}"
    }
}

configurations {
    commonJava {
        canBeResolved = false
        canBeConsumed = true
    }
    commonResources {
        canBeResolved = false
        canBeConsumed = true
    }
}

artifacts {
    commonJava sourceSets.main.java.sourceDirectories.singleFile
    commonResources sourceSets.main.resources.sourceDirectories.singleFile
}

tasks.register("genJNIHeader", Exec) {
    workingDir(rootProject.projectDir)
    def outputDir = file("../native/cpp/SRNativeMain/include").absolutePath
    def sourceFile = file("../common/src/main/java/io/homo/superresolution/core/SuperResolutionNative.java").absolutePath
    def sourceDirs = sourceSets.main.java.srcDirs.join(File.pathSeparator)
    def classpath = configurations.compileClasspath.asPath
    commandLine(
            "javac",
            "-h", outputDir,
            "-sourcepath", sourceDirs,
            "-classpath", classpath,
            "-encoding", "UTF-8",
            sourceFile
    )

    doLast {
        delete(fileTree(
                dir: file("../common/src/main/java").absolutePath,
                includes: ["**/*.class"],
                excludes: ["**/*.java"]
        ))
    }
}

processResources {

}