plugins {
    id 'org.jetbrains.kotlin.multiplatform'
    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.all {
            kotlinOptions.jvmTarget = '1.8'
        }
        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")
//    else if (isMingwX64) nativeTarget = mingwX64("native")
//    else throw new GradleException("Host OS is not supported in Kotlin/Native.")

    sourceSets {
        all {
            languageSettings {
                optIn('kotlin.RequiresOptIn')
                explicitApi('strict')
            }
        }
        commonMain {
            dependencies {
                api "io.ktor:ktor-client-core:2.1.3"
                implementation 'io.github.microutils:kotlin-logging:2.1.21'
            }
        }
        commonTest {
            dependencies {
                implementation kotlin('test')
                implementation "io.ktor:ktor-client-mock:2.1.3"
            }
        }
        // JVM is disabled for now, because Android app is including it instead of Android library
        jvmMain {
            dependencies {
                implementation "io.ktor:ktor-client-cio:2.1.3"
            }
        }
        jvmTest {
            dependencies {
                implementation 'junit:junit:4.13.2'
            }
        }
        androidMain {
            // needed because of https://issuetracker.google.com/issues/231701341
            kotlin.srcDir("src/commonMain/kotlin")
            dependencies {
                implementation "io.ktor:ktor-client-okhttp:2.1.3"
                implementation("com.github.bumptech.glide:glide:4.12.0") {
                    transitive = false // we don't need all that it pulls in, just the basics
                }
                implementation "com.github.bumptech.glide:annotations:4.12.0"
            }
        }
        androidTest {
            // needed because of https://issuetracker.google.com/issues/231701341
            kotlin.srcDir("src/commonTest/kotlin")
            dependencies {
                implementation kotlin('test')
                implementation 'org.json:json:20220320'
                implementation 'junit:junit:4.13.2'
                implementation 'ch.qos.logback:logback-classic:1.2.11'
            }
        }
        androidAndroidTest {
            dependencies {
                implementation 'androidx.test:runner:1.4.0'
                implementation 'androidx.test.ext:junit:1.1.3'
            }
        }
        nativeMain {
            dependencies {
                implementation "io.ktor:ktor-client-curl:2.1.3"
            }
        }
        nativeTest {

        }
    }
}

android {
    compileSdkVersion 31
    sourceSets {
        main.manifest.srcFile('src/androidMain/AndroidManifest.xml')
        getByName("androidTest").java.srcDir(file("src/androidAndroidTest/kotlin"))
    }
    defaultConfig {
        minSdkVersion 21
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
        testInstrumentationRunnerArguments disableAnalytics: 'true'
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    lintOptions {
        checkReleaseBuilds false
        abortOnError true

        htmlReport true
        xmlReport false
        textReport true

        lintConfig file("lint.xml")
    }
}

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"
