plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
}

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

// keystore for releasing this Android app
def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

// ensemble-configured properties
def ensembleProperties = new Properties()
def rootFlutterDir = rootDir.parentFile
def ensemblePropertiesFile = new File(rootFlutterDir, 'ensemble/ensemble.properties')
if (ensemblePropertiesFile.exists()) {
    ensemblePropertiesFile.withReader('UTF-8') { reader -> ensembleProperties.load(reader) }
}
def appId = ensembleProperties.getProperty('appId')
def appName = ensembleProperties.getProperty('appName')
def googleMapsAPIKey = ensembleProperties.getProperty('googleMapsAPIKey')
def androidMinSdkVersion = Integer.parseInt(ensembleProperties.getProperty('android.minSdkVersion'))
def androidCompileSdkVersion = Integer.parseInt(ensembleProperties.getProperty('android.compileSdkVersion'))
def androidTargetSdkVersion = Integer.parseInt(ensembleProperties.getProperty('android.targetSdkVersion'))
def auth0Domain = ensembleProperties.getProperty("auth0Domain")
def auth0Scheme = ensembleProperties.getProperty("auth0Scheme")
def branchTestKey = ensembleProperties.getProperty("branchTestKey")
def branchLiveKey = ensembleProperties.getProperty("branchLiveKey")
def moenagageWorkspaceId = ensembleProperties.getProperty("moenagageWorkspaceId")

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

android {
    namespace appId
    compileSdkVersion androidCompileSdkVersion
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
        coreLibraryDesugaringEnabled true
    }

    buildFeatures {
        buildConfig true
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

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

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId appId
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        minSdkVersion androidMinSdkVersion
        targetSdkVersion androidTargetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        
        // pass properties to native kotlin files
        buildConfigField("String", "MOENGAGE_WORKSPACE_ID", "\"${moenagageWorkspaceId}\"")

        // pass properties to AndroidManifest
        manifestPlaceholders.googleMapsAPIKey = googleMapsAPIKey
        manifestPlaceholders.appId = appId
        manifestPlaceholders.appName = appName

        manifestPlaceholders.auth0Domain = auth0Domain
        manifestPlaceholders.auth0Scheme = auth0Scheme
        manifestPlaceholders.branchTestKey = branchTestKey
        manifestPlaceholders.branchLiveKey = branchLiveKey
    }

    // signning configuration for your release version
    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
            storePassword keystoreProperties['storePassword']
        }
    }

    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }
}

flutter {
    source '../..'
}

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

    def work_version = "2.8.1"
    implementation "androidx.work:work-runtime-ktx:$work_version"
    api('androidx.work:work-runtime') {
        version {
            require '[2.1.0, 2.8.99]'
            prefer '$work_version'
        }
    }
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.1.4'
    
    // Material Design support
    implementation 'com.google.android.material:material:1.9.0'
}
