// Load a specific properties file to access the keystore if exists
def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file("keystore.properties")

if (keystorePropertiesFile.exists()) {
    // Load keystore properties from file
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
} else {
    // Load keystore properties from environment
    def env = System.getenv()
    if (env['ANDROID_KEYALIAS']) keystoreProperties.put('keyAlias', env['ANDROID_KEYALIAS'])
    else keystoreProperties.put('keyAlias', "dappre")
    if (env['ANDROID_KEYPASSWORD']) keystoreProperties.put('keyPassword', env['ANDROID_KEYPASSWORD'])
    else keystoreProperties.put('keyPassword', "dappre")
    if (env['ANDROID_STOREFILE']) keystoreProperties.put('storeFile', env['ANDROID_STOREFILE'])
    else keystoreProperties.put('storeFile', ".")
    if (env['ANDROID_STOREPASSWORD']) keystoreProperties.put('storePassword', env['ANDROID_STOREPASSWORD'])
    else keystoreProperties.put('storePassword', "dappre")
}

apply plugin: 'com.android.application'
//apply plugin: 'jacoco'
//apply plugin: 'kotlin-android'
//apply plugin: 'kotlinx-serialization'
//apply plugin: 'kotlin-android-extensions'
//apply plugin: 'kotlin-kapt'
//apply plugin: 'kotlin-allopen'
//apply plugin: 'androidx.navigation.safeargs.kotlin'
apply plugin: 'com.google.firebase.crashlytics'
apply plugin: 'com.google.gms.google-services'

android {
    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile file(keystoreProperties['storeFile'])
            storePassword keystoreProperties['storePassword']
        }
    }

    compileSdkVersion build_versions.compile_sdk
    buildToolsVersion build_versions.build_tools

    dataBinding {
        enabled = true
    }
//    viewBinding {
//        enabled = true
//    }

//    kapt {
//        correctErrorTypes true
//        javacOptions {
//            // Increase the max count of errors from annotation processors.
//            // Default is 100.
//            option("-Xmaxerrs", 500)
//        }
//    }

    defaultConfig {
        applicationId "net.dolden.dummyand"
        minSdkVersion build_versions.min_sdk
        targetSdkVersion build_versions.target_sdk
        versionCode project.hasProperty('versionCode') ? project.property('versionCode') as int : 80
        versionName project.hasProperty('versionName') ? project.property('versionName') : "0.0.5"
        vectorDrawables.useSupportLibrary true
        multiDexEnabled true

        buildConfigField "boolean", "CRASHLYTICS", "true"

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

    dexOptions {
        maxProcessCount 1
        threadCount 4
        javaMaxHeapSize "512m"
        preDexLibraries false
    }

    buildTypes {
        release {
            signingConfig signingConfigs.release
            minifyEnabled true
            // When minifyEnabled is set to true, Crashlytics automatically
            // uploads mapping files because the plugin detects that obfuscation
            // is enabled. mappingFileUploadEnabled defaults to true if
            // minifyEnabled is true.
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-glide.pro', 'proguard-fresco.pro', 'proguard-rules.pro'
            debuggable false
        }
        debug {
            minifyEnabled true
            firebaseCrashlytics {
                // If you don't need crash reporting for your debug build,
                // you can speed up your build by disabling mapping file uploading.
                mappingFileUploadEnabled false
            }
            useProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-glide.pro', 'proguard-fresco.pro', 'proguard-rules.pro'
            debuggable true
            testCoverageEnabled !project.hasProperty('android.injected.invoked.from.ide')
            ext.enableCrashlytics = false
        }
    }

    testOptions {
        unitTests.includeAndroidResources = true
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
//    kotlinOptions {
//        jvmTarget = "1.8"
//    }
    sourceSets {
        androidTest.java.srcDirs += "src/test-common/java"
        test.java.srcDirs += "src/test-common/java"
        test.java.srcDirs += "../networking/src/test/java"
    }
    lintOptions {
        lintConfig rootProject.file('lint.xml')
        checkReleaseBuilds false
    }

    flavorDimensions "env"

    productFlavors { // May need to match branch name, passed by Jenkins PL as environment variable named BRANCH_NAME
        systemtest {
            resValue "string", "app_name", "DummyAnd STst"
            //applicationIdSuffix ".stst"
            applicationIdSuffix ".dev2"
//            manifestPlaceholders = [errorBaseUrl   : "https://systemtest-card.testonly.digital-me.nl/card",
//                                    cardNodeBaseUrl: "https://systemtest-card.testonly.digital-me.nl/card",
//                                    userNodeBaseUrl: "https://systemtest-user.testonly.digital-me.nl/user"]
        }

        acceptance {
            resValue "string", "app_name", "DummyAnd Acc"
            applicationIdSuffix ".acc"
//            manifestPlaceholders = [errorBaseUrl   : "https://card.dolden.net/card",
//                                    cardNodeBaseUrl: "https://card.dolden.net/card",
//                                    userNodeBaseUrl: "https://user.dolden.net/user"]
        }

        dev2 {
            resValue "string", "app_name", "DummyAnd Dev2"
            applicationIdSuffix ".dev2"
//            manifestPlaceholders = [errorBaseUrl   : "https://dev2-card.testonly.digital-me.nl/card",
//                                    cardNodeBaseUrl: "https://dev2-card.testonly.digital-me.nl/card",
//                                    userNodeBaseUrl: "https://dev2-user.testonly.digital-me.nl/user"]
        }

        production {
            resValue "string", "app_name", "DummyAnd"
//            manifestPlaceholders = [errorBaseUrl   : "https://card.digital-me.nl/card",
//                                    cardNodeBaseUrl: "https://card.digital-me.nl/card",
//                                    userNodeBaseUrl: "https://user.digital-me.nl/user"]
        }
    }

    applicationVariants.all { variant ->
        if (variant.buildType.name == "release") {
            variant.outputs.all { output ->
                // Avoid to rename, so Fastlane default will work for now
                //output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace("app-release.apk", "dappre_${versionName}.${versionCode}.apk"))

                if (variant.getBuildType().isMinifyEnabled()) {
                    variant.getAssembleProvider().configure() {
						it.doLast {
                            copy {
                                from variant.mappingFile
                                into output.outputFile.parent
                                // Avoid to rename, so Fastlane default will work for now
                                //rename {
                                //    String fileName ->
                                //        "mapping_${versionName}.${versionCode}.txt"
                                //}
                            }
                        }
                    }
                }
            }
        }
    }
}

repositories {
    mavenCentral()
}

//jacoco {
//    toolVersion = "0.8.4+"
//}

buildscript {
    repositories {
        jcenter()
        google()
        mavenLocal()
    }
    dependencies {
//        classpath 'com.quinn.hunter:hunter-debug-plugin:0.9.7'
//        classpath 'com.quinn.hunter:hunter-timing-plugin:0.9.1'
//        classpath 'com.quinn.hunter:hunter-transform:0.9.3'
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')

    // plugins
//    implementation 'com.quinn.hunter:hunter-debug-library:0.9.6'
//    implementation 'com.quinn.hunter:hunter-timing-library:0.9.4'

    debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.2'

    // KAPT
//    kapt deps.room.compiler
//    kapt deps.glide.compiler
//    kapt 'androidx.annotation:annotation:1.1.0'

    //noinspection GradleDependency
//    kapt deps.data_binding_compiler
//    kapt deps.dagger.android_support_compiler
//    kapt deps.dagger.compiler

    // kapt deps.dagger.assisted_inject
//    kapt 'com.squareup.inject:assisted-inject-processor-dagger2:0.5.2'
//
//    kapt deps.lifecycle.compiler
//    implementation deps.kotlin.stdlib8
//    implementation 'org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.20.0-1.3.70-eap-274-2'
//    implementation 'com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.5.0'
    implementation deps.firebase_crashlytics
    implementation deps.firebase
//    implementation deps.firebase_messaging
//    implementation deps.firebase_dynamic_links
    implementation deps.firebase_analytics

    // [test] dependencies
    testImplementation deps.junit
    testImplementation deps.mockito.core

    implementation deps.support.app_compat
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.multidex:multidex:2.0.1'
}
