plugins {
    id "com.github.johnrengelman.shadow"
    id 'com.adarshr.test-logger' version '3.2.0'
    id "modernfix.mod-common-conventions"
    id "modernfix.platform-conventions"
}

architectury {
    platformSetupLoomIde()
    fabric()
}

configurations {
    common
    shadowCommon // Don't use shadow from the shadow plugin since it *excludes* files.
    compileClasspath.extendsFrom common
    runtimeClasspath.extendsFrom common

    modIncludeImplementation

    include.extendsFrom modIncludeImplementation
    modImplementation.extendsFrom modIncludeImplementation

    testAgent {
        canBeConsumed = false
    }
}

dependencies {
    modImplementation "net.fabricmc:fabric-loader:${rootProject.fabric_loader_version}"
    testImplementation "net.fabricmc:fabric-loader-junit:${rootProject.fabric_loader_version}"
    annotationProcessor("io.github.llamalad7:mixinextras-fabric:${rootProject.mixinextras_version}")

    modCompileOnly(fabricApi.module("fabric-api-base", rootProject.fabric_api_version)) { exclude group: 'net.fabricmc', module: 'fabric-loader' }
    modCompileOnly(fabricApi.module("fabric-screen-api-v1", rootProject.fabric_api_version)) { exclude group: 'net.fabricmc', module: 'fabric-loader' }
    modCompileOnly(fabricApi.module("fabric-command-api-v2", rootProject.fabric_api_version)) { exclude group: 'net.fabricmc', module: 'fabric-loader' }
    modCompileOnly(fabricApi.module("fabric-model-loading-api-v1", rootProject.fabric_api_version)) { exclude group: 'net.fabricmc', module: 'fabric-loader' }
    modCompileOnly(fabricApi.module("fabric-models-v0", "0.84.0+1.20.1")) { exclude group: 'net.fabricmc', module: 'fabric-loader' }
    modCompileOnly(fabricApi.module("fabric-resource-loader-v0", rootProject.fabric_api_version)) { exclude group: 'net.fabricmc', module: 'fabric-loader' }
    modCompileOnly(fabricApi.module("fabric-data-generation-api-v1", rootProject.fabric_api_version)) { exclude group: 'net.fabricmc', module: 'fabric-loader' }
    if(project.use_fabric_api_at_runtime.toBoolean()) {
        modImplementation("com.terraformersmc:modmenu:${rootProject.modmenu_version}") { transitive false }
        modImplementation "curse.maven:spark-361579:${rootProject.spark_version}"
        modRuntimeOnly("net.fabricmc.fabric-api:fabric-api:${rootProject.fabric_api_version}") { exclude group: 'net.fabricmc', module: 'fabric-loader' }
    } else {
        modCompileOnly("com.terraformersmc:modmenu:${rootProject.modmenu_version}") { transitive false }
        modCompileOnly "curse.maven:spark-361579:${rootProject.spark_version}"
    }

    // Remove the next line if you don't want to depend on the API
    // modApi "me.shedaniel:architectury-fabric:${rootProject.architectury_version}"

    common(project(path: ":common", configuration: "namedElements")) { transitive false }
    testImplementation(shadowCommon(project(path: ":common", configuration: "transformProductionFabric"))) { transitive false }
    shadowCommon(project(path: ":annotations"))

    testImplementation(platform("org.junit:junit-bom:${project.junit_version}"))
    testImplementation("org.junit.jupiter:junit-jupiter-api")
    testImplementation("org.junit.jupiter:junit-jupiter-params")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
    testImplementation("org.junit.platform:junit-platform-launcher")
    testImplementation("org.assertj:assertj-core:3.19.0")
    testImplementation("com.google.guava:guava-testlib:21.0")
    testImplementation("org.mockito:mockito-junit-jupiter:5.3.1")

    testAgent(project("path": ":test_agent", "configuration": "agentJar"))
}

tasks.named("test") {
    useJUnitPlatform()
    def runDir = file('test_run')
    doFirst() {
        runDir.mkdir()
    }
    workingDir = runDir
    systemProperty 'modernfix.ignoreConfigForTesting', 'true'

    // inject our custom agent to fix #817
    FileCollection agentFile = configurations.getByName("testAgent")
    jvmArgs "-javaagent:${agentFile.singleFile.absolutePath}"
    dependsOn(agentFile)
}

processResources {
    inputs.property "version", project.version

    filesMatching("fabric.mod.json") {
        expand "version": project.version
    }
}

shadowJar {
    exclude "architectury.common.json"

    configurations = [project.configurations.shadowCommon]
    archiveClassifier.set("dev-shadow")
}

remapJar {
    injectAccessWidener = true
    input.set shadowJar.archiveFile
    dependsOn shadowJar
    archiveClassifier.set(null)
}

jar {
    archiveClassifier.set("dev")
}

components.java {
    withVariantsFromConfiguration(project.configurations.shadowRuntimeElements) {
        skip()
    }
}

publishing {
    publications {
        mavenFabric(MavenPublication) {
            artifactId = rootProject.archives_base_name + "-" + project.name
            from components.java
        }
    }

    // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing.
    repositories {
        // Add repositories to publish to here.
    }
}
