plugins {
    id "java"
    id "com.google.cloud.tools.jib" version "3.4.0"
    id 'com.github.johnrengelman.shadow' version '8.1.0'
}

dependencies {
    implementation platform("org.lwjgl:lwjgl-bom:$lwjglVersion")
    implementation "org.lwjgl:lwjgl"
    implementation "org.lwjgl:lwjgl-assimp"
    implementation "org.lwjgl:lwjgl-stb"
    implementation "org.lwjgl:lwjgl-opengl"
    implementation "org.lwjgl:lwjgl-ktx"

    // joml (java opengl math library)
    implementation "org.joml:joml:1.10.5"

    // proj4j (java projection library)
    implementation "org.locationtech.proj4j:proj4j:1.2.3"
    implementation "org.locationtech.proj4j:proj4j-epsg:1.2.3"

    // jackson (json library)
    implementation 'com.fasterxml.jackson.core:jackson-core:2.15.2'
    // jackson-xml
    implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.15.2'

    // GeoTools
    implementation "org.geotools:gt-geotiff:26.5"
    implementation 'org.geotools:gt-shapefile:26.5'
    implementation 'org.geotools:gt-geojson:26.5'
    implementation 'org.geotools:gt-geojsondatastore:26.5'
    implementation "org.geotools:gt-coverage:26.5"
    implementation "org.geotools:gt-main:26.5"
    implementation "org.geotools:gt-image:26.5"
    implementation "org.geotools:gt-grid:26.5"
    implementation "org.geotools:gt-process:26.5"
    implementation "org.geotools:gt-process-raster:26.5"
    implementation "org.geotools:gt-process-feature:26.5"
    implementation "org.geotools:gt-geopkg:26.5"

    // add shaded dependencies
    implementation "javax.media:jai_core:1.1.3"
    implementation "javax.media:jai_imageio:1.1"
    implementation "javax.media:jai_codec:1.1.3"

    // JDOM
    implementation "org.jdom:jdom2:2.0.6"

    // jgltf (java gltf library)
    implementation "de.javagl:jgltf-obj:2.0.4"
    implementation "de.javagl:jgltf-model:2.0.4"
    implementation "de.javagl:jgltf-validator:2.0.4"
    implementation 'de.javagl:jgltf-impl-v1:2.0.4'
    implementation 'de.javagl:jgltf-impl-v2:2.0.4'

    // CityGML4J
    implementation 'org.citygml4j:citygml4j-xml:3.0.0'
    implementation 'org.citygml4j:citygml4j-cityjson:3.0.0'
    // LASzip4J
    implementation 'com.github.mreutegg:laszip4j:0.17'
    // Indoor4J
    implementation files("libs/Indoor4J.jar")

    // native libraries for LWJGL
    runtimeOnly "org.lwjgl:lwjgl::natives-linux"
    runtimeOnly "org.lwjgl:lwjgl-opengl::natives-linux"
    runtimeOnly "org.lwjgl:lwjgl-glfw::natives-linux"
    runtimeOnly "org.lwjgl:lwjgl-assimp::natives-linux"
    runtimeOnly "org.lwjgl:lwjgl-openal::natives-linux"
    runtimeOnly "org.lwjgl:lwjgl-stb::natives-linux"
    runtimeOnly "org.lwjgl:lwjgl-ktx::natives-linux"

    runtimeOnly "org.lwjgl:lwjgl::natives-windows"
    runtimeOnly "org.lwjgl:lwjgl-opengl::natives-windows"
    runtimeOnly "org.lwjgl:lwjgl-glfw::natives-windows"
    runtimeOnly "org.lwjgl:lwjgl-assimp::natives-windows"
    runtimeOnly "org.lwjgl:lwjgl-openal::natives-windows"
    runtimeOnly "org.lwjgl:lwjgl-stb::natives-windows"
    runtimeOnly "org.lwjgl:lwjgl-ktx::natives-windows"

    runtimeOnly "org.lwjgl:lwjgl::natives-macos"
    runtimeOnly "org.lwjgl:lwjgl-opengl::natives-macos"
    runtimeOnly "org.lwjgl:lwjgl-glfw::natives-macos"
    runtimeOnly "org.lwjgl:lwjgl-assimp::natives-macos"
    runtimeOnly "org.lwjgl:lwjgl-openal::natives-macos"
    runtimeOnly "org.lwjgl:lwjgl-stb::natives-macos"
    runtimeOnly "org.lwjgl:lwjgl-ktx::natives-macos"
}
test {
    useJUnitPlatform {
        includeTags 'default'
    }
}

