apply plugin: 'com.android.application'
apply plugin: 'com.github.triplet.play'
apply plugin: 'io.fabric'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android {
  def ext = rootProject.extensions.ext

  compileSdkVersion ext.android.compileSdk

  defaultConfig {
    minSdkVersion ext.android.minSdk
    targetSdkVersion ext.android.targetSdk

    applicationId ext.application.package
    versionCode ext.application.versionCode
    versionName ext.application.versionName

    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    multiDexEnabled true

    resValue "string", "default_package_name", ext.application.package

    // Api Keys
    def (fabricApiKey, flowUpApiKey) = apiKeysFromFile()
    manifestPlaceholders = [fabricApiKey: fabricApiKey]
    resValue "string", "flow_up_api_key", flowUpApiKey
  }

  sourceSets {
    main.java.srcDirs += 'src/main/kotlin'
  }

  compileOptions {
    sourceCompatibility ext.compiler.java
    targetCompatibility ext.compiler.java
  }

  // Enables long timeouts required on slow environments as Travis
  adbOptions {
    timeOutInMs 20 * 60 * 1000 // 20 minutes
    installOptions "-d", "-t"
  }

  dexOptions {
    preDexLibraries true
    javaMaxHeapSize '4G'
  }

  packagingOptions {
    exclude 'META-INF/services/javax.annotation.processing.Processor'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
  }

  lintOptions {
    ignore 'InvalidPackage'
  }

  signingConfigs {
    debug {
      storeFile file("../debug.keystore.jks")
    }
    release {
      storeFile file("../app.keystore.jks")
    }
  }

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

  flavorDimensions "sdk"

  productFlavors {
    fastBuild {
      dimension "sdk"

      // Enable pre-dexing to produce an APK that can be tested on
      // Android 5.0+ without the time-consuming DEX build processes.
      minSdkVersion 21
    }

    regular {
      dimension "sdk"

      minSdkVersion ext.android.minSdk
    }
  }
}

dependencies {
  def presentationDependencies = rootProject.ext.presentationDependencies
  def presentationTestDependencies = rootProject.ext.presentationTestDependencies

  implementation project(':domain')
  implementation project(':data')

  implementation presentationDependencies.kotlinStdLib
  implementation presentationDependencies.kotlinReflect
  implementation presentationDependencies.multiDex
  implementation presentationDependencies.flowUp
  implementation presentationDependencies.supportVersion
  implementation presentationDependencies.appCompat
  implementation presentationDependencies.design
  implementation presentationDependencies.recyclerView
  implementation presentationDependencies.preferenceCompat
  implementation presentationDependencies.fab
  implementation presentationDependencies.dexter
  implementation presentationDependencies.zxingAndroidEmbedded
  implementation presentationDependencies.robinHoodTicker
  implementation presentationDependencies.materialDialogs
  implementation(presentationDependencies.materialDrawer) {
    transitive = true
  }
  implementation(presentationDependencies.crashlytics) {
    transitive = true
  }

  kapt presentationDependencies.daggerCompiler
  implementation presentationDependencies.dagger
  implementation presentationDependencies.rxJava
  implementation presentationDependencies.rxAndroid
  compileOnly presentationDependencies.javaxAnnotation


  androidTestImplementation presentationTestDependencies.androidAnnotations
  androidTestImplementation presentationTestDependencies.supportRunner
  androidTestImplementation presentationTestDependencies.supportRules
  androidTestImplementation presentationTestDependencies.espressoCore
  androidTestImplementation presentationTestDependencies.espressoIntents
}

play {
  jsonFile = file('../gplay-secret.json')
  track = 'beta'
}

def (debugPassword, releaseKeystorePassword, releaseKeyAlias, releaseKeyPassword) =
signingConfigFromFile()
if (debugPassword != null && releaseKeystorePassword != null && releaseKeyAlias != null && releaseKeyPassword != null) {
  android.signingConfigs.debug.storePassword = debugPassword
  android.signingConfigs.debug.keyPassword = debugPassword
  android.signingConfigs.release.storePassword = releaseKeystorePassword
  android.signingConfigs.release.keyAlias = releaseKeyAlias
  android.signingConfigs.release.keyPassword = releaseKeyPassword
} else {
  println "Unable to find the right credentials to sign the apk :("
  android.buildTypes.release.signingConfig = null
}

def signingConfigFromFile() {
  println '-> signingConfigFromFile'
  Properties props = new Properties()
  def propFile = file('../signing.properties')

  if (propFile.canRead()) {
    props.load(new FileInputStream(propFile))

    if (props != null &&
        props.containsKey('DEBUG_PASSWORD') &&
        props.containsKey('RELEASE_STORE_PASSWORD') &&
        props.containsKey('RELEASE_KEY_ALIAS') &&
        props.containsKey('RELEASE_KEY_PASSWORD')) {

      def debugPassword = props['DEBUG_PASSWORD']
      def releaseKeystorePassword = props['RELEASE_STORE_PASSWORD']
      def releaseKeyAlias = props['RELEASE_KEY_ALIAS']
      def releaseKeyPassword = props['RELEASE_KEY_PASSWORD']

      return [debugPassword, releaseKeystorePassword, releaseKeyAlias, releaseKeyPassword]
    } else {
      println 'signing.properties found but some entries are missing'
    }
  } else {
    println 'signing.properties not found'
  }

  return [null, null, null]
}

def apiKeysFromFile() {
  println '-> apiKeysFromFile'
  Properties props = new Properties()
  def propFile = file('../api-keys.properties')

  if (propFile.canRead()) {
    props.load(new FileInputStream(propFile))

    if (props != null && props.containsKey('FABRIC') && props.containsKey('FLOW_UP')) {

      def fabricApiKey = props['FABRIC']
      def flowUpApiKey = props['FLOW_UP']

      return [fabricApiKey, flowUpApiKey]
    } else {
      println 'api-keys.properties found but some entries are missing'
    }
  } else {
    println 'api-keys.properties not found'
  }

  return [null, null]
}
