plugins {
    id "com.android.application"
    id 'com.google.gms.google-services'
    id "kotlin-android"
    // The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
    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)
    }
}

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

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

def dartDefines = [:];
if (project.hasProperty('dart-defines')) {
    dartDefines = dartDefines + project.property('dart-defines')
            .split(',')
            .collectEntries { entry ->
                def pair = new String(entry.decodeBase64(), 'UTF-8').split('=')
                pair.length == 2 ? [(pair.first()): pair.last()] : [:]
            }
}

def keyProperties = new Properties()
def keyPropertiesFile = rootProject.file("key.properties")
if (keyPropertiesFile.exists()) {
    keyPropertiesFile.withReader('UTF-8') { reader ->
        keyProperties.load(reader)
    }
}

task selectGoogleServicesJson(type: Copy) {
    def sourceFile = file("src/${dartDefines.FLAVOR}/google-services.json")
    if (sourceFile.exists()) {
        from sourceFile
        into './'
    } else {
        logger.warn("Warning: google-services.json for flavor ${flavor} does not exist.")
    }
}

def dependentTasks = [
    'processDebugGoogleServices',
    'processReleaseGoogleServices',
    'compileFlutterBuildDebug',
    'compileFlutterBuildRelease',
    'mergeDebugAssets',
    'mergeDebugShaders',
    'mergeReleaseAssets',
    'mergeReleaseShaders',
    'generateDebugResources',
    'generateReleaseResources',
    'mergeDebugJniLibFolders',
    'mergeReleaseJniLibFolders'
]

afterEvaluate {
    tasks.matching { task -> dependentTasks.contains(task.name) }.all { task ->
        task.dependsOn selectGoogleServicesJson
    }
}

android {
    namespace = "jp.flutterkaigi.conf2024"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion

    compileOptions {
        // Flag to enable support for the new language APIs
        coreLibraryDesugaringEnabled true
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId = "jp.flutterkaigi.conf2024"
        if (dartDefines.APP_ID_SUFFIX != null) {
            applicationIdSuffix = "${dartDefines.APP_ID_SUFFIX}"
        }
        // 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.
        minSdk = flutter.minSdkVersion
        targetSdk = flutter.targetSdkVersion
        versionCode = flutterVersionCode.toInteger()
        versionName = flutterVersionName
        resValue "string", "appLabel", "${dartDefines.APP_NAME}"
    }

    signingConfigs {
        release {
            if (keyPropertiesFile.exists()) {
                keyAlias keyProperties.getProperty('keyAlias')
                keyPassword keyProperties.getProperty('keyPassword')
                storeFile rootProject.file(keyProperties.getProperty('keyStoreFilePath'))
                storePassword keyProperties.getProperty('keyStorePassword')
            }
        }
    }

    buildTypes {
        release {
            signingConfig = signingConfigs.release
            ndk {
                debugSymbolLevel = 'SYMBOL_TABLE'
            }
        }
    }
}

dependencies {
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.3'
}

flutter {
    source = "../.."
}
