import org.jetbrains.compose.desktop.application.dsl.TargetFormat
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

val appVersionName = "1.1"
val appVersionCode = 9
val appVersionDisplay = "$appVersionName.$appVersionCode"

plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.androidApplication)
    alias(libs.plugins.composeMultiplatform)
    alias(libs.plugins.composeCompiler)
    alias(libs.plugins.composeHotReload)
}

abstract class GenerateAppVersionConstantsTask : DefaultTask() {
    @get:Input
    abstract val versionName: Property<String>

    @get:Input
    abstract val versionCode: Property<Int>

    @get:OutputDirectory
    abstract val outputDir: DirectoryProperty

    @TaskAction
    fun generate() {
        val outputFile = outputDir.file("net/sergeych/toread/AppVersion.kt").get().asFile
        outputFile.parentFile.mkdirs()
        outputFile.writeText(
            """
            package net.sergeych.toread

            internal const val AppVersionName = "${versionName.get()}"
            internal const val AppVersionCode = ${versionCode.get()}
            internal const val AppVersionDisplay = "${versionName.get()}.${versionCode.get()}"
            """.trimIndent() + "\n",
        )
    }
}

val generateAppVersionConstants by tasks.registering(GenerateAppVersionConstantsTask::class) {
    versionName.set(appVersionName)
    versionCode.set(appVersionCode)
    outputDir.set(layout.buildDirectory.dir("generated/appVersion/commonMain/kotlin"))
}

kotlin {
    jvmToolchain(17)

    androidTarget {
        compilerOptions {
            jvmTarget.set(JvmTarget.JVM_17)
        }
    }

    jvm()

    js {
        browser()
        binaries.executable()
    }

    @OptIn(ExperimentalWasmDsl::class)
    wasmJs {
        browser()
        binaries.executable()
    }

    sourceSets {
        commonMain {
            kotlin.srcDir(generateAppVersionConstants)
        }
        androidMain.dependencies {
            implementation(libs.compose.uiToolingPreview)
            implementation(libs.androidx.activity.compose)
            implementation(libs.androidx.core.ktx)
        }
        commonMain.dependencies {
            implementation(libs.compose.runtime)
            implementation(libs.compose.foundation)
            implementation(libs.compose.material.icons.extended)
            implementation(libs.compose.material3)
            implementation(libs.compose.ui)
            implementation(libs.compose.components.resources)
            implementation(libs.compose.uiToolingPreview)
            implementation(libs.androidx.lifecycle.viewmodelCompose)
            implementation(libs.androidx.lifecycle.runtimeCompose)
            implementation(libs.kotlinx.coroutinesCore)
            implementation(projects.shared)
        }
        commonTest.dependencies {
            implementation(libs.kotlin.test)
        }
        jvmMain.dependencies {
            implementation(compose.desktop.currentOs)
            implementation(libs.kotlinx.coroutinesSwing)
        }
    }
}

android {
    namespace = "net.sergeych.toread"
    compileSdk = libs.versions.android.compileSdk.get().toInt()

    defaultConfig {
        applicationId = "net.sergeych.toread"
        minSdk = libs.versions.android.minSdk.get().toInt()
        targetSdk = libs.versions.android.targetSdk.get().toInt()
        versionCode = appVersionCode
        versionName = appVersionName
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
            signingConfig = signingConfigs.getByName("debug")
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
}

dependencies {
    debugImplementation(libs.compose.uiTooling)
}

compose.desktop {
    application {
        mainClass = "net.sergeych.toread.MainKt"

        nativeDistributions {
            targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
            packageName = "To Read"
            packageVersion = appVersionDisplay
            fileAssociation("application/x-fictionbook+xml", "fb2", "FictionBook document")
            fileAssociation("application/zip", "fb2.zip", "Zipped FictionBook document")

            macOS {
                iconFile.set(project.file("src/jvmMain/resources/icons/icon.icns"))
            }
            windows {
                iconFile.set(project.file("src/jvmMain/resources/icons/icon.ico"))
                shortcut = true
                menu = true
            }
            linux {
                packageName = "to-read"
                iconFile.set(project.file("src/jvmMain/resources/icons/icon.png"))
                shortcut = true
            }
        }
    }
}
