plugins {
    id "com.android.application"
    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 keystorePropertiesFile = rootProject.file('key.properties')
def keystoreProperties = new Properties()

if (keystorePropertiesFile.exists()) {
    keystorePropertiesFile.withReader('UTF-8') { reader ->
        keystoreProperties.load(reader)
    }
} else {
    keystoreProperties['storeFile'] = '.'
}


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 = "de.enough.enough_mail_app"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion

    compileOptions {
        // Enable core library desugaring
        coreLibraryDesugaringEnabled true
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId = "de.enough.enough_mail_app"
        // 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
    }

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

    buildTypes {
        release {
            if (keystorePropertiesFile.exists()) {
                signingConfig signingConfigs.release
            } else {
                System.err.println("WARNING: android/key.properties not found, now using debug key.")
                signingConfig signingConfigs.debug
            }
        }
    }
}

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


dependencies {
    // Add the desugaring library
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.1.4'
}


// def localProperties = new Properties()
// def localPropertiesFile = rootProject.file('local.properties')
// if (localPropertiesFile.exists()) {
//     localPropertiesFile.withReader('UTF-8') { reader ->
//         localProperties.load(reader)
//     }
// } else {
//     throw new GradleException("local.properties file not found")
// }

// 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"

// def keystorePropertiesFile = rootProject.file('key.properties')
// def keystoreProperties = new Properties()

// if (keystorePropertiesFile.exists()) {
//     keystorePropertiesFile.withReader('UTF-8') { reader ->
//         keystoreProperties.load(reader)
//     }
// } else {
//     keystoreProperties['storeFile'] = '.'
// }

// android {
//     defaultConfig {
//         multiDexEnabled true
//     }

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

//     compileSdkVersion rootProject.ext.compileSdkVersion

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


//     defaultConfig {
//         applicationId "de.enough.enough_mail_app"
//         minSdkVersion rootProject.ext.minSdkVersion
//         targetSdkVersion rootProject.ext.targetSdkVersion
//         versionCode flutterVersionCode.toInteger()
//         versionName flutterVersionName
//     }

//     signingConfigs {
//         release {
//             keyAlias keystoreProperties['keyAlias']
//             keyPassword keystoreProperties['keyPassword']
//             storeFile file(keystoreProperties['storeFile'])
//             storePassword keystoreProperties['storePassword']
//         }
//     }

//     buildTypes {
//         release {
//             if (keystorePropertiesFile.exists()) {
//                 signingConfig signingConfigs.release
//             } else {
//                 System.err.println("WARNING: android/key.properties not found, now using debug key.")
//                 signingConfig signingConfigs.debug  
//             }
//         }
//     }
//     namespace 'de.enough.enough_mail_app'
//     lint {
//         disable 'InvalidPackage'
//     }
// }



// flutter {
//     source '../..'
// }

// dependencies {
//     implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
//     androidTestImplementation 'androidx.test:runner:1.3.0'                   // or higher
//     androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'   // or higher
//     coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.2.2'
//     implementation 'androidx.window:window:1.0.0'
//     implementation 'androidx.window:window-java:1.0.0'
// }
