plugins {
    id 'org.jetbrains.kotlin.multiplatform'
    id 'org.jetbrains.kotlin.plugin.serialization'
    id 'com.android.library'
    id 'org.jetbrains.dokka'
    id 'com.vanniktech.maven.publish'
}

kotlin {
    androidTarget {
        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 {
                implementation libs.kotlinx.serialization.json
                implementation libs.microutils.kotlin.logging

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

        }
    }
}

android {
    namespace "org.fdroid.index"
    compileSdk libs.versions.compileSdk.get().toInteger()
    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"
            }"""]
    )
}
