/*
 * TimeLimit Copyright <C> 2019 Jonas Lochmann
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "androidx.navigation.safeargs.kotlin"
apply plugin: 'kotlin-kapt'
apply plugin: 'com.github.triplet.play'
apply plugin: 'com.getkeepsafe.dexcount'

androidExtensions {
    experimental = true
}

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "io.timelimit.android"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 34
        versionName "0.5.8 Beta"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        kapt {
            arguments {
                arg("room.schemaLocation", "$projectDir/schemas".toString())
            }
        }
        buildConfigField 'String', 'googlePlayKey', '"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqxGPPhjSGrSI4jfIJmw83dDGTO4Sg74nXhOoHl5Omyvxa1Hv+Tzp/wC97emXSmKq9Li2bV4q3yJLjtLJtoDmh9V1vpdyjH13qOidhIUYOtlkGexbwKz56jZwRFXGBIy0RFDDr3j5DLfUWMk0dqplLgIHDqsdUNY50419oMGKAOCX3DEw+ky+0zMY/hlUkZHnd0HLi0CYNkdZyMMYnk8OiOZ3YoDU/vz9PTf93MCAONYcXBlo3Y5hdVY2tyH4qNZh1eyqe0Trz4fRf+y1ljoa0s4ZqYskbsgpy+zUbGb82cxmATAsrdJMnNKMEa30HBvfMzKiOPR6sq3A8PPPoWT5KQIDAQAB"'
    }

    flavorDimensions 'api', 'channel', 'server'
    productFlavors {
        storeVersion {
            dimension 'channel'
            buildConfigField 'boolean', 'storeCompilant', 'true'
            applicationIdSuffix ".store"
        }
        directVersion {
            dimension 'channel'
            buildConfigField 'boolean', 'storeCompilant', 'false'
            applicationIdSuffix ".direct"
        }

        googleApi {
            dimension 'api'
            buildConfigField 'boolean', 'useGoogleApis', 'true'
            buildConfigField 'String', "googleAuthClientId", '"700283994167-4vklse9lj88oo41bd5aav8t0r9ohjhh6.apps.googleusercontent.com"'
            applicationIdSuffix ".google"
        }
        noGoogleApi {
            dimension 'api'
            buildConfigField 'boolean', 'useGoogleApis', 'false'
            applicationIdSuffix ".aosp"
        }

        developmentServer {
            dimension 'server'
            // this is your own computer when using the Android Emulator
            buildConfigField 'String', 'serverUrl', '"http://10.0.2.2:8080"'
            buildConfigField 'boolean', 'hasServer', 'true'
            applicationIdSuffix '.developmentserver'
        }
        productionServer {
            dimension 'server'
            buildConfigField 'String', 'serverUrl', '"https://api.timelimit.io"'
            buildConfigField 'String', 'serverDomain', '"api.timelimit.io"'
            buildConfigField 'boolean', 'hasServer', 'true'
            // no applicationIdSuffix
        }
        noServer {
            dimension 'server'
            buildConfigField 'String', 'serverUrl', '"https://127.0.0.1"'
            buildConfigField 'boolean', 'hasServer', 'false'
            applicationIdSuffix '.offline'
        }
    }

    signingConfigs {
        release {
            // empty
        }
    }

    variantFilter { variant ->
        def dim = variant.flavors.collectEntries {
            [(it.productFlavor.dimension): it.productFlavor.name]
        }

        if (dim.channel == 'storeVersion' && dim.api == 'noGoogleApi') {
            variant.setIgnore(true)
        }

        if (dim.channel == 'directVersion' && dim.api == 'googleApi') {
            variant.setIgnore(true)
        }

        if (dim.server == 'noServer' && dim.api == 'googleApi') {
            variant.setIgnore(true)
        }
    }

    lintOptions {
        disable 'GoogleAppIndexingWarning'
        baseline file("lint-baseline.xml")
    }

    dataBinding {
        enabled = true
    }

    buildTypes {
        debug {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

dependencies {
    def nav_version = "1.0.0-rc02"
    def room_version = "2.0.0"
    def work_version = '1.0.0-rc02'
    def paging_version = "2.1.0"

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.cardview:cardview:1.0.0'

    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'

    implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version"
    implementation "android.arch.navigation:navigation-ui:$nav_version"
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'

    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
    testImplementation "androidx.room:room-testing:$room_version"

    implementation "androidx.paging:paging-runtime:$paging_version"

    implementation "android.arch.work:work-runtime:$work_version"
    implementation "android.arch.work:work-runtime-ktx:$work_version"
    androidTestImplementation "android.arch.work:work-testing:$work_version"

    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.0'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.0'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

    implementation 'com.jakewharton.threetenabp:threetenabp:1.1.0'

    implementation 'org.mindrot:jbcrypt:0.4'

    implementation 'com.getkeepsafe.taptargetview:taptargetview:1.11.0'

    implementation 'com.google.android:flexbox:1.0.0'

    implementation 'com.jaredrummler:android-device-names:1.1.7'

    implementation 'com.squareup.okhttp3:okhttp:3.11.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.8.1'

    googleApiImplementation 'com.google.android.gms:play-services-auth:16.0.1'

    implementation 'org.solovyev.android:checkout:1.2.1'

    implementation('io.socket:socket.io-client:1.0.0') {
        exclude group: 'org.json', module: 'json'
    }
}

play {
    jsonFile = file("play-key.json")
    track = "production"
}

// based on https://stackoverflow.com/a/20573171
def propsFile = project.file('keystore.properties')
def configName = 'release'

if (propsFile.exists() && android.signingConfigs.hasProperty(configName)) {
    def props = new Properties()
    props.load(new FileInputStream(propsFile))
    android.signingConfigs[configName].storeFile = file(props['storeFile'])
    android.signingConfigs[configName].storePassword = props['storePassword']
    android.signingConfigs[configName].keyAlias = props['keyAlias']
    android.signingConfigs[configName].keyPassword = props['keyPassword']
}