java {
    //withJavadocJar()
}

javadoc {
    options.addStringOption("locale", "ko_KR")
    options.addStringOption("encoding", "UTF-8")
    options.addStringOption("charset", "UTF-8")
    options.addStringOption("docencoding", "UTF-8")
}

jar {
    dependsOn 'test'
    finalizedBy shadowJar
    manifest {
        attributes "Main-Class": "com.gaia3d.command.mago.Mago3DTilerMain"
        attributes "Implementation-Version": project.version
        attributes 'Implementation-Title': 'mago-3d-tiler'
        attributes 'Implementation-Vendor': 'Gaia3d, Inc.'
        attributes 'Implementation-Vendor-Id': 'com.gaia3d'
        attributes 'Implementation-URL': 'http://java.sun.com/products/java-media/jai/forDevelopers/jai-imageio/'
    }
    from {
        configurations.runtimeClasspath.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}

shadowJar {
    mergeServiceFiles()
    manifest {
        attributes 'Main-Class': 'com.gaia3d.command.mago.Mago3DTilerMain'
        attributes 'Implementation-Version': project.version
        attributes 'Implementation-Title': 'mago-3d-tiler'
        attributes 'Implementation-Vendor': 'Gaia3d, Inc.'
        attributes 'Implementation-Vendor-Id': 'com.gaia3d'
        attributes 'Implementation-URL': 'http://java.sun.com/products/java-media/jai/forDevelopers/jai-imageio/'
    }
    relocate 'org.slf4j', 'shadow.slf4j'
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    from sourceSets.main.runtimeClasspath
    configurations = [project.configurations.runtimeClasspath]
    archiveFileName = "mago-3d-tiler-${project.version}.jar"
    destinationDirectory.set(file("${projectDir}/dist"))
}

jib {
    from {
        image = "eclipse-temurin:17-jre"
        platforms {
            platform {
                architecture = "amd64"
                os = "linux"
            }
            platform {
                architecture = "arm64"
                os = "linux"
            }
        }
    }
    to {
        image = "gaia3d/mago-3d-tiler"
        tags = ["latest", project.version] as List<String>
        if (findProperty("jib.to.auth.username") || System.getenv("JIB_TO_AUTH_USERNAME")) {
            auth {
                username = findProperty("jib.to.auth.username") ?: System.getenv("JIB_TO_AUTH_USERNAME")
                password = findProperty("jib.to.auth.password") ?: System.getenv("JIB_TO_AUTH_PASSWORD")
            }
        }
    }
    container {
        appRoot = "/mago-3d-tiler"
        creationTime = "USE_CURRENT_TIMESTAMP"
        jvmFlags = [
                "-Djava.awt.headless=true",
                "-Dfile.encoding=UTF-8",
                "-XX:+UseContainerSupport",
                "-XX:MaxGCPauseMillis=100",
                "-XX:+UseStringDeduplication",
        ]
        entrypoint = ["java", "-jar", "/app/mago-3d-tiler-${project.version}.jar"]
    }
    extraDirectories {
        paths {
            path {
                from = file("dist")
                into = "/app"
            }
        }
    }
}

clean {
    delete file("${projectDir}/dist")
}

compileJava {
    options.encoding = 'UTF-8'
}

tasks.named('jib') {
    dependsOn(shadowJar)
    onlyIf {
        def jarFile = layout.buildDirectory.file("../dist/mago-3d-tiler-${project.version}.jar").get().asFile
        if (!jarFile.exists()) {
            logger.lifecycle("⚠️ JAR file not found: ${jarFile.absolutePath}")
            logger.lifecycle(" > Please build the project first.")
            throw new GradleException("JAR file not found: ${jarFile.absolutePath}")
        }
        return true
    }
}