plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.kotlin.android)
    alias(libs.plugins.compose)
    alias(libs.plugins.kotlin.parcelize)
    alias(libs.plugins.kotlin.serialization)
    alias(libs.plugins.hilt)
    alias(libs.plugins.protobuf)
    alias(libs.plugins.devtools.ksp)
    alias(libs.plugins.detekt)
}

def keystorePropertiesFile = rootProject.file("keystore.properties")
def keystoreProperties = new Properties()
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

android {
    namespace 'com.geeksville.mesh'

    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
            storePassword keystoreProperties['storePassword']
        }
    }
    compileSdk 35
    defaultConfig {
        applicationId "com.geeksville.mesh"
        minSdkVersion 21 // The oldest emulator image I have tried is 22 (though 21 probably works)
        targetSdk 34
        versionCode 30517 // format is Mmmss (where M is 1+the numeric major number
        versionName "2.5.17"
        testInstrumentationRunner "com.geeksville.mesh.TestRunner"

        // per https://developer.android.com/studio/write/vector-asset-studio
        vectorDrawables.useSupportLibrary = true
    }
    flavorDimensions = ['default']
    productFlavors {
        fdroid {
            dimension = 'default'
            dependenciesInfo {
                includeInApk = false
            }
        }
        google {
            dimension = 'default'
            if (useCrashlytics) {
                apply plugin: 'com.google.gms.google-services'
                apply plugin: 'com.google.firebase.crashlytics'
            }
        }
    }
    buildTypes {
        release {
            if (keystoreProperties['storeFile']) {
                signingConfig signingConfigs.release
            }
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            pseudoLocalesEnabled true
        }
    }
    defaultConfig {
        // We have to list all translated languages here, because some of our libs have bogus languages that google play
        // doesn't like and we need to strip them (gr)
        resConfigs "bg", "ca", "cs", "de", "el", "en", "es", "et", "fi", "fr", "fr-rHT", "ga", "gl", "hr", "hu", "is", "it", "iw", "ja", "ko", "lt", "nl", "nb", "pl", "pt", "pt-rBR", "ro", "ru", "sk", "sl", "sq", "sr", "sv", "tr", "zh-rCN", "zh-rTW", "uk"

        ndk {
            // abiFilters "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
        }
    }
    bundle {
        language {
            enableSplit false
        }
    }
    buildFeatures {
        viewBinding true
        compose true
        aidl true
        buildConfig true
    }
    // Configure the build-logic plugins to target JDK 17
    // This matches the JDK used to build the project, and is not related to what is running on device.
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_17.toString()
        freeCompilerArgs += ['-opt-in=kotlin.RequiresOptIn']
    }
    lint {
        abortOnError false
    }
    sourceSets {
        // Adds exported schema location as test app assets.
        androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
    }
}

// per protobuf-gradle-plugin docs, this is recommended for android
protobuf {
    protoc {
        artifact = libs.protobuf.protoc.get()
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java {
                    // turned off for now so I can use json printing in debug panel
                    // use the smaller android version of the library
                    //option "lite"
                }
                kotlin {
                }
            }
        }
    }
}

// workaround for https://github.com/google/ksp/issues/1590
androidComponents {
    onVariants(selector().all(), { variant ->
        afterEvaluate {
            def capName = variant.name.capitalize()
            tasks.getByName("ksp${capName}Kotlin") {
                setSource(tasks.getByName("generate${capName}Proto").outputs)
            }
        }
    })
}

dependencies {

    implementation(fileTree(dir: 'libs', include: ['*.jar']))

    // Bundles
    implementation(libs.bundles.androidx)
    implementation(libs.bundles.ui)
    debugImplementation(libs.bundles.ui.tooling)
    implementation(libs.bundles.lifecycle)
    implementation(libs.bundles.navigation)
    implementation(libs.bundles.coroutines)
    implementation(libs.bundles.datastore)
    implementation(libs.bundles.room)
    implementation(libs.bundles.hilt)
    implementation(libs.bundles.protobuf)

    //OSM
    implementation(libs.bundles.osm)
    implementation(libs.osmdroid.geopackage){ exclude group: "com.j256.ormlite" }

    //ZXing
    implementation(libs.zxing.android.embedded) { transitive = false }
    implementation(libs.zxing.core)  // do not update

    //Individual dependencies
    implementation(libs.appintro)
    implementation(libs.awesome.app.rating)
    implementation(libs.core.splashscreen)
    implementation(libs.emoji2.emojipicker)
    implementation(libs.kotlinx.collections.immutable)
    implementation(libs.kotlinx.serialization.json)
    implementation(libs.org.eclipse.paho.client.mqttv3)
    implementation(libs.streamsupport.minifuture)
    implementation(libs.usb.serial.android)
    implementation(libs.work.runtime.ktx)
    implementation(libs.core.location.altitude)

    //Compose BOM
    implementation(platform(libs.compose.bom))
    androidTestImplementation(platform(libs.compose.bom))

    //Firebase BOM
    googleImplementation(platform(libs.firebase.bom)) //For Firebase
    googleImplementation(libs.bundles.firebase)

    //ksp
    ksp(libs.room.compiler)
    ksp(libs.hilt.compiler)
    kspAndroidTest(libs.hilt.compiler)

    //Testing
    testImplementation(libs.bundles.testing)
    debugImplementation(libs.bundles.testing.android.manifest)
    androidTestImplementation(libs.bundles.testing.android)
    androidTestImplementation(libs.bundles.testing.hilt)
    androidTestImplementation(libs.bundles.testing.navigation)
    androidTestImplementation(libs.bundles.testing.room)

    detektPlugins(libs.detekt.formatting)
}

ksp {
//    arg("room.generateKotlin", "true")
    arg("room.schemaLocation", "$projectDir/schemas")
}

repositories {
    maven { url "https://jitpack.io" }
}

detekt {
    config.setFrom("../config/detekt/detekt.yml")
    baseline = file("../config/detekt/detekt-baseline.xml")
}
