plugins {
    id 'application'
    id 'java'
    id 'org.openjfx.javafxplugin' version '0.1.0'
    id 'checkstyle'
    id 'com.diffplug.spotless' version '7.0.2'
}

group = 'ch.unibas.dmi.dbis'
version = '0.0.1-ALPHA'

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(25)
    }
}

application {
    mainClass = 'ch.unibas.dmi.dbis.cs108.casono.Main'
}

repositories {
    mavenCentral()
}

javafx {
    version = "25.0.2"
    modules = ['javafx.controls', 'javafx.fxml', 'javafx.base', 'javafx.graphics', 'javafx.web']
}

dependencies {
    // Source: https://mvnrepository.com/artifact/org.apache.logging.log4j
    implementation("org.apache.logging.log4j:log4j-api:2.25.3")
    runtimeOnly("org.apache.logging.log4j:log4j-core:2.25.3")

    // Source: https://mvnrepository.com/artifact/org.fusesource.jansi/jansi
    runtimeOnly("org.fusesource.jansi:jansi:2.4.2")

    testImplementation("org.junit.jupiter:junit-jupiter:5.10.0")
    testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.10.0")
    implementation 'org.json:json:20240303'
}

test {
    useJUnitPlatform()
}

checkstyle {
    toolVersion = '10.21.0'
    configFile = file('config/checkstyle/checkstyle.xml')
    configProperties = [
        'suppressionFile': file('config/checkstyle/suppressions.xml').absolutePath
    ]
}

spotless {
    java {
        googleJavaFormat('1.25.2').aosp()
        importOrder()
        removeUnusedImports()
        trimTrailingWhitespace()
        endWithNewline()
    }
}

tasks.named('jar', Jar) {
    manifest {
        attributes('Main-Class': application.mainClass.get())
    }
}


// Dieses JVM-Argument erlaubt JavaFX und anderen Libraries den Zugriff auf native Methoden.
// Ohne diese Einstellung erscheinen Warnungen und zukünftige Java-Versionen könnten den Zugriff blockieren.
// Siehe: https://openjdk.org/jeps/472
tasks.withType(JavaExec) {
    jvmArgs += '--enable-native-access=ALL-UNNAMED'
}



tasks.register('cruntest', JavaExec) {
    classpath = sourceSets.main.runtimeClasspath
    mainClass = 'ch.unibas.dmi.dbis.cs108.casono.Main'
    jvmArgs '--enable-native-access=ALL-UNNAMED'
    args 'client', '0.0.0.0:1234'
}

tasks.register('fatJar', Jar) {
    group = 'build'
    description = 'Assembles a runnable fat JAR including runtime dependencies.'

    archiveClassifier = 'all'
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE

    manifest {
        attributes('Main-Class': application.mainClass.get())
    }

    // include your compiled classes/resources
    from(sourceSets.main.output)

    // make sure runtimeClasspath is resolved before we try to zipTree it
    dependsOn(configurations.runtimeClasspath)

    // unpack runtime deps into the jar
    from({
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    })
}
