import net.fabricmc.loom.task.RemapJarTask

plugins {
    id 'multiloader-loader'
    id 'fabric-loom'
}
dependencies {
    minecraft "com.mojang:minecraft:${minecraft_version}"
    mappings loom.layered {
        officialMojangMappings()
        parchment("org.parchmentmc.data:parchment-${parchment_minecraft}:${parchment_version}@zip")
    }
    modImplementation "net.fabricmc:fabric-loader:${fabric_loader_version}"
    modImplementation "net.fabricmc.fabric-api:fabric-api:${fabric_version}"
    implementation group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.1'
    implementation project(":common")
}

loom {
    def commonResources = project(':common').file('src/main/resources/').getAbsolutePath().toString()
    def fabricResources = file('src/main/resources/').getAbsolutePath().toString()
    def fabricBuildResources = file('build/resources/main/').getAbsolutePath().toString()
    def commonBuildResources = project(':common').file('build/resources/main/').getAbsolutePath().toString()
    def generatedOutput = project(':common').file('src/generated/resources/').getAbsolutePath().toString()

    if (project(":common").file("src/main/resources/${mod_id}.accesswidener").exists()) {
        accessWidenerPath.set(project(":common").file("src/main/resources/${mod_id}.accesswidener"))
    }
    mixin {
        defaultRefmapName.set("${mod_id}.refmap.json")
    }
    runs {
        client {
            client()
            setConfigName("Fabric Client")
            ideConfigGenerated(true)
            runDir("run")
            programArgs "--username", "dev"
            def args = [
                    "-Dlib39.modid=${mod_id}".toString(),
                    "-Dlib39.output=${generatedOutput}".toString(),
                    "-Dlib39.existing.fabric=${fabricResources}".toString(),
                    "-Dlib39.existing.common=${commonResources}".toString(),
                    "-Dlib39.existing.fabricBuild=${fabricBuildResources}".toString(),
                    "-Dlib39.existing.commonBuild=${commonBuildResources}".toString()
            ]
            vmArgs.addAll(args)

            // 也可以添加JVM参数
            vmArgs.addAll(
                    "-Dfabric.log.level=info",
                    "-Dmixin.debug.export=true"
            )
        }

        server {
            server()
            serverWithGui()
            setConfigName("Fabric Server")
            ideConfigGenerated(true)
            runDir("run")

            def args = [
                    "-Dlib39.modid=${mod_id}".toString(),
                    "-Dlib39.output=${generatedOutput}".toString(),
                    "-Dlib39.existing.fabric=${fabricResources}".toString(),
                    "-Dlib39.existing.common=${commonResources}".toString(),
                    "-Dlib39.existing.fabricBuild=${fabricBuildResources}".toString(),
                    "-Dlib39.existing.commonBuild=${commonBuildResources}".toString()
            ]
            vmArgs.addAll(args)
        }
    }
}
tasks.named('sourcesJar', Jar) {
    dependsOn classes
    dependsOn project(':common').tasks.named('sourcesJar')  // 显式依赖common的source
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    archiveClassifier.set('sources')
    from sourceSets.main.allSource
    from project(':common').sourceSets.main.allSource
}

// 配置javadoc任务
tasks.named('javadoc', Javadoc) {
    source project(':common').sourceSets.main.allJava
    dependsOn project(':common').tasks.named('javadoc')  // 显式依赖common的javadoc
    source sourceSets.main.allJava
    classpath = configurations.compileClasspath
    classpath += project(':common').sourceSets.main.compileClasspath
    options.encoding = 'UTF-8'
    options.charSet = 'UTF-8'
    options.links("https://docs.oracle.com/en/java/javase/17/docs/api/")
    options.memberLevel = JavadocMemberLevel.PUBLIC
    options.addBooleanOption('Xdoclint:none', true)
    options.addStringOption('doctitle', "${mod_id} ${minecraft_version} ${version} Javadoc")
}

// 配置javadocJar任务
tasks.named('javadocJar', Jar) {
    dependsOn javadoc
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    archiveClassifier.set('javadoc')
    from javadoc.destinationDir
    from project(':common').javadoc.destinationDir
}

// 确保build任务包含所有需要的jar
tasks.named('build') {
    dependsOn tasks.named('sourcesJar')
    dependsOn tasks.named('javadocJar')
}

// 配置remap任务以包含sources和javadoc
remapJar {
    dependsOn tasks.named('sourcesJar')
    dependsOn tasks.named('javadocJar')
    inputFile.set(tasks.named('jar').get().archiveFile)
    addNestedDependencies = false
}

remapSourcesJar {
    dependsOn tasks.named('sourcesJar')
    inputFile.set(tasks.named('sourcesJar').get().archiveFile)
}

// 为javadocJar创建remap任务
tasks.register('remapJavadocJar', RemapJarTask) {
    dependsOn tasks.named('javadocJar')
    inputFile.set(tasks.named('javadocJar').get().archiveFile)
    archiveClassifier.set('javadoc')
    addNestedDependencies = false
}

// 将remapped artifacts添加到发布配置
publishing {
    publications {
        mavenJava(MavenPublication) {
            // 重置artifactsId
            artifactId = "${mod_id}-fabric-${minecraft_version}"
            artifacts.clear()
            // 手动添加需要的artifacts
            artifact(remapJar) {
                builtBy remapJar
            }
            artifact(remapSourcesJar) {
                builtBy remapSourcesJar
                classifier = 'sources'
            }
            artifact(remapJavadocJar) {
                builtBy remapJavadocJar
                classifier = 'javadoc'
            }
            pom {
                name = mod_name
                description = project.description ?: "default"
                developers {
                    developer {
                        id = mod_author
                        name = mod_author
                    }
                }
            }
        }

    }
}

tasks.named('generateMetadataFileForMavenJavaPublication') {
    dependsOn tasks.named('remapJavadocJar')
    dependsOn tasks.named('remapJar')
    dependsOn tasks.named('remapSourcesJar')
}

