def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    // for release using credentials stored in a local file
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
} else {
    // for release using credentials in environment variables set up by Github Actions
    // warning: in property file, single quotes should be escaped with a backslash
    // but they should not be escaped when stored in env variables
    keystoreProperties['storeFile'] = System.getenv('AVES_STORE_FILE')
    keystoreProperties['storePassword'] = System.getenv('AVES_STORE_PASSWORD')
    keystoreProperties['keyAlias'] = System.getenv('AVES_KEY_ALIAS')
    keystoreProperties['keyPassword'] = System.getenv('AVES_KEY_PASSWORD')
    keystoreProperties['googleApiKey'] = System.getenv('AVES_GOOGLE_API_KEY')
}

android {
    compileSdkVersion 30 // latest (or latest-1 if the sources of latest SDK are unavailable)

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        applicationId "deckers.thibault.aves"
        // some Java 8 APIs (java.util.stream, etc.) require minSdkVersion 24
        // Gradle plugin 4.0 desugaring features allow targeting older SDKs
        // but Flutter (as of v1.17.3) fails to run in release mode when using Gradle plugin 4.0:
        // https://github.com/flutter/flutter/issues/58247
        minSdkVersion 24
        targetSdkVersion 30 // same as compileSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        manifestPlaceholders = [googleApiKey:keystoreProperties['googleApiKey']]
    }

//    compileOptions {
//        // enable support for Java 8 language APIs (stream, optional, etc.)
//        coreLibraryDesugaringEnabled true
//        sourceCompatibility JavaVersion.VERSION_1_8
//        targetCompatibility JavaVersion.VERSION_1_8
//    }

    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
            storePassword keystoreProperties['storePassword']
        }
    }

    buildTypes {
        debug {
            applicationIdSuffix ".debug"
        }
        profile {
            applicationIdSuffix ".profile"
        }
        release {
            signingConfig signingConfigs.release
        }
    }
}

flutter {
    source '../..'
}

repositories {
    maven {
        url "https://s3.amazonaws.com/repo.commonsware.com"
    }
}

dependencies {
    // enable support for Java 8 language APIs (stream, optional, etc.)
//    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.9'

    implementation "androidx.exifinterface:exifinterface:1.2.0"
    implementation 'com.commonsware.cwac:document:0.4.1'
    implementation 'com.drewnoakes:metadata-extractor:2.14.0'
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    implementation 'com.google.guava:guava:29.0-android'

    annotationProcessor 'androidx.annotation:annotation:1.1.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

    compileOnly rootProject.findProject(':streams_channel')
}

apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'
