apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: "androidx.navigation.safeargs.kotlin"
apply plugin: 'com.google.gms.google-services'

buildscript {
    repositories {
        google()
        jcenter()
    }
}

def getGitHash = { ->
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'git', 'rev-parse', '--short', 'HEAD'
        standardOutput = stdout
    }
    return stdout.toString().trim()
}

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        applicationId "au.gov.health.covidsafe"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 136
        versionName "2.9"
        buildConfigField "String", "GITHASH", "\"${getGitHash()}\""
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

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

        buildConfigField "String", "ORG", ORG
        buildConfigField "int", "PROTOCOL_VERSION", PROTOCOL_VERSION
        buildConfigField "int", "SERVICE_FOREGROUND_NOTIFICATION_ID", SERVICE_FOREGROUND_NOTIFICATION_ID
        buildConfigField "String", "SERVICE_FOREGROUND_CHANNEL_ID", SERVICE_FOREGROUND_CHANNEL_ID
        buildConfigField "String", "SERVICE_FOREGROUND_CHANNEL_NAME", SERVICE_FOREGROUND_CHANNEL_NAME

        buildConfigField "int", "PUSH_NOTIFICATION_ID", PUSH_NOTIFICATION_ID
        buildConfigField "String", "PUSH_NOTIFICATION_CHANNEL_ID", PUSH_NOTIFICATION_CHANNEL_ID

        buildConfigField "long", "SCAN_DURATION", SCAN_DURATION
        buildConfigField "long", "MIN_SCAN_INTERVAL", MIN_SCAN_INTERVAL
        buildConfigField "long", "MAX_SCAN_INTERVAL", MAX_SCAN_INTERVAL
        buildConfigField "long", "MAX_QUEUE_TIME", MAX_QUEUE_TIME
        buildConfigField "long", "BM_CHECK_INTERVAL", BM_CHECK_INTERVAL
        buildConfigField "long", "HEALTH_CHECK_INTERVAL", HEALTH_CHECK_INTERVAL
        buildConfigField "long", "CONNECTION_TIMEOUT", CONNECTION_TIMEOUT
        buildConfigField "long", "BLACKLIST_DURATION", BLACKLIST_DURATION
        buildConfigField "long", "PERIPHERAL_PAYLOAD_SAVE_INTERVAL", PERIPHERAL_PAYLOAD_SAVE_INTERVAL

        buildConfigField "long", "ADVERTISING_DURATION", ADVERTISING_DURATION
        buildConfigField "long", "ADVERTISING_INTERVAL", ADVERTISING_INTERVAL

        buildConfigField "boolean", "ENABLE_DEBUG_SCREEN", "false"
    }

    signingConfigs {

        release {
        }

        staging {
        }

        debug {
        }
    }

    buildTypes {
        debug {
            buildConfigField "String", "BLE_SSID", STAGING_SERVICE_UUID
            buildConfigField "String", "BLE_ANDROIDSIGNALCHARACTERISTIC", STAGING_ANDROIDSIGNALCHARACTERISTICUUID
            buildConfigField "String", "BLE_IOSSIGNALCHARACTERISTIC", STAGING_IOSSIGNALCHARACTERISTICUUID
            buildConfigField "String", "BLE_PAYLOADCHARACTERISTIC", SRAGING_PAYLOADCHARACTERISTICUUID
            buildConfigField "boolean", "ENABLE_DEBUG_SCREEN", "true"
            buildConfigField "String", "END_POINT_PREFIX", TEST_END_POINT_PREFIX
            buildConfigField "String", "BASE_URL", TEST_BASE_URL
            buildConfigField "String", "IOS_BACKGROUND_UUID", DEBUG_BACKGROUND_IOS_SERVICE_UUID
            buildConfigField "String", "ENCRYPTION_PUBLIC_KEY", DEBUG_ENCRYPTION_PUBLIC_KEY


            String ssid = STAGING_SERVICE_UUID
            versionNameSuffix "-debug-${getGitHash()}-${ssid.substring(ssid.length() - 5, ssid.length() - 1)}"
            resValue "string", "app_name", "COVIDSafe Debug"
            applicationIdSuffix ".debug"
            signingConfig signingConfigs.debug
        }

        staging {
            buildConfigField "String", "BLE_SSID", STAGING_SERVICE_UUID
            buildConfigField "String", "BLE_ANDROIDSIGNALCHARACTERISTIC", STAGING_ANDROIDSIGNALCHARACTERISTICUUID
            buildConfigField "String", "BLE_IOSSIGNALCHARACTERISTIC", STAGING_IOSSIGNALCHARACTERISTICUUID
            buildConfigField "String", "BLE_PAYLOADCHARACTERISTIC", SRAGING_PAYLOADCHARACTERISTICUUID
            buildConfigField "boolean", "ENABLE_DEBUG_SCREEN", "true"
            buildConfigField "String", "END_POINT_PREFIX", STAGING_END_POINT_PREFIX
            buildConfigField "String", "BASE_URL", STAGING_BASE_URL
            buildConfigField "String", "IOS_BACKGROUND_UUID", STAGING_BACKGROUND_IOS_SERVICE_UUID
            buildConfigField "String", "ENCRYPTION_PUBLIC_KEY", STAGING_ENCRYPTION_PUBLIC_KEY

            // Retrieve bluetooth ssid from staging's strings.xml
            String ssid = STAGING_SERVICE_UUID
            versionNameSuffix "-beta-${getGitHash()}-${ssid.substring(ssid.length() - 5, ssid.length() - 1)}"
            debuggable false

            applicationIdSuffix ".beta"
            resValue "string", "app_name", "COVIDSafe beta"

            lintOptions {
                // Ignore lint errors for now
                abortOnError false
            }
            matchingFallbacks = ['release']
            signingConfig signingConfigs.staging
        }

        release {

            buildConfigField "String", "BLE_SSID", PRODUCTION_SERVICE_UUID
            buildConfigField "String", "BLE_ANDROIDSIGNALCHARACTERISTIC", PRODUCTION_ANDROIDSIGNALCHARACTERISTICUUID
            buildConfigField "String", "BLE_IOSSIGNALCHARACTERISTIC", PRODUCTION_IOSSIGNALCHARACTERISTICUUID
            buildConfigField "String", "BLE_PAYLOADCHARACTERISTIC", PRODUCTION_PAYLOADCHARACTERISTICUUID
            buildConfigField "String", "END_POINT_PREFIX", PRODUCTION_END_POINT_PREFIX
            buildConfigField "String", "BASE_URL", PROD_BASE_URL
            buildConfigField "String", "IOS_BACKGROUND_UUID", PRODUCTION_BACKGROUND_IOS_SERVICE_UUID
            buildConfigField "String", "ENCRYPTION_PUBLIC_KEY", PRODUCTION_ENCRYPTION_PUBLIC_KEY

            debuggable false
            jniDebuggable false
            renderscriptDebuggable false
            minifyEnabled false
            shrinkResources false
            multiDexEnabled false
            zipAlignEnabled true
            resValue "string", "app_name", "COVIDSafe"

            lintOptions {
                // Ignore lint errors for now
                abortOnError false
            }
            signingConfig signingConfigs.release
        }
    }

    buildFeatures {
        dataBinding true
    }

    sourceSets {
        staging {
            java.srcDirs = ['src/debug/java']
            res.srcDirs = ['src/debug/res']
            manifest.srcFile 'src/debug/AndroidManifest.xml'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }

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

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
    implementation project(":feedback-android")
    // kotlin
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutines_version"

    //androidx
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation "androidx.coordinatorlayout:coordinatorlayout:1.1.0"

    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"

    implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.1.0-alpha01'
    implementation 'pub.devrel:easypermissions:3.0.0'
    implementation 'com.google.code.gson:gson:2.8.6'

    // room
    kapt "androidx.room:room-compiler:$room_version"
    implementation "androidx.room:room-runtime:$room_version"
    implementation "androidx.room:room-ktx:$room_version"

    implementation "com.squareup.okhttp3:logging-interceptor:$okhttp_version"

    // rx
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'

    //bottom navigation
    implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0'
    implementation 'androidx.navigation:navigation-ui-ktx:2.3.0'

    implementation "androidx.swiperefreshlayout:swiperefreshlayout:$androidx_swiperefreshlayout_version"

    //Lottie
    implementation 'com.airbnb.android:lottie:3.4.1'

    implementation 'com.google.guava:guava:28.2-android'
    implementation "androidx.collection:collection:1.1.0"
    implementation "com.google.crypto.tink:tink-android:1.4.0"
    implementation "androidx.lifecycle:lifecycle-service:2.2.0"
    implementation 'com.github.razir.progressbutton:progressbutton:2.0.1'

    // MPAndroidChart
    implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'

    // flags
    implementation 'com.github.blongho:worldCountryData:1.4'

    // Firebase Cloud Messaging
    implementation 'com.google.firebase:firebase-messaging:20.2.4'

    //Unit testing
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    androidTestImplementation "androidx.room:room-testing:2.2.5"
}
