import dependencies.Dep

def ideaActive = System.getProperty("idea.active") == "true"

apply plugin: 'com.android.library'
apply plugin: 'kotlin-multiplatform'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlinx-serialization'

apply from: rootProject.file('gradle/android.gradle')
android.libraryVariants.all {
    // TODO replace with https://issuetracker.google.com/issues/72050365 once released.
    it.generateBuildConfigProvider.configure {
        enabled = true
    }
}

kotlin {
    android("android")
    iosArm64("iosArm64") {
        binaries {
            framework {
                freeCompilerArgs += "-Xobjc-generics"
            }
        }
    }
    iosX64("iosX64") {
        binaries {
            framework {
                freeCompilerArgs += "-Xobjc-generics"
            }
        }
    }
    if (ideaActive) {
        iosX64("ios") {
            binaries {
                framework {
                    freeCompilerArgs += "-Xobjc-generics"
                }
            }
        }
    }
    sourceSets {
        commonMain.dependencies {
            implementation Dep.Kotlin.stdlibCommon
            implementation Dep.Kotlin.coroutinesCommon
            implementation Dep.Ktor.clientCommon
            implementation Dep.Ktor.serializationCommon
            implementation Dep.Kotlin.serializationCommon
        }
        androidMain {
            dependsOn commonMain
            dependencies {
                implementation Dep.Kotlin.stdlibJvm
                implementation Dep.Kotlin.androidCoroutinesDispatcher
                implementation Dep.Ktor.clientAndroid
                implementation Dep.Ktor.serializationJvm
                implementation Dep.Kotlin.serializationAndroid
                implementation Dep.OkHttp.client
                implementation Dep.Stetho.stetho
                implementation Dep.Dagger.core
                implementation Dep.Dagger.androidSupport
                implementation Dep.OkHttp.loggingInterceptor
            }
        }
        androidTest {
            dependsOn androidMain
            dependencies {
                implementation Dep.Test.junit
                implementation Dep.Test.slf4j
            }
        }
        iOSMain {
            final String mode = project.findProperty("XCODE_CONFIGURATION")?.toUpperCase() ?: 'DEBUG'
            if (mode == "RELEASE") {
                kotlin.srcDirs += "src/iosMain/kotlinRelease"
            } else {
                kotlin.srcDirs += "src/iosMain/kotlinDebug"
            }
            dependsOn commonMain
            dependencies {
                implementation Dep.Kotlin.coroutinesNative
                implementation Dep.Ktor.clientIos
                implementation Dep.Ktor.jsonNative
                implementation Dep.Ktor.serializationNative
                implementation Dep.Kotlin.serializationNative
            }
        }
        configure(iosX64Main) {
            dependsOn iOSMain
            dependencies {
                implementation Dep.Kotlin.coroutinesIosX64
                implementation Dep.Ktor.clientIosX64
                implementation Dep.Ktor.jsonIosIosX64
                implementation Dep.Ktor.serializationIosX64
                implementation Dep.Kotlin.serializationIosX64
            }
        }
        configure(iosArm64Main) {
            dependsOn iOSMain
            dependencies {
                implementation Dep.Kotlin.coroutinesIosArm64
                implementation Dep.Ktor.clientIosArm64
                implementation Dep.Ktor.jsonIosArm64
                implementation Dep.Ktor.serializationIosArm64
                implementation Dep.Kotlin.serializationIosArm64
            }
        }
    }
}

android {
    defaultConfig {
        buildConfigField "String", "API_ENDPOINT", "\"https://deploy-preview-49--droidkaigi-api-dev.netlify.com/2020\""
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            buildConfigField "String", "API_ENDPOINT", "\"https://api.droidkaigi.jp/2020\""
        }
    }
}

dependencies {
    kapt Dep.Dagger.compiler
    kapt Dep.Dagger.androidProcessor
}
