plugins {
    id 'java-library'
    id 'eclipse'
    id 'idea'
    id 'maven-publish'
    id 'net.neoforged.gradle.userdev' version '7.0.97'
    id 'net.neoforged.gradle.mixin' version '7.0.97'
}

def stdout = new ByteArrayOutputStream()
exec {
    commandLine 'git', 'rev-parse', '--short', 'HEAD'
    standardOutput = stdout
}
def commit = stdout.toString().replace("\n", "").replace("\r", "").trim()

version = project.hasProperty("dev") ? commit : mod_version

repositories {
    mavenLocal()
}

base {
//    todo: only for main jar, not api jar.
    archivesName = project.projectDir.name + "-mc${minecraft_version}-api${api_version}"
}

java.toolchain.languageVersion = JavaLanguageVersion.of(17)

minecraft.accessTransformers.file rootProject.file('src/api/resources/META-INF/accesstransformer.cfg')

runs {
    configureEach {
        systemProperty 'forge.logging.markers', 'REGISTRIES'
        systemProperty 'forge.logging.console.level', 'debug'

        modSource project.sourceSets.main
    }

    client {
        systemProperty 'forge.enabledGameTestNamespaces', 'esteemedinnovation'
    }

    server {
        systemProperty 'forge.enabledGameTestNamespaces', 'esteemedinnovation'
        programArgument '--nogui'
    }

    gameTestServer {
        systemProperty 'forge.enabledGameTestNamespaces', 'esteemedinnovation'
    }

    data {
        programArguments.addAll '--mod', 'esteemedinnovation', '--all', '--output', file('src/generated/resources/').getAbsolutePath(),
                '--existing', file('src/main/resources/').getAbsolutePath()
    }
}

sourceSets {
    api {
        java {
            srcDirs = ['src/api/java']
        }
        resources {
            srcDirs = ['src/api/resources']
        }
    }
    main {
        resources {
            srcDirs += 'src/generated/resources'
        }
        compileClasspath += api.output
        runtimeClasspath += api.output
    }
    test {
        resources.srcDirs = []
        compileClasspath += api.output
        runtimeClasspath += api.output
    }
}

dependencies {
    apiImplementation "net.neoforged:neoforge:${neoforge_version}"
    implementation "net.neoforged:neoforge:${neoforge_version}"
    testImplementation 'junit:junit:4.12'
}

subsystems {
    parchment {
        minecraftVersion = parchment_mc_version
        mappingsVersion = parchment_mappings_version
    }
}

tasks.withType(ProcessResources).configureEach {
    var replaceProperties = [
            minecraft_version: minecraft_version, minecraft_version_range: minecraft_version_range,
            neo_version: neoforge_version, neo_version_range: neoforge_version_range,
            loader_version_range: loader_version_range,
            mod_license: "CC0",
            mod_authors: "Esteemed Innovation Team",
    ]
    if (it.name == 'processApiResources') {
        replaceProperties.mod_id = api_mod_id
        replaceProperties.mod_name = api_mod_name
        replaceProperties.mod_version = api_version
        replaceProperties.mod_description = api_description
    } else {
        replaceProperties.mod_id = mod_mod_id
        replaceProperties.mod_name = mod_mod_name
        replaceProperties.mod_version = mod_version
        replaceProperties.mod_description = mod_description
    }
    inputs.properties replaceProperties

    filesMatching(['META-INF/mods.toml', 'pack.mcmeta']) {
        expand replaceProperties + [project: project]
    }
}

tasks.withType(JavaCompile).configureEach {
    options.encoding = 'UTF-8'
}

// TODO: Automate for when a new API package is built/published.
javadoc {
    source = sourceSets.api.allJava
    destinationDir = file('./docs/')
    options.memberLevel = JavadocMemberLevel.PRIVATE
    title = 'Esteemed Innovation API version ' + api_version
}

task apiJar(type: Jar) {
    duplicatesStrategy(DuplicatesStrategy.FAIL)
    archiveVersion = api_version
    archiveClassifier.set('api')
    from sourceSets.api.output
}

jar {
    dependsOn(classes, apiClasses)
    duplicatesStrategy(DuplicatesStrategy.FAIL)
    //todo
}

artifacts {
    archives apiJar
    archives jar
}

publishing {
    publications {
        apiPublication(MavenPublication) {
            artifact apiJar
        }
    }
}