import com.android.build.OutputFile

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'
apply plugin: 'kotlin-android-extensions'

buildscript {
    repositories {
        google()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
        jcenter()
    }

    dependencies {
        classpath "io.realm:realm-gradle-plugin:5.12.0"
    }
}

kapt {
    correctErrorTypes = true
}

// map for the version codes
// x86 must have greater values than arm, see https://software.intel.com/en-us/android/articles/google-play-supports-cpu-architecture-filtering-for-multiple-apk
// 64 bits have greater value than 32 bits
ext.abiVersionCodes = ["armeabi-v7a": 1, "arm64-v8a": 2, "x86": 3, "x86_64": 4].withDefault { 0 }

android {
    compileSdkVersion 28

    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId "im.vector"
        minSdkVersion 16
        targetSdkVersion 28
        // use the version code
        versionCode rootProject.ext.versionCodeProp
        versionName rootProject.ext.versionNameProp

        // Keep abiFilter for the universalApk
        ndk {
            abiFilters "armeabi-v7a", "x86", 'arm64-v8a', 'x86_64'
        }

        multiDexEnabled true

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    // Ref: https://developer.android.com/studio/build/configure-apk-splits.html
    splits {
        // Configures multiple APKs based on ABI.
        abi {
            // Enables building multiple APKs per ABI.
            enable true

            // By default all ABIs are included, so use reset() and include to specify that we only
            // want APKs for armeabi-v7a, x86, arm64-v8a and x86_64.

            // Resets the list of ABIs that Gradle should create APKs for to none.
            reset()

            // Specifies a list of ABIs that Gradle should create APKs for.
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"

            // Generate a universal APK that includes all ABIs, so user who install from CI tool can use this one by default.
            universalApk true
        }
    }

    android.applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def baseAbiVersionCode = project.ext.abiVersionCodes.get(output.getFilter(OutputFile.ABI))
            output.versionCodeOverride = baseAbiVersionCode * 10_000_000 + variant.versionCode
        }
    }

    dexOptions {
        jumboMode true
        javaMaxHeapSize "2g"
    }

    signingConfigs {
        debug {
            keyAlias 'androiddebugkey'
            keyPassword 'android'
            storeFile file('./signature/debug.keystore')
            storePassword 'android'
        }
    }

    buildTypes {
        debug {
            applicationIdSuffix ".debug"
            signingConfig signingConfigs.debug

            resValue "bool", "debug_mode", "true"
            resValue "string", "git_revision", "\"${gitRevision()}\""
            resValue "string", "git_revision_date", "\"${gitRevisionDate()}\""
            resValue "string", "git_branch_name", "\"${gitBranchName()}\""
            resValue "string", "build_number", rootProject.ext.buildNumberProp
            buildConfigField "boolean", "LOW_PRIVACY_LOG_ENABLE", "false"
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        release {
            resValue "bool", "debug_mode", "false"
            resValue "string", "git_revision", "\"${gitRevision()}\""
            resValue "string", "git_revision_date", "\"${gitRevisionDate()}\""
            resValue "string", "git_branch_name", "\"${gitBranchName()}\""
            resValue "string", "build_number", rootProject.ext.buildNumberProp
            buildConfigField "boolean", "LOW_PRIVACY_LOG_ENABLE", "false"
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    // Specifies a flavor dimension (no need to specify it in each productsFlavor, cause we only declare one dimension).
    flavorDimensions "flavor_dimension_vector"

    productFlavors {
        app {
            applicationId "im.vector.app"
            // use the version name
            versionCode rootProject.ext.versionCodeProp
            versionName rootProject.ext.versionNameProp
            buildConfigField "boolean", "ALLOW_FCM_USE", "true"
            buildConfigField "String", "SHORT_FLAVOR_DESCRIPTION", "\"G\""
            buildConfigField "String", "FLAVOR_DESCRIPTION", "\"GooglePlay\""
        }

        appfdroid {
            applicationId "im.vector.alpha"
            // use the version name
            versionCode rootProject.ext.versionCodeProp
            versionName rootProject.ext.versionNameProp
            buildConfigField "boolean", "ALLOW_FCM_USE", "false"
            buildConfigField "String", "SHORT_FLAVOR_DESCRIPTION", "\"F\""
            buildConfigField "String", "FLAVOR_DESCRIPTION", "\"FDroid\""
        }
    }

    lintOptions {
        lintConfig file("lint.xml")
    }

    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }
}

apply plugin: 'org.jetbrains.kotlin.android.extensions'

androidExtensions {
    experimental = true
}

static def gitRevision() {
    def cmd = "git rev-parse --short HEAD"
    return cmd.execute().text.trim()
}

static def gitRevisionDate() {
    def cmd = "git show -s --format=%ci HEAD^{commit}"
    return cmd.execute().text.trim()
}

static def gitBranchName() {
    def cmd = "git name-rev --name-only HEAD"
    return cmd.execute().text.trim()
}

dependencies {
    // Kotlin
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    // Kotlin KTX
    implementation 'androidx.core:core-ktx:1.0.2'

    // Kotlin coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1'
    implementation "org.jetbrains.anko:anko-commons:0.10.8"

    // Note: do not upgrade to 1.0.3 because it lead to crashes at startup on Android 16
    implementation 'androidx.multidex:multidex:2.0.1'

    implementation 'me.leolin:ShortcutBadger:1.1.2@aar'

    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'androidx.preference:preference:1.0.0'
    implementation 'androidx.legacy:legacy-preference-v14:1.0.0'
    implementation 'com.jakewharton:butterknife:10.1.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    kapt 'com.jakewharton:butterknife-compiler:10.1.0'

    // Work manager
    // (Java only)
    implementation "android.arch.work:work-runtime:1.0.1"
    // Kotlin + coroutines
    implementation "android.arch.work:work-runtime-ktx:1.0.1"

    // UI
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.getbase:floatingactionbutton:1.10.1'
    implementation 'com.binaryfork:spanny:1.0.4'
    implementation 'com.github.chrisbanes:PhotoView:2.1.4'

    // Epoxy
    implementation 'com.airbnb.android:epoxy:3.7.0'
    implementation 'com.airbnb.android:mvrx:1.0.1'
    kapt 'com.airbnb.android:epoxy-processor:3.7.0'

    // Network
    implementation 'com.squareup.retrofit2:retrofit:2.6.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
    implementation "com.google.code.gson:gson:$gson_version"
    implementation "com.squareup.okhttp3:okhttp:$okhttp_version"
    implementation 'com.squareup.okhttp3:okhttp-urlconnection:3.12.1'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
    implementation 'com.facebook.stetho:stetho:1.5.0'
    implementation 'com.facebook.stetho:stetho-okhttp3:1.5.0'
    implementation 'com.squareup.okio:okio:1.17.2'

    // Use Glide library to display image (Gif supported)
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    kapt 'com.github.bumptech.glide:compiler:4.9.0'

    // Passphrase strength helper
    implementation 'com.nulab-inc:zxcvbn:1.2.5'

    implementation 'com.googlecode.libphonenumber:libphonenumber:8.9.12'

    //Alerter
    implementation 'com.tapadoo.android:alerter:4.0.3'

    /************* Matrix SDK management **************/
    // update settings.gradle
    // use the matrix SDK as external dependency
    implementation 'com.github.matrix-org:matrix-android-sdk:v0.9.35'
    // use the matrix SDK as a sub project
    // you have to uncomment some lines in settings.gradle
    //implementation project(':matrix-sdk')
    //implementation project(':matrix-sdk-crypto')
    //implementation project(':matrix-sdk-core')

    /************* jitsi **************/
    implementation('org.jitsi.react:jitsi-meet-sdk:2.2.2')

    /************* analytics **************/
    // another tracking than GA
    implementation 'org.matomo.sdk:tracker:4.0.2'

    /************* flavors management **************/

    // app flavor only
    appImplementation('com.google.firebase:firebase-messaging:20.0.0') {
        exclude group: 'com.google.firebase', module: 'firebase-core'
        exclude group: 'com.google.firebase', module: 'firebase-analytics'
        exclude group: 'com.google.firebase', module: 'firebase-measurement-connector'
    }

    // appfdroid flavor only

    // Test
    testImplementation 'junit:junit:4.12' // Test
    testImplementation 'androidx.test:runner:1.2.0'
    testImplementation 'androidx.test:rules:1.2.0'
    testImplementation 'org.mockito:mockito-core:2.23.0'
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.hamcrest:hamcrest-library:1.3'
    testImplementation 'org.apache.maven:maven-ant-tasks:2.1.3' // fixes issue on linux/mac
    testImplementation "org.robolectric:robolectric:4.0.2"

    // Robolectric
    androidTestImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test:rules:1.2.0'
}

//Ensure we never move past okhttp 3.12.x
configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        if (details.requested.group == 'com.squareup.okhttp3' && details.requested.name == 'okhttp') {
            details.useVersion '3.12.3'
            details.because '3.13+ dropped support for android <5'
        }
    }
}

if (!getGradle().getStartParameter().getTaskRequests().toString().contains("Appfdroid")
        && !getGradle().getStartParameter().getTaskRequests().toString().contains("assembleAndroidTest")) {
    apply plugin: 'com.google.gms.google-services'
}
