import com.android.build.OutputFile

apply plugin: 'com.android.application'
apply plugin: 'com.google.android.gms.oss-licenses-plugin'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

kapt {
    correctErrorTypes = true
}

androidExtensions {
    experimental = true
}

// Note: 2 digits max for each value
ext.versionMajor = 1
ext.versionMinor = 0
ext.versionPatch = 9

static def getGitTimestamp() {
    def cmd = 'git show -s --format=%ct'
    return cmd.execute().text.trim() as Long
}

static def generateVersionCodeFromTimestamp() {
    // It's unix timestamp, minus timestamp of October 3rd 2018 (first commit date) divided by 100: It's incremented by one every 100 seconds.
    // plus 20_000_000 for compatibility reason with the previous way the Version Code was computed
    // Note that the result will be multiplied by 10 when adding the digit for the arch
    return ((getGitTimestamp() - 1_538_524_800) / 100).toInteger() + 20_000_000
}

def generateVersionCodeFromVersionName() {
    // plus 4_000_000 for compatibility reason with the previous way the Version Code was computed
    // Note that the result will be multiplied by 10 when adding the digit for the arch
    return (versionMajor * 1_00_00 + versionMinor * 1_00 + versionPatch) + 4_000_000
}

def getVersionCode() {
    if (gitBranchName() == "develop") {
        return generateVersionCodeFromTimestamp()
    } else {
        return generateVersionCodeFromVersionName()
    }
}

static def gitRevision() {
    def cmd = "git rev-parse --short=8 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 fromEnv = System.env.BUILDKITE_BRANCH as String ?: ""

    if (!fromEnv.isEmpty()) {
        return fromEnv
    } else {
        // Note: this command return "HEAD" on Buildkite, so use the system env 'BUILDKITE_BRANCH' content first
        def cmd = "git rev-parse --abbrev-ref HEAD"
        return cmd.execute().text.trim()
    }
}

// For Google Play build, build on any other branch than master will have a "-dev" suffix
static def getGplayVersionSuffix() {
    if (gitBranchName() == "master") {
        return ""
    } else {
        return "-dev"
    }
}

static def gitTag() {
    def cmd = "git describe --exact-match --tags"
    return cmd.execute().text.trim()
}

// For F-Droid build, build on a not tagged commit will have a "-dev" suffix
static def getFdroidVersionSuffix() {
    if (gitTag() == "") {
        return "-dev"
    } else {
        return ""
    }
}

project.android.buildTypes.all { buildType ->
    buildType.javaCompileOptions.annotationProcessorOptions.arguments =
            [
                    validateEpoxyModelUsage: String.valueOf(buildType.name == 'debug')
            ]
}

// map for the version codes last digit
// x86 must have greater values than arm
// 64 bits have greater value than 32 bits
ext.abiVersionCodes = ["armeabi-v7a": 1, "arm64-v8a": 2, "x86": 3, "x86_64": 4].withDefault { 0 }

def buildNumber = System.env.BUILDKITE_BUILD_NUMBER as Integer ?: 0

