plugins {
    id 'multiloader-loader'
    id 'net.neoforged.moddev.legacyforge'
}

mixin {
    add(sourceSets.main, "${mod_id}.refmap.json")

    config("${mod_id}.mixins.json")
    config("${mod_id}.forge.mixins.json")
}
def commonResources = project(':common').file('src/main/resources/').getAbsolutePath()
def forgeResources = file('src/main/resources/').getAbsolutePath()
def forgeBuildResources = file('build/resources/main/').getAbsolutePath()
def commonBuildResources = project(':common').file('build/resources/main/').getAbsolutePath()
def generatedOutput = project(':common').file('src/generated/resources/').getAbsolutePath()

legacyForge {
    version = "${minecraft_version}-${forge_version}"

    validateAccessTransformers = true

    def at = project(':common').file('src/main/resources/META-INF/accesstransformer.cfg')
    def generated = project(':common').file('src/generated/resources/')
    if (at.exists()) {
        accessTransformers = ["src/main/resources/META-INF/accesstransformer.cfg"]
    }
    parchment {
        minecraftVersion = parchment_minecraft
        mappingsVersion = parchment_version
    }
    runs {
        client {
            client()
            devLogin = true
            programArguments.addAll(
                    '--mod', project.mod_id,
                    '--all',
                    '--existing', forgeResources,
                    '--existing', commonResources,
                    '--existing', forgeBuildResources,
                    '--existing', commonBuildResources
            )
        }
        data {
            data()

            // 使用之前定义的变量
            programArguments.addAll(
                    '--mod', project.mod_id,
                    '--all',
                    '--output', generatedOutput,
                    '--existing', forgeResources,
                    '--existing', commonResources,
                    '--existing', forgeBuildResources,
                    '--existing', commonBuildResources
            )
        }
        server {
            server()
        }
    }

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

sourceSets.main.resources.srcDir project(':common').file('src/generated/resources')

dependencies {
    compileOnly project(":common")
    annotationProcessor("org.spongepowered:mixin:0.8.5-SNAPSHOT:processor")
}

jar {
    finalizedBy('reobfJar')
    manifest.attributes([
            "MixinConfigs": "${mod_id}.mixins.json,${mod_id}.forge.mixins.json"
    ])
}

// 配置sourceJar任务
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
    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
    dependsOn project(':common').tasks.named('javadoc')  // 显式依赖common的javadoc

    archiveClassifier.set('javadoc')
    from javadoc.destinationDir
    from project(':common').javadoc.destinationDir
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}

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

// 处理reobf
tasks.named('reobfJar') {
    dependsOn tasks.named('sourcesJar')
    dependsOn tasks.named('javadocJar')
}

// 发布配置
publishing {
    publications {
        mavenJava(MavenPublication) {
            artifactId = "${mod_id}-forge-${minecraft_version}"
            artifacts.clear()
            artifact(tasks.named('reobfJar').get()) {
                builtBy tasks.named('reobfJar')
            }
            artifact(tasks.named('sourcesJar').get()) {
                builtBy tasks.named('sourcesJar')
                classifier = 'sources'
            }
            artifact(tasks.named('javadocJar').get()) {
                builtBy tasks.named('javadocJar')
                classifier = 'javadoc'
            }
            pom {
                name = mod_name
                description = project.description ?: "default"
                developers {
                    developer {
                        id = mod_author
                        name = mod_author
                    }
                }
            }
        }
    }
}

// 处理资源
processResources {
    from project(':common').sourceSets.main.resources
    inputs.property "version", project.version
    inputs.property "minecraft_version", minecraft_version
    inputs.property "forge_version", forge_version
    inputs.property "mod_id", mod_id
    inputs.property "mod_name", mod_name
    inputs.property "description", description
    inputs.property "mod_author", mod_author

    filesMatching(['META-INF/mods.toml', 'pack.mcmeta']) {
        expand([
                version: project.version,
                minecraft_version: minecraft_version,
                forge_version: forge_version,
                mod_id: mod_id,
                mod_name: mod_name,
                description: description,
                mod_author: mod_author
        ])
    }
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
