apply plugin: "com.android.application"
apply plugin: "org.jetbrains.kotlin.android"
apply plugin: "com.facebook.react"

apply plugin: "com.google.protobuf"

// LN address notifications
apply plugin: "com.google.gms.google-services"

import com.android.build.OutputFile

/**
 * This is the configuration block to customize your React Native Android app.
 * By default you don't need to apply any configuration, just uncomment the lines you need.
 */
react {
    autolinkLibrariesWithApp()
    /* Folders */
    //   The root of your project, i.e. where "package.json" lives. Default is '..'
    // root = file("../")
    //   The folder where the react-native NPM package is. Default is ../node_modules/react-native
    // reactNativeDir = file("../node_modules/react-native")
    //   The folder where the react-native Codegen package is. Default is ../node_modules/@react-native/codegen
    // codegenDir = file("../node_modules/@react-native/codegen")
    //   The cli.js file which is the React Native CLI entrypoint. Default is ../node_modules/react-native/cli.js
    // cliFile = file("../node_modules/react-native/cli.js")

    /* Variants */
    //   The list of variants to that are debuggable. For those we're going to
    //   skip the bundling of the JS bundle and the assets. By default is just 'debug'.
    //   If you add flavors like lite, prod, etc. you'll have to list your debuggableVariants.
    // debuggableVariants = ["liteDebug", "prodDebug"]

    /* Bundling */
    //   A list containing the node command and its flags. Default is just 'node'.
    // nodeExecutableAndArgs = ["node"]
    //
    //   The command to run when bundling. By default is 'bundle'
    // bundleCommand = "ram-bundle"
    //
    //   The path to the CLI configuration file. Default is empty.
    // bundleConfig = file(../rn-cli.config.js)
    //
    //   The name of the generated asset file containing your JS bundle
    // bundleAssetName = "MyApplication.android.bundle"
    //
    //   The entry file for bundle generation. Default is 'index.android.js' or 'index.js'
    // entryFile = file("../js/MyApplication.android.js")
    //
    //   A list of extra flags to pass to the 'bundle' commands.
    //   See https://github.com/react-native-community/cli/blob/main/docs/commands.md#bundle
    // extraPackagerArgs = []

    /* Hermes Commands */
    //   The hermes compiler command to run. By default it is 'hermesc'
    // hermesCommand = "$rootDir/my-custom-hermesc/bin/hermesc"
    //
    //   The list of flags to pass to the Hermes compiler. By default is "-O", "-output-source-map"
    // hermesFlags = ["-O", "-output-source-map"]
}

/**
 * Set this to true to Run Proguard on Release builds to minify the Java bytecode.
 */
def enableProguardInReleaseBuilds = true

/**
 * The preferred build flavor of JavaScriptCore (JSC)
 *
 * For example, to use the international variant, you can use:
 * `def jscFlavor = 'org.webkit:android-jsc-intl:+'`
 *
 * The international variant includes ICU i18n library and necessary data
 * allowing to use e.g. `Date.toLocaleString` and `String.localeCompare` that
 * give correct results when using with locales other than en-US. Note that
 * this variant is about 6MiB larger per architecture than default.
 */
def jscFlavor = 'io.github.nickmartens:jsc-android:2026004.+'

/**
 * Set this to true to create two separate APKs instead of one:
 *   - An APK that only works on ARM devices
 *   - An APK that only works on x86 devices
 * The advantage is the size of the APK is reduced by about 4MB.
 * Upload all the APKs to the Play Store and people will download
 * the correct one based on the CPU architecture of their device.
 */
def enableSeparateBuildPerCPUArchitecture = true

/**
 * Architectures to build native code for in debug.
 */
def nativeArchitectures = project.getProperties().get("reactNativeDebugArchitectures")