android {
    compileSdkVersion 29

    // Due to a bug introduced in Android gradle plugin 3.6.0, we have to specify the ndk version to use
    // Ref: https://issuetracker.google.com/issues/144111441
    ndkVersion "21.3.6528147"

    defaultConfig {
        applicationId "im.vector.app"
        // Set to API 21: see #405
        minSdkVersion 21
        targetSdkVersion 29
        multiDexEnabled true

        // `develop` branch will have version code from timestamp, to ensure each build from CI has a incremented versionCode.
        // Other branches (master, features, etc.) will have version code based on application version.
        versionCode project.getVersionCode()

        // Required for sonar analysis
        versionName "${versionMajor}.${versionMinor}.${versionPatch}-sonar"

        buildConfigField "String", "GIT_REVISION", "\"${gitRevision()}\""
        resValue "string", "git_revision", "\"${gitRevision()}\""

        buildConfigField "String", "GIT_REVISION_DATE", "\"${gitRevisionDate()}\""
        resValue "string", "git_revision_date", "\"${gitRevisionDate()}\""

        buildConfigField "String", "GIT_BRANCH_NAME", "\"${gitBranchName()}\""
        resValue "string", "git_branch_name", "\"${gitBranchName()}\""

        buildConfigField "String", "BUILD_NUMBER", "\"${buildNumber}\""
        resValue "string", "build_number", "\"${buildNumber}\""

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

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

        // 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
            }
        }

        applicationVariants.all { variant ->
            variant.outputs.each { output ->
                def baseAbiVersionCode = project.ext.abiVersionCodes.get(output.getFilter(OutputFile.ABI))
                // Known limitation: it does not modify the value in the BuildConfig.java generated file
                output.versionCodeOverride = variant.versionCode * 10 + baseAbiVersionCode
            }
        }
    }

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

    buildTypes {
        debug {
            applicationIdSuffix ".debug"
            resValue "string", "app_name", "Element dbg"

            resValue "bool", "debug_mode", "true"
            buildConfigField "boolean", "LOW_PRIVACY_LOG_ENABLE", "false"
            // Set to true if you want to enable strict mode in debug
            buildConfigField "boolean", "ENABLE_STRICT_MODE_LOGS", "false"

            signingConfig signingConfigs.debug
        }

        release {
            resValue "string", "app_name", "Element (Riot.im)"

            resValue "bool", "debug_mode", "false"
            buildConfigField "boolean", "LOW_PRIVACY_LOG_ENABLE", "false"
            buildConfigField "boolean", "ENABLE_STRICT_MODE_LOGS", "false"

            postprocessing {
                removeUnusedCode true
                removeUnusedResources true
                // We do not activate obfuscation as it makes it hard then to read crash reports, and it's a bit useless on an open source project :)
                obfuscate false
                optimizeCode true
                proguardFiles 'proguard-rules.pro'
            }
        }
    }

    flavorDimensions "store"

    productFlavors {
        gplay {
            dimension "store"

            versionName "${versionMajor}.${versionMinor}.${versionPatch}${getGplayVersionSuffix()}"

            resValue "bool", "isGplay", "true"
            buildConfigField "boolean", "ALLOW_FCM_USE", "true"
            buildConfigField "String", "SHORT_FLAVOR_DESCRIPTION", "\"G\""
            buildConfigField "String", "FLAVOR_DESCRIPTION", "\"GooglePlay\""
        }

        fdroid {
            dimension "store"

            versionName "${versionMajor}.${versionMinor}.${versionPatch}${getFdroidVersionSuffix()}"

            resValue "bool", "isGplay", "false"
            buildConfigField "boolean", "ALLOW_FCM_USE", "false"
            buildConfigField "String", "SHORT_FLAVOR_DESCRIPTION", "\"F\""
            buildConfigField "String", "FLAVOR_DESCRIPTION", "\"FDroid\""
        }
    }

    lintOptions {
        lintConfig file("lint.xml")

        abortOnError true
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }

    sourceSets {
        androidTest {
            java.srcDirs += "src/sharedTest/java"
        }
        test {
            java.srcDirs += "src/sharedTest/java"
        }
    }
}

