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

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

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

android {

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = '17'
    }

    compileSdkVersion 36

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

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.github.emavgl.piggybank"
        minSdkVersion flutter.minSdkVersion
        targetSdkVersion 35
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    signingConfigs {
        release {
            def keystoreProperties = new Properties()
            def keystorePropertiesFile = rootProject.file('key.properties')
            if (keystorePropertiesFile.exists()) {
                keystorePropertiesFile.withReader('UTF-8') { reader ->
                    keystoreProperties.load(reader)
                }
            }

            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : file(System.env.X_KEYSTORE_PATH)
            storePassword keystoreProperties['storePassword']
        }
    }

    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }

    flavorDimensions "oinkoin"
    productFlavors {
        dev {
            dimension "oinkoin"
            applicationIdSuffix ".dev.pro"
            resValue "string", "app_name", "Oinkoin Debug"
        }
        free {
            dimension "oinkoin"
            applicationIdSuffix ""
            resValue "string", "app_name", "Oinkoin"
        }
        pro {
            dimension "oinkoin"
            applicationId "com.github.emavgl.piggybankpro"
            resValue "string", "app_name", "Oinkoin Pro"
        }
        fdroid {
            dimension "oinkoin"
            applicationId "com.github.emavgl.piggybankpro"
            resValue "string", "app_name", "Oinkoin"
        }
    }

    namespace 'com.example.piggybank' // MainActivity package namespace
}

flutter {
    source '../..'
}

dependencies {
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test:runner:1.5.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

ext.abiCodes = ['armeabi-v7a': 1, 'arm64-v8a': 2, x86_64: 4]

// F-droid requires variant.versionCode * 1000 + baseAbiVersionCode
// instead of the flutter default: baseAbiVersionCode * 1000 + variant.versionCode
// read more at: https://github.com/emavgl/oinkoin/issues/120
android.applicationVariants.all { variant ->
    variant.outputs.each { output ->
        def baseAbiVersionCode = project.ext.abiCodes.get(output.getFilter("ABI"))
        if (baseAbiVersionCode != null) {
            output.versionCodeOverride = variant.versionCode * 1000 + baseAbiVersionCode
        }
    }
}


