plugins {
    id 'org.jetbrains.kotlin.multiplatform'
    id 'org.jetbrains.kotlin.plugin.serialization' version '1.9.22'
    id 'com.android.library'
    id 'org.jetbrains.dokka'
    id "org.jlleitschuh.gradle.ktlint" version "10.2.1"
    id 'com.vanniktech.maven.publish'
}

kotlin {
    android {
        compilations.configureEach {
            kotlinOptions.jvmTarget = '17'
        }
        publishLibraryVariants("release")
    }
//    def hostOs = System.getProperty("os.name")
//    def isMingwX64 = hostOs.startsWith("Windows")
//    def nativeTarget
//    if (hostOs == "Mac OS X") nativeTarget = macosX64('native')
//    else if (hostOs == "Linux") nativeTarget = linuxX64("native") {
//        binaries { sharedLib { baseName = "fdroid_index" } }
//    }
//    else if (isMingwX64) nativeTarget = mingwX64("native")
//    else throw new GradleException("Host OS is not supported in Kotlin/Native.")

    sourceSets {
        configureEach {
            languageSettings {
                optIn('kotlin.RequiresOptIn')
                explicitApi('strict')
            }
        }
        commonMain {
            dependencies {
                // 1.4.1 because https://github.com/Kotlin/kotlinx.serialization/issues/2231
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.1"
                implementation 'io.github.microutils:kotlin-logging:2.1.21'

                implementation project(":libs:download")
                implementation "io.ktor:ktor-io:2.3.8"
            }
        }
        commonTest {
            dependencies {
                implementation project(":libs:sharedTest")
                implementation kotlin('test')
                implementation "com.goncalossilva:resources:0.2.1"
            }
        }
        // JVM is disabled for now, because Android app is including it instead of Android library
        jvmMain {
            dependencies {
            }
        }
        jvmTest {
            dependencies {
                implementation 'junit:junit:4.13.2'
            }
        }
        androidMain {
            dependencies {
                implementation "org.jetbrains.kotlin:kotlin-reflect:1.9.22"
                implementation 'androidx.core:core-ktx:1.12.0'
            }
        }
        androidUnitTest {
            dependencies {
                implementation 'junit:junit:4.13.2'
                implementation 'io.mockk:mockk:1.13.8'
            }
        }
        androidInstrumentedTest {
            dependencies {
                implementation project(":libs:sharedTest")
                implementation kotlin('test')
                implementation 'androidx.test:runner:1.5.2'
                implementation 'androidx.test.ext:junit:1.1.5'
            }
        }
        nativeMain {
            dependencies {
            }
        }
        nativeTest {

        }
    }
}

android {
    namespace "org.fdroid.index"
    compileSdk 34
    sourceSets {
        main.manifest.srcFile('src/androidMain/AndroidManifest.xml')
        getByName("androidTest").java.srcDir(file("src/androidAndroidTest/kotlin"))
    }
    defaultConfig {
        minSdkVersion 21
        consumerProguardFiles 'consumer-rules.pro'

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        testInstrumentationRunnerArguments disableAnalytics: 'true'
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
    testOptions {
        unitTests.returnDefaultValues = true
    }
}

signing {
    useGpgCmd()
}

import org.jetbrains.dokka.gradle.DokkaTask
tasks.withType(DokkaTask).configureEach {
    pluginsMapConfiguration.set(
            ["org.jetbrains.dokka.base.DokkaBase": """{
                "customAssets": ["${file("${rootProject.rootDir}/logo-icon.svg")}"],
                "footerMessage": "© 2010-2022 F-Droid Limited and Contributors"
            }"""]
    )
}

apply from: "${rootProject.rootDir}/gradle/ktlint.gradle"
