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 plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 31
    
    lintOptions {
        disable 'InvalidPackage'
    }    

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

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }
   
    defaultConfig {
        applicationId "com.krawieck.lemmur"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    signingConfigs {
        if (System.getenv("ANDROID_STORE_PATH")) {
            prod {
                keyAlias System.getenv("ANDROID_KEY_ALIAS")
                keyPassword System.getenv("ANDROID_KEY_PASSWORD")
                storeFile file(System.getenv("ANDROID_STORE_PATH"))
                storePassword System.getenv("ANDROID_STORE_PASSWORD")
            }
        } else {
            prod {
                keyAlias signingConfigs.debug.keyAlias
                keyPassword signingConfigs.debug.keyPassword
                storeFile signingConfigs.debug.storeFile
                storePassword signingConfigs.debug.storePassword
            }
        }
    }

    flavorDimensions "app"

    productFlavors {
        dev {
            dimension "app"
            applicationIdSuffix ".dev"
            versionNameSuffix "-dev"
            manifestPlaceholders = [
                appName: "lemmur DEV"
            ]
            signingConfig signingConfigs.debug
        }

        prod {
            dimension "app"
            manifestPlaceholders = [
                appName: "lemmur"
            ]
            signingConfig signingConfigs.prod
        }
    }

    buildTypes {
        debug {
            testCoverageEnabled true
            debuggable true
            minifyEnabled false
            signingConfig null
        }

        release {
            debuggable false
            minifyEnabled true
            shrinkResources false
            zipAlignEnabled true
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
