apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.google.android.gms.oss-licenses-plugin'
apply from: "$rootDir/gradle/base.gradle"

def apiVersion = "2.2"
def minorVersion = "1"
def versionString = apiVersion + "." + minorVersion
def buildCode = getBuildCode()
def buildName = versionString + "." + buildCode.toString() + "-" + getGitDescribe()
def packageName = "at.roteskreuz.stopcorona"

android {
    compileSdkVersion 30

    defaultConfig {
        applicationId packageName
        minSdkVersion 23
        targetSdkVersion 30
        versionCode buildCode
        versionName buildName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        javaCompileOptions {
            annotationProcessorOptions {
                arguments += ["room.schemaLocation":
                                      "$projectDir/schemas".toString()]
            }
        }

        def addConstant = {constantName, constantValue ->
            manifestPlaceholders += [ (constantName):constantValue]
            buildConfigField "String", "${constantName}", "\"${constantValue}\""
        }
        addConstant("DEEPLINK_HOST", "webview")
        addConstant("DEEPLINK_SCHEME", "stoppcorona")
    }

    signingConfigs {
        release {
            keyAlias getKeyAlias()
            storeFile file("../keystore/release.keystore")
            storePassword getStorePassword()
            keyPassword getKeyPassword()
        }

        debug {
            def keyStoreFile = file("../keystore/android_debug.keystore")
            if (keyStoreFile.exists()) {
                keyAlias "android_debug_key"
                storeFile keyStoreFile
                storePassword "android_debug"
                keyPassword "android_debug"
            }
        }
    }

    flavorDimensions "default"

    productFlavors {
        dummyApi {
            /*
             * Default values provided for CI during merge requests only.
             *
             * Always provide real values through local.properties
             */
            buildConfigField "String", "HOSTNAME", '"dummy"'
            buildConfigField "String", "BASE_URL", '"dummy"'
            buildConfigField "String", "HOSTNAME_TAN", '"dummy"'
            buildConfigField "String", "BASE_URL_TAN", '"dummy"'
            buildConfigField "String", "HOSTNAME_CDN", '"dummy"'
            buildConfigField "String", "BASE_URL_CDN", '"dummy"'
            buildConfigField "String[]", "CERTIFICATE_CHAIN", '{}'
            buildConfigField "String[]", "CERTIFICATE_CHAIN_TAN", '{}'
            buildConfigField "String[]", "CERTIFICATE_CHAIN_CDN", '{}'
            buildConfigField "String", "AUTHORIZATION_VALUE", '"dummy"'
            buildConfigField "String", "AGES_URL", '"dummy"'
        }

        stageApi {
            applicationIdSuffix ".stage"
            versionName versionString + "." + buildCode.toString() + "-" + getGitDescribe() + "-stageApi"

            buildConfigField "String", "HOSTNAME", resolveProperty("STAGE_HOSTNAME", '"dummy"')
            buildConfigField "String", "BASE_URL", resolveProperty("STAGE_BASE_URL", '"dummy"')
            buildConfigField "String", "HOSTNAME_TAN", resolveProperty("STAGE_HOSTNAME_TAN", '"dummy"')
            buildConfigField "String", "BASE_URL_TAN", resolveProperty("STAGE_BASE_URL_TAN", '"dummy"')
            buildConfigField "String", "HOSTNAME_CDN", resolveProperty("STAGE_HOSTNAME_CDN", '"dummy"')
            buildConfigField "String", "BASE_URL_CDN", resolveProperty("STAGE_BASE_URL_CDN", '"dummy"')
            buildConfigField "String[]", "CERTIFICATE_CHAIN", resolveProperty("STAGE_CERTIFICATE_CHAIN", '{}')
            buildConfigField "String[]", "CERTIFICATE_CHAIN_TAN", resolveProperty("STAGE_CERTIFICATE_CHAIN_TAN", '{}')
            buildConfigField "String[]", "CERTIFICATE_CHAIN_CDN", resolveProperty("STAGE_CERTIFICATE_CHAIN_CDN", '{}')
            buildConfigField "String", "AUTHORIZATION_VALUE", resolveProperty("STAGE_AUTHORIZATION_VALUE", '"dummy"')
            buildConfigField "String", "AGES_URL", resolveProperty("STAGE_AGES_URL", '"dummy"')
        }

        prodApi {
            buildConfigField "String", "HOSTNAME", resolveProperty("PROD_HOSTNAME", '"dummy"')
            buildConfigField "String", "BASE_URL", resolveProperty("PROD_BASE_URL", '"dummy"')
            buildConfigField "String", "HOSTNAME_TAN", resolveProperty("PROD_HOSTNAME_TAN", '"dummy"')
            buildConfigField "String", "BASE_URL_TAN", resolveProperty("PROD_BASE_URL_TAN", '"dummy"')
            buildConfigField "String", "HOSTNAME_CDN", resolveProperty("PROD_HOSTNAME_CDN", '"dummy"')
            buildConfigField "String", "BASE_URL_CDN", resolveProperty("PROD_BASE_URL_CDN", '"dummy"')
            buildConfigField "String[]", "CERTIFICATE_CHAIN", resolveProperty("PROD_CERTIFICATE_CHAIN", '{}')
            buildConfigField "String[]", "CERTIFICATE_CHAIN_TAN", resolveProperty("PROD_CERTIFICATE_CHAIN_TAN", '{}')
            buildConfigField "String[]", "CERTIFICATE_CHAIN_CDN", resolveProperty("PROD_CERTIFICATE_CHAIN_CDN", '{}')
            buildConfigField "String", "AUTHORIZATION_VALUE", resolveProperty("PROD_AUTHORIZATION_VALUE", '"dummy"')
            buildConfigField "String", "AGES_URL", resolveProperty("PROD_AGES_URL", '"dummy"')
        }
    }

    buildTypes {
        debug {
            signingConfig signingConfigs.debug
            // don't generate crashlytics id on each build https://developer.android.com/studio/build/optimize-your-build?utm_source=android-studio#disable-crashlytics-build-id
            ext.alwaysUpdateBuildId = false
        }
        release {
            minifyEnabled true
            // Commented out because it breaks CI. Do not put back in!
            // In release builds, the signing config is provided by the CI
            //signingConfig signingConfigs.release
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }

    packagingOptions {
        exclude 'META-INF/atomicfu.kotlin_module'
    }
}

dependencies {


    implementation project(":core")

    implementation 'com.squareup.okhttp3:okhttp:4.9.3'
    implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0'

    kapt "androidx.room:room-compiler:2.2.4"

    def coroutinesVersion = "1.3.7"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:$coroutinesVersion"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-rx2:$coroutinesVersion"

    implementation "androidx.constraintlayout:constraintlayout:1.1.3"
    implementation "androidx.cardview:cardview:1.0.0"

    implementation "android.arch.lifecycle:extensions:1.1.1"

    implementation 'com.google.android.gms:play-services-oss-licenses:17.0.0'

    kapt "com.airbnb.android:epoxy-processor:3.7.0"

    kapt "com.squareup.moshi:moshi-kotlin-codegen:1.8.0"

    debugImplementation "com.facebook.stetho:stetho:1.5.1"

    def glideVersion = "4.9.0"
    implementation "com.github.bumptech.glide:glide:$glideVersion"
    kapt "com.github.bumptech.glide:compiler:$glideVersion"

    // see https://tasks.pxp-x.com/browse/CTAA-1499 and https://github.com/google/exposure-notifications-android/issues/23
    // we keep the unreliable file dependency with manual play-services-base
    implementation 'com.google.android.gms:play-services-base:17.3.0'
    implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])

    def work_version = "2.4.0"
    implementation "androidx.work:work-runtime-ktx:$work_version"

    implementation "com.airbnb.android:lottie:3.4.0"

    implementation 'com.itextpdf:itextg:5.5.10'
    implementation "androidx.webkit:webkit:1.4.0"

    implementation 'com.mapbox.mapboxsdk:mapbox-sdk-geojson:5.8.0'

    testImplementation "junit:junit:4.13"
    testImplementation "org.threeten:threetenbp:1.3.8"
    androidTestImplementation 'com.android.support:support-annotations:28.0.0'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
}

// Used in the .gitlab-ci.yml to access the app version
task getAppVersion {
    println " version = $versionString"
}