android {
    namespace "app.zeusln.zeus"
    defaultConfig {
        compileSdk rootProject.ext.compileSdkVersion
        applicationId "app.zeusln.zeus"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 118
        versionName "0.13.0-prealpha"
        multiDexEnabled true
    }
    signingConfigs {
        release {
            if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) {
                storeFile file(MYAPP_RELEASE_STORE_FILE)
                storePassword MYAPP_RELEASE_STORE_PASSWORD
                keyAlias MYAPP_RELEASE_KEY_ALIAS
                keyPassword MYAPP_RELEASE_KEY_PASSWORD
            }
        }
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk true  // If true, also generate a universal APK
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
            if (nativeArchitectures) {
                ndk {
                    abiFilters nativeArchitectures.split(',')
                }
            }
        }
        release {
            shrinkResources true
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            //signingConfig signingConfigs.release
        }
    }
    // applicationVariants are e.g. debug, release
    applicationVariants.configureEach { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // https://developer.android.com/studio/build/configure-apk-splits.html
            // Example: versionCode 1 will generate 1001 for armeabi-v7a, 1002 for x86, etc.
            def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        defaultConfig.versionCode * 1000 + versionCodes.get(abi)
            }
        }
    }

    packagingOptions {
        pickFirst "lib/armeabi-v7a/libc++_shared.so"
        pickFirst "lib/arm64-v8a/libc++_shared.so"
        pickFirst "lib/x86/libc++_shared.so"
        pickFirst "lib/x86_64/libc++_shared.so"

        pickFirst "lib/armeabi-v7a/libgojni.so"
        pickFirst "lib/arm64-v8a/libgojni.so"
        pickFirst "lib/x86/libgojni.so"
        pickFirst "lib/x86_64/libgojni.so"

        // CashuDevKit native libraries (Rust FFI)
        pickFirst "lib/armeabi-v7a/libcdk_ffi.so"
        pickFirst "lib/arm64-v8a/libcdk_ffi.so"
        pickFirst "lib/x86/libcdk_ffi.so"
        pickFirst "lib/x86_64/libcdk_ffi.so"

        // Zeus Cashu Restore native libraries (Rust FFI)
        pickFirst "lib/armeabi-v7a/libuniffi_zeus_cashu_restore.so"
        pickFirst "lib/arm64-v8a/libuniffi_zeus_cashu_restore.so"
        pickFirst "lib/x86/libuniffi_zeus_cashu_restore.so"
        pickFirst "lib/x86_64/libuniffi_zeus_cashu_restore.so"

        jniLibs {
            useLegacyPackaging true
        }
    }

    // Reproducible build settings
    tasks.withType(Zip).configureEach {
        reproducibleFileOrder = true
        preserveFileTimestamps = false
    }

    sourceSets {
        main {
            proto {
                srcDir '../../proto'
            }
        }
    }
}

dependencies {
    // The version of react-native is set by the React Native Gradle Plugin
    implementation("com.facebook.react:react-android")

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

    // CashuDevKit - Cashu wallet FFI bindings
    implementation files("../cdk/cashudevkit.aar")
    // Zeus Cashu Restore - standalone NUT-13 restore for v1 legacy seeds
    implementation files("../zeus-restore/zeus-cashu-restore.aar")
    // CDK dependencies
    implementation "net.java.dev.jna:jna:5.14.0@aar"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3"

    implementation 'androidx.multidex:multidex:2.0.1'
    implementation project(':react-native-randombytes')
    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.2.0-alpha01'
    implementation files("../../node_modules/react-native-tor/android/libs/sifir_android.aar")
    // gif
    implementation 'com.facebook.fresco:fresco:3.1.3'
    implementation 'com.facebook.fresco:animated-gif:3.1.3'

    // Pegasus
    // implementation(name:"Lndmobile", ext:"aar")
    implementation project(":lndmobile")
    implementation "androidx.concurrent:concurrent-futures:1.1.0"
    implementation "androidx.work:work-runtime:2.8.1"
    implementation "com.google.guava:guava:31.0.1-android"
    implementation "io.grpc:grpc-protobuf-lite:1.45.1"

    implementation "com.jakewharton:process-phoenix:2.0.0"
    implementation 'org.brotli:dec:0.1.2'

    // LN address notifications
    // Import the Firebase BoM
    implementation platform('com.google.firebase:firebase-bom:32.3.1')

    // Tor + Persistent LND
    implementation "com.jakewharton:process-phoenix:2.0.0"

    // implementation 'info.guardianproject:tor-android:0.4.7.8'
    // implementation 'info.guardianproject:jtorctl:0.4.5.7'
}

configurations.configureEach {
    // https://stackoverflow.com/questions/54717283/program-type-already-present-com-google-common-util-concurrent-listenablefuture
    exclude group: "com.google.guava", module: "listenablefuture"
}

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
tasks.register('copyDownloadableDepsToLibs', Copy) {
    from configurations.implementation
    into 'libs'
}


protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.24.1"
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java {
                    option "lite"
                }
            }
        }
    }
}

