import org.jetbrains.compose.desktop.application.dsl.TargetFormat.Deb
import org.jetbrains.compose.desktop.application.dsl.TargetFormat.Dmg
import org.jetbrains.compose.desktop.application.dsl.TargetFormat.Msi

plugins {
    alias(libs.plugins.kotlin.kover)
    alias(libs.plugins.kotlin.jvm)
    alias(libs.plugins.compose.compiler)
    alias(libs.plugins.compose)
}

kotlin {
    jvmToolchain(libs.versions.jdk.get().toInt())

    dependencies {
        implementation(compose.desktop.currentOs)
        implementation(compose.material3)
        implementation(compose.components.resources)
        implementation(projects.commonUI)
        implementation(compose.components.uiToolingPreview)

        testImplementation(libs.kotlin.test)
        testImplementation(libs.atrium)
        testImplementation(compose.desktop.uiTestJUnit4)
        testImplementation(compose.desktop.currentOs)
    }
}

compose.desktop {
    application {
        mainClass = "ch.dissem.yaep.ui.desktop.MainKt"

        nativeDistributions {
            val formats = mutableListOf(Deb)
            packageName = "YAEP"
            val baseVersion = version.toString()
            packageVersion = baseVersion
            if (baseVersion.matches(Regex("[1-9][0-9]*(\\.[0-9]+)(\\.[0-9]+)"))) {
                formats.add(Dmg)
                macOS {
                    iconFile.set(project.file("icon.icns"))
                }
            }
            if (baseVersion.matches(Regex("[1-2]?[0-9]?[0-9]\\.[1-2]?[0-9]?[0-9]\\.[1-6]?[0-9]?[0-9]?[0-9]?[0-9]"))) {
                windows {
                    formats.add(Msi)
                    iconFile.set(project.file("icon.ico"))
                }
            }
            linux {
                debPackageVersion = if (baseVersion.first().isDigit()) {
                    baseVersion
                } else {
                    "0-$baseVersion"
                }
                iconFile.set(project.file("icon.png"))
            }
            targetFormats(*formats.toTypedArray())

            buildTypes.release.proguard {
                configurationFiles.from(project.file("proguard-rules.pro"))
                isEnabled.set(false)
                obfuscate.set(false)
            }
        }
    }
}
