plugins {
    id 'java'
    id 'eclipse'
    id 'org.jetbrains.gradle.plugin.idea-ext' version '1.1.8'
    id 'com.github.johnrengelman.shadow' version '8.1.1'
}

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')

group = project.mod_group_id
version = mod_version

repositories {
    mavenLocal()
    mavenCentral()
    maven {
        name = "papermc-repo"
        url = "https://repo.papermc.io/repository/maven-public/"
    }
    maven {
        name = "sonatype"
        url = "https://oss.sonatype.org/content/groups/public/"
    }
    maven {
        url = uri(maven_repository_url)
        credentials {
            username = System.getenv("GH_USERNAME")
            password = System.getenv("PACKAGE_READ_ONLY_TOKEN")
        }
    }
}

dependencies {
    implementation "com.velocitypowered:velocity-api:${project.api_version}-SNAPSHOT"
    annotationProcessor "com.velocitypowered:velocity-api:${project.api_version}-SNAPSHOT"

    annotationProcessor "org.projectlombok:lombok:${project.lombok_version}"
    compileOnly "org.projectlombok:lombok:${project.lombok_version}"

    implementation "com.github.theword.queqiao:queqiao-tool:${tool_version}"
    implementation "org.java-websocket:Java-WebSocket:${java_websocket_version}"
    implementation "org.yaml:snakeyaml:${snakeyaml_version}"
    implementation "org.slf4j:slf4j-api:${slf4j_version}"
    implementation "org.slf4j:slf4j-simple:${slf4j_version}"
    implementation "commons-io:commons-io:${commons_io_version}"
    implementation "org.glavo:rcon-java:${glavoRconVersion}"

    testImplementation "org.slf4j:slf4j-simple:${slf4j_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"
    }
}

def targetJavaVersion = Integer.parseInt(project.java_version)
java {
    def javaVersion = JavaVersion.toVersion(targetJavaVersion)
    sourceCompatibility = javaVersion
    targetCompatibility = javaVersion
    if (JavaVersion.current() < javaVersion)
        toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion)
}

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

def templateSource = file('src/main/templates')
def templateDest = layout.buildDirectory.dir('generated/sources/templates')
def generateTemplates = tasks.register('generateTemplates', Copy) { task ->
    def props = ['version': project.version]
    task.inputs.properties props

    task.from templateSource
    task.into templateDest
    task.expand props
}

sourceSets.main.java.srcDir(generateTemplates.map { it.outputs })

project.idea.project.settings.taskTriggers.afterSync generateTemplates
project.eclipse.synchronizationTasks(generateTemplates)

shadowJar {
    archiveBaseName.set("${mod_name}-velocity+${api_version}")
    archiveClassifier.set("")
    destinationDirectory.set(file("../../QueQiao-jar/${version}"))
    dependencies {
        include(dependency("com.github.theword.queqiao:queqiao-tool:${tool_version}"))
        include(dependency("commons-io:commons-io:${commons_io_version}"))
        include(dependency("org.java-websocket:Java-WebSocket:${java_websocket_version}"))
        include(dependency("org.yaml:snakeyaml:${snakeyaml_version}"))
        include(dependency("org.glavo:rcon-java:${glavoRconVersion}"))
    }

    mergeServiceFiles()
}

tasks.build {
    dependsOn shadowJar
}