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

def secretProperties = new Properties()
def secretPropertiesFile = rootProject.file('../_tools/secrets/gradle_home/gradle.properties')
if (secretPropertiesFile.exists()) {
    System.out.println("loading secrets from ${secretPropertiesFile.absolutePath}")
    secretPropertiesFile.withReader('UTF-8') { reader ->
        secretProperties.load(reader)
    }
} else {
    System.err.println("secrets not found at ${secretPropertiesFile.absolutePath}")
}

kotlin {
    jvmToolchain(17)
}

android {
    namespace 'design.codeux.authpass'
    compileSdk 36
//    buildToolsVersion "29.0.2"
    ndkVersion System.getProperty("design.codeux.ndkVersion", "28.2.13676358")


    defaultConfig {
        applicationId "design.codeux.authpass"
        minSdk 24
        targetSdk 36
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        ndk {
            // Filter for architectures supported by Flutter.
            // https://github.com/flutter/flutter/issues/83596#issuecomment-1082781598
            // https://docs.flutter.dev/development/add-to-app/android/project-setup
            abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86_64'
        }
    }

    signingConfigs {
        release {
            keyAlias 'authpass'
            keyPassword secretProperties['design.codeux.authpass.storePassword']
            storeFile file(secretProperties['design.codeux.authpass.storeFile'] ?: "invalid")
            storePassword secretProperties['design.codeux.authpass.storePassword']
        }
        amazon {
            keyAlias 'amazon-key'
            keyPassword secretProperties['design.codeux.authpass.amazon.storePassword']
            storeFile file(secretProperties['design.codeux.authpass.amazon.storeFile'] ?: "invalid")
            storePassword secretProperties['design.codeux.authpass.amazon.storePassword']
        }
    }

    flavorDimensions = ["hpdist"]

    productFlavors {
        samsungapps {
            // Assigns this product flavor to the "version" flavor dimension.
            // If you are using only one dimension, this property is optional,
            // and the plugin automatically assigns all the module's flavors to
            // that dimension.
            dimension "hpdist"
            applicationIdSuffix ".samsungapps"
//            minSdkVersion 23
//            targetSdkVersion 29
            signingConfig signingConfigs.release
        }
        huawei {
            dimension "hpdist"
            applicationIdSuffix ".huawei"
            signingConfig signingConfigs.release
        }
        amazon {
            dimension "hpdist"
            applicationIdSuffix ".amazon"
            signingConfig signingConfigs.amazon
        }
        playstore {
            dimension "hpdist"
            signingConfig signingConfigs.release
        }
        playstoredev {
            dimension "hpdist"
            applicationIdSuffix ".dev"
            signingConfig signingConfigs.release
        }
        fdroid {
            dimension "hpdist"
            project.flutter.target = 'lib/env/fdroid.dart'
            applicationIdSuffix = '.fdroid'
            // https://issuetracker.google.com/issues/197430188
            signingConfig?.setConfig(null)
        }
        // Binary distribution for direct release through github.
        sideload {
            dimension "hpdist"
            applicationIdSuffix = '.sideload'
            signingConfig signingConfigs.release
            
        }

    }


    buildTypes {
        release {
//            minification is enabled by flutter build command, so nothing we need to do here.
//            NOTE: only `flutter build apk/appbundle` will enable it,
//            BUT NOT `flutter run --release`!!
            minifyEnabled true
            shrinkResources false
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),'proguard-rules.pro'
        }
        debug {
            project.flutter.target = 'lib/env/development.dart'
            applicationIdSuffix = '.debug'
        }
    }
    lint {
        checkReleaseBuilds false
        disable 'InvalidPackage'
    }
}

android.applicationVariants.all { variant ->
    if (variant.flavorName == "fdroid" && variant.buildType.name == "release") {
        variant.outputs.all { output ->
            output.outputFileName = "app-fdroid-release.apk"
        }
    }
}


flutter {
    source '../..'
}

dependencies {
    testImplementation 'junit:junit:4.13.2'

    implementation "androidx.appcompat:appcompat:1.6.1"
    implementation "androidx.fragment:fragment-ktx:1.6.2"

    // logging
    implementation "org.slf4j:slf4j-api:2.0.7"
    implementation "io.github.microutils:kotlin-logging:1.7.6"
    implementation "uk.uuid.slf4j:slf4j-android:2.0.7-0"

    androidTestImplementation 'androidx.test:runner:1.5.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
