import de.arxsilex.maptools.build.PrepareWorkerLauncherTask

val hostOs = System.getProperty("os.name").lowercase()

plugins {
    kotlin("jvm")
    id("org.jetbrains.compose")
    id("org.jetbrains.kotlin.plugin.compose")
}

kotlin {
    jvmToolchain(25)
}

dependencies {
    implementation(project(":core"))
    runtimeOnly(project(":worker"))
    implementation(compose.desktop.currentOs)
    implementation("org.jetbrains.compose.components:components-resources:1.12.0")
    implementation("org.jetbrains.compose.material3:material3:1.9.0")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.11.0")
    implementation("com.github.mapsforge.mapsforge:mapsforge-map-reader:0.29.0")
    testImplementation(kotlin("test"))
}

compose.resources {
    packageOfResClass = "de.arxsilex.maptools.converter.desktop.generated.resources"
}

compose.desktop {
    application {
        mainClass = "de.arxsilex.maptools.converter.desktop.MainKt"
        jvmArgs(
            "--enable-native-access=ALL-UNNAMED",
            "-Dmaptools.version=${project.version}",
        )

        nativeDistributions {
            // Mapsforge POI uses JDBC and Apache SIS discovers GeoTIFF providers through JNDI.
            // Both paths are loaded dynamically and therefore cannot be discovered reliably by
            // jlink's static module analysis.
            modules("java.sql", "java.naming", "jdk.management")
            packageName = "ArxSilex MapTools Converter"
            packageVersion = project.version.toString()
            description = "Offline-Konverter für OpenStreetMap- und Mapsforge-Kartendaten"
            vendor = "ArxSilex"

            windows {
                iconFile.set(project.file("src/main/resources/branding/maptools-converter.ico"))
            }

            linux {
                iconFile.set(project.file("src/main/composeResources/drawable/maptools_logo.png"))
                packageName = "arxsilex-maptools-converter"
            }
        }

        buildTypes.release.proguard {
            version.set("7.8.0")
        }
    }
}

val packagedApplicationName = "ArxSilex MapTools Converter"
val workerLauncherName = "maptools-worker"
val prepareWorkerLauncher = tasks.register<PrepareWorkerLauncherTask>("prepareWorkerLauncher") {
    group = "distribution"
    description = "Ergänzt den nativen Starter für den isolierten Export-Worker."
    dependsOn("createDistributable")
    if (hostOs.contains("windows")) {
        mainLauncher.set(layout.buildDirectory.file("compose/binaries/main/app/$packagedApplicationName/$packagedApplicationName.exe"))
        mainConfiguration.set(layout.buildDirectory.file("compose/binaries/main/app/$packagedApplicationName/app/$packagedApplicationName.cfg"))
        workerLauncher.set(layout.buildDirectory.file("compose/binaries/main/app/$packagedApplicationName/$workerLauncherName.exe"))
        workerConfiguration.set(layout.buildDirectory.file("compose/binaries/main/app/$packagedApplicationName/app/$workerLauncherName.cfg"))
    } else {
        mainLauncher.set(layout.buildDirectory.file("compose/binaries/main/app/$packagedApplicationName/bin/$packagedApplicationName"))
        mainConfiguration.set(layout.buildDirectory.file("compose/binaries/main/app/$packagedApplicationName/lib/app/$packagedApplicationName.cfg"))
        workerLauncher.set(layout.buildDirectory.file("compose/binaries/main/app/$packagedApplicationName/bin/$workerLauncherName"))
        workerConfiguration.set(layout.buildDirectory.file("compose/binaries/main/app/$packagedApplicationName/lib/app/$workerLauncherName.cfg"))
    }
}