dependencies {

    def epoxy_version = '3.11.0'
    def fragment_version = '1.2.5'
    def arrow_version = "0.8.2"
    def coroutines_version = "1.3.8"
    def markwon_version = '4.1.2'
    def big_image_viewer_version = '1.6.2'
    def glide_version = '4.11.0'
    def moshi_version = '1.8.0'
    def daggerVersion = '2.25.4'
    def autofill_version = "1.0.0"
    def work_version = '2.4.0'
    def arch_version = '2.1.0'
    def lifecycle_version = '2.2.0'

    implementation project(":matrix-sdk-android")
    implementation project(":matrix-sdk-android-rx")
    implementation project(":diff-match-patch")
    implementation project(":multipicker")
    implementation project(":attachment-viewer")
    implementation 'com.android.support:multidex:1.0.3'

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"

    implementation "androidx.recyclerview:recyclerview:1.2.0-alpha05"
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation "androidx.fragment:fragment:$fragment_version"
    implementation "androidx.fragment:fragment-ktx:$fragment_version"
    // Keep at 2.0.0-beta4 at the moment, as updating is breaking some UI
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'
    implementation 'androidx.core:core-ktx:1.3.1'

    implementation "org.threeten:threetenbp:1.4.0:no-tzdb"
    implementation "com.gabrielittner.threetenbp:lazythreetenbp:0.7.0"

    implementation "com.squareup.moshi:moshi-adapters:$moshi_version"
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
    kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshi_version"

    // Log
    implementation 'com.jakewharton.timber:timber:4.7.1'

    // Debug
    implementation 'com.facebook.stetho:stetho:1.5.1'

    // Phone number https://github.com/google/libphonenumber
    implementation 'com.googlecode.libphonenumber:libphonenumber:8.10.23'

    // rx
    implementation 'io.reactivex.rxjava2:rxkotlin:2.3.0'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
    implementation 'com.jakewharton.rxrelay2:rxrelay:2.1.1'
    // RXBinding
    implementation 'com.jakewharton.rxbinding3:rxbinding:3.0.0'
    implementation 'com.jakewharton.rxbinding3:rxbinding-appcompat:3.0.0'
    implementation 'com.jakewharton.rxbinding3:rxbinding-material:3.0.0'

    implementation("com.airbnb.android:epoxy:$epoxy_version")
    implementation "com.airbnb.android:epoxy-glide-preloading:$epoxy_version"
    kapt "com.airbnb.android:epoxy-processor:$epoxy_version"
    implementation "com.airbnb.android:epoxy-paging:$epoxy_version"
    implementation 'com.airbnb.android:mvrx:1.3.0'

    // Work
    implementation "androidx.work:work-runtime-ktx:$work_version"

    // Paging
    implementation "androidx.paging:paging-runtime-ktx:2.1.2"

    // Functional Programming
    implementation "io.arrow-kt:arrow-core:$arrow_version"

    // Pref
    implementation 'androidx.preference:preference:1.1.1'

    // UI
    implementation 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
    implementation 'com.google.android.material:material:1.3.0-alpha01'
    implementation 'me.gujun.android:span:1.7'
    implementation "io.noties.markwon:core:$markwon_version"
    implementation "io.noties.markwon:html:$markwon_version"
    implementation 'com.googlecode.htmlcompressor:htmlcompressor:1.4'
    implementation 'me.saket:better-link-movement-method:2.2.0'
    implementation 'com.google.android:flexbox:1.1.1'
    implementation "androidx.autofill:autofill:$autofill_version"
    implementation 'com.github.vector-im:PFLockScreen-Android:1.0.0-beta10'

    // Custom Tab
    implementation 'androidx.browser:browser:1.2.0'

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

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

    implementation 'com.otaliastudios:autocomplete:1.1.0'

    // Butterknife
    implementation 'com.jakewharton:butterknife:10.2.0'
    kapt 'com.jakewharton:butterknife-compiler:10.2.0'

    // Shake detection
    implementation 'com.squareup:seismic:1.0.2'

    // Image Loading
    implementation "com.github.piasy:BigImageViewer:$big_image_viewer_version"
    implementation "com.github.piasy:GlideImageLoader:$big_image_viewer_version"
    implementation "com.github.piasy:ProgressPieIndicator:$big_image_viewer_version"
    implementation "com.github.piasy:GlideImageViewFactory:$big_image_viewer_version"

    // implementation 'com.github.MikeOrtiz:TouchImageView:3.0.2'
    implementation 'com.github.chrisbanes:PhotoView:2.0.0'

    implementation "com.github.bumptech.glide:glide:$glide_version"
    kapt "com.github.bumptech.glide:compiler:$glide_version"
    implementation 'com.danikula:videocache:2.7.1'
    implementation 'com.github.yalantis:ucrop:2.2.6'

    // Badge for compatibility
    implementation 'me.leolin:ShortcutBadger:1.1.22@aar'

    // DI
    implementation "com.google.dagger:dagger:$daggerVersion"
    kapt "com.google.dagger:dagger-compiler:$daggerVersion"
    compileOnly 'com.squareup.inject:assisted-inject-annotations-dagger2:0.5.0'
    kapt 'com.squareup.inject:assisted-inject-processor-dagger2:0.5.0'

    // gplay flavor only
    gplayImplementation('com.google.firebase:firebase-messaging:20.2.4') {
        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'
    }

    // OSS License, gplay flavor only
    gplayImplementation 'com.google.android.gms:play-services-oss-licenses:17.0.0'

    implementation "androidx.emoji:emoji-appcompat:1.1.0"

    implementation 'com.github.BillCarsonFr:JsonViewer:0.5'

    // WebRTC
    // org.webrtc:google-webrtc is for development purposes only
    // implementation 'org.webrtc:google-webrtc:1.0.+'
    implementation('org.jitsi.react:jitsi-meet-sdk:2.9.3') { transitive = true }

    // QR-code
    // Stick to 3.3.3 because of https://github.com/zxing/zxing/issues/1170
    implementation 'com.google.zxing:core:3.3.3'
    implementation 'me.dm7.barcodescanner:zxing:1.9.13'

    // TESTS
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.amshove.kluent:kluent-android:1.44'
    // Plant Timber tree for test
    testImplementation 'net.lachlanmckee:timber-junit-rule:1.0.1'

    // Activate when you want to check for leaks, from time to time.
    //debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.3'

    androidTestImplementation 'androidx.test:core:1.2.0'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test:rules:1.2.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    androidTestImplementation 'org.amshove.kluent:kluent-android:1.44'
    androidTestImplementation "androidx.arch.core:core-testing:$arch_version"
    // Plant Timber tree for test
    androidTestImplementation 'net.lachlanmckee:timber-junit-rule:1.0.1'
}

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