plugins {
    id 'java-library'
    id 'maven-publish'
    id 'idea'
    id 'net.neoforged.moddev' version '2.0.78'
}

def getVersionFromFile(String fileName) {
    def versionFile = file(fileName)
    if (versionFile.exists()) {
        return versionFile.text.trim()
    } else {
        throw new Exception("File not found: ${versionFile.absolutePath}") as Throwable
    }
}

def tool_version = getVersionFromFile('../../tool_version.txt')

def mod_version = getVersionFromFile('../../version.txt')

version = mod_version
group = mod_group_id

repositories {
    mavenLocal()
    maven {
        url = uri(maven_repository_url)
        credentials {
            username = System.getenv("GH_USERNAME")
            password = System.getenv("PACKAGE_READ_ONLY_TOKEN")
        }
    }

}

base {
    archivesName = "${mod_name}-neoforge+${minecraft_version}"
}

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

neoForge {
    version = project.neo_version

    parchment {
        mappingsVersion = project.parchment_mappings_version
        minecraftVersion = project.parchment_minecraft_version
    }

    runs {
        client {
            client()
            systemProperty 'neoforge.enabledGameTestNamespaces', project.mod_id
        }

        server {
            server()
            programArgument '--nogui'
            systemProperty 'neoforge.enabledGameTestNamespaces', project.mod_id
        }

        gameTestServer {
            type = "gameTestServer"
            systemProperty 'neoforge.enabledGameTestNamespaces', project.mod_id
        }

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

        configureEach {
            systemProperty 'forge.logging.markers', 'REGISTRIES'
            logLevel = org.slf4j.event.Level.DEBUG
        }
    }

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

sourceSets.main.resources { srcDir 'src/generated/resources' }

dependencies {
    annotationProcessor "org.projectlombok:lombok:${lombok_version}"
    implementation "org.projectlombok:lombok:${lombok_version}"

    jarJar(implementation("com.github.theword.queqiao:queqiao-tool:${tool_version}"))
    additionalRuntimeClasspath "com.github.theword.queqiao:queqiao-tool:${tool_version}"

    jarJar(implementation("org.java-websocket:Java-WebSocket:${java_websocket_version}"))
    jarJar(implementation("org.yaml:snakeyaml:${snakeyaml_version}"))
    jarJar(implementation("org.glavo:rcon-java:${glavoRconVersion}"))

    testImplementation("com.google.code.gson:gson:${gson_version}")
    testImplementation("org.junit.jupiter:junit-jupiter-api:${junit_jupiter_version}")
    testImplementation("org.junit.jupiter:junit-jupiter-engine:${junit_jupiter_version}")
}

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
    }
}

var generateModMetadata = tasks.register("generateModMetadata", ProcessResources) {
    var replaceProperties = [
            minecraft_version      : minecraft_version,
            minecraft_version_range: minecraft_version_range,
            neo_version            : neo_version,
            neo_version_range      : neo_version_range,
            loader_version_range   : loader_version_range,
            mod_id                 : mod_id,
            mod_name               : mod_name,
            mod_license            : mod_license,
            mod_version            : mod_version,
            mod_authors            : mod_authors,
            mod_description        : mod_description
    ]
    inputs.properties replaceProperties
    expand replaceProperties
    from "src/main/templates"
    into "build/generated/sources/modMetadata"
}

sourceSets.main.resources.srcDir generateModMetadata
neoForge.ideSyncTask generateModMetadata

tasks.named('jar') {
    destinationDirectory = file("../../QueQiao-jar/${mod_version}")
}

idea {
    module {
        downloadSources = true
        downloadJavadoc = true
    }
}