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

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

    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        applicationId "nl.digital_me.dummyandroidapp"
        minSdkVersion 14
        targetSdkVersion rootProject.ext.compileSdkVersion
        versionCode project.hasProperty('versionCode') ? project.property('versionCode') as int : 67
        versionName project.hasProperty('versionName') ? project.property('versionName') : "0.0.5"
        multiDexEnabled true
    }

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

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

        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-glide.pro', 'proguard-fresco.pro', 'proguard-rules.pro'
            debuggable true
        }
    }

    flavorDimensions "env"

    productFlavors { // May need to match branch name, passed by Jenkins PL as environment variable named LAZY_BRANCH
        systemtest {
            resValue "string", "app_name", "DummyAnd STst"
            applicationIdSuffix ".stst"
//            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.each { 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.assemble.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 {
    maven { url 'https://maven.fabric.io/public' }
    mavenCentral()
}

dependencies {
    //compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    implementation "com.android.support:design:${rootProject.ext.supportLibVersion}"
    implementation 'com.android.support:multidex:1.0.3'
}
