apply plugin: 'com.android.application'

allprojects {
    repositories {
        mavenLocal()
        google()
        maven {
            name "vsts-maven-adal-android"
            url "https://identitydivision.pkgs.visualstudio.com/_packaging/AndroidADAL/maven/v1"
            credentials {
                username System.getenv("ENV_VSTS_MVN_ANDROIDADAL_USERNAME") != null ? System.getenv("ENV_VSTS_MVN_ANDROIDADAL_USERNAME") : project.findProperty("vstsUsername")
                password System.getenv("ENV_VSTS_MVN_ANDROIDADAL_ACCESSTOKEN") != null ? System.getenv("ENV_VSTS_MVN_ANDROIDADAL_ACCESSTOKEN") : project.findProperty("vstsMavenAccessToken")
            }
        }
        mavenCentral()
    }
}

def adalVersion = "4.8.18"
def commonVersion = "18.2.2"

if (project.hasProperty("distAdalVersion")) {
    println "Overriding adal version to dist property " + distAdalVersion
    adalVersion = distAdalVersion
}

println "Building with adal " + adalVersion

if (project.hasProperty("distCommonVersion")) {
    println "Overriding common version to dist property " + distCommonVersion
    commonVersion = distCommonVersion
}

println "Building with common " + commonVersion

def destApplicationId = "com.microsoft.aad.adal.userappwithbroker"
if (project.hasProperty("buildApplicationId")) {
    println "Overriding applicationId to " + buildApplicationId
    destApplicationId = buildApplicationId
}

println "Using application Id " + destApplicationId
android {
    namespace "com.microsoft.aad.adal.example.userappwithbroker"
    compileSdk rootProject.ext.compileSdkVersion

    compileOptions {
        // Sets Java compatibility to Java 8
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

   buildFeatures {
       buildConfig = true
   }
    defaultConfig {
        multiDexEnabled true
        applicationId destApplicationId
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
    }

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

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

        release {
            def debugKeyFile = rootProject.file("gradle/debug.keystore")
            if(debugKeyFile.exists()){
                storeFile rootProject.file("gradle/debug.keystore")
                storePassword "android"
                keyAlias "androiddebugkey"
                keyPassword "android"
            }
        }
    }

    flavorDimensions 'main'

    productFlavors {
        local{

        }
        dist{

        }
    }

    buildTypes {
        debug {
            buildConfigField("String", "REGULAR_REDIDRECT_URI", "\"msauth://com.microsoft.aad.adal.userappwithbroker/1wIqXSqBj7w%2Bh11ZifsnqwgyKrY%3D\"")
        }
        release {
            signingConfig signingConfigs.release
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt')
            buildConfigField("String", "REGULAR_REDIDRECT_URI", "\"msauth://com.microsoft.aad.adal.userappwithbroker/1wIqXSqBj7w%2Bh11ZifsnqwgyKrY%3D\"")
        }
    }

    lintOptions {
        abortOnError false
    }
}

dependencies {
    // Compile Dependencies
    implementation "com.google.android.material:material:$rootProject.ext.materialVersion"

    localImplementation(project(':adal')) {
        exclude module: 'common'
    }

    distImplementation("com.microsoft.aad:adal:${adalVersion}") {
        exclude module: 'support-v4'
        exclude module: 'android'
        exclude module: 'common'
    }

    localApi(project(':common'))
    distApi(group: 'com.microsoft.identity', name: 'common', version: "${commonVersion}")
}
