plugins {
    id 'com.gladed.androidgitversion' version '0.4.14'
}

apply plugin: "com.android.application"
apply plugin: "com.facebook.react"
apply plugin: "org.sonarqube"
apply plugin: 'kotlin-android'


configurations.all {
    resolutionStrategy.force 'androidx.activity:activity-ktx:1.7.1', 'androidx.activity:activity:1.7.0', 'com.fasterxml.jackson.core:jackson-core:2.14.0'
    all*.exclude module: 'bcprov-jdk15to18'
    all*.exclude module: 'bcutil-jdk18on'
    all*.exclude module: 'bcprov-jdk15on'
    all*.exclude module: 'bcutil-jdk15on'
    all*.exclude module: 'titanium-json-ld'
}

react {
    /* 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")
    entryFile = file("../../index.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 create four separate APKs instead of one,
 * one for each native architecture. This is useful if you don't
 * use App Bundles (https://developer.android.com/guide/app-bundle/)
 * and want to have separate APKs to upload to the Play Store.
 */
def enableSeparateBuildPerCPUArchitecture = true

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

/**
 * 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 = 'org.webkit:android-jsc:+'

/**
 * Private function to get the list of Native Architectures you want to build.
 * This reads the value from reactNativeArchitectures in your gradle.properties
 * file and works together with the --active-arch-only flag of react-native run-android.
 */
def reactNativeArchitectures() {
    def value = project.getProperties().get("reactNativeArchitectures")
    return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
}

androidGitVersion {
    baseCode 0
    codeFormat 'MXXNXXPXX'
    format '%tag_%%commit_%%branch%'
}

android {
    ndkVersion rootProject.ext.ndkVersion
    compileSdkVersion rootProject.ext.compileSdkVersion
    ext {
        APP_NAME = "@string/app_name"
    }

    namespace 'io.mosip.residentapp'

    defaultConfig {
        applicationId 'io.mosip.residentapp'
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionName "0.18.0"
        versionCode 1
        manifestPlaceholders = [
                APP_NAME             : APP_NAME,
                appAuthRedirectScheme: 'io.mosip.residentapp.inji'
        ]
    }

    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk true  // If true, also generate a universal APK
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }
    }

    signingConfigs {
        release {
            // for sonarqube job we will generate dummy release keystore to sign the app as we are not doing it in workflow
            def hasSonarqube = System.properties.containsKey("sonarqube")
            def keystore = file('release.keystore').exists() ? file('release.keystore') : file('dummyrelease.keystore')
            storeFile file("$keystore")
            if (hasSonarqube && !keystore.exists()) {
                exec {
                    commandLine 'keytool',
                            '-genkey',
                            '-v',
                            '-storetype', 'PKCS12',
                            '-keyalg', 'RSA',
                            '-keysize', '2048',
                            '-validity', '10000',
                            '-storepass', 'password',
                            '-keypass', 'password',
                            '-alias', 'androidreleasekey',
                            '-keystore', 'dummyrelease.keystore',
                            '-dname', 'CN=,OU=,O=,L=,S=,C=US'
                }
                storePassword "password"
                keyAlias "androidreleasekey"
                keyPassword "password"
            } else {
                def keystoreAlias = System.getenv("RELEASE_KEYSTORE_ALIAS")
                def keystorePass = System.getenv("RELEASE_KEYSTORE_PASSWORD")
                storePassword "$keystorePass"
                keyAlias "$keystoreAlias"
                keyPassword "$keystorePass"
            }
            v2SigningEnabled true
            v1SigningEnabled false
        }
        debug {
            def keystore = file('debug.keystore')
            if (keystore.exists()) {
                def keystoreAlias = System.getenv("DEBUG_KEYSTORE_ALIAS")
                def keystorePass = System.getenv("DEBUG_KEYSTORE_PASSWORD")
                storeFile file("$keystore")
                storePassword "$keystorePass"
                keyAlias "$keystoreAlias"
                keyPassword "$keystorePass"
                v2SigningEnabled true
                v1SigningEnabled false
            } else {
                android.buildTypes.debug.signingConfig = null
            }
        }
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            lintOptions {
                checkReleaseBuilds false
                abortOnError false
            }
            // Caution! In production, you need to generate your own keystore file.
            // see https://reactnative.dev/docs/signed-apk-android.
            signingConfig signingConfigs.release
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }

    flavorDimensions "inji"

    productFlavors {
        residentapp {
            versionName defaultConfig.versionName
            dimension "inji"
            resValue "string", "app_name", "Inji"
        }
        inji {
            applicationId "io.mosip.inji.wallet"
            versionName defaultConfig.versionName
            dimension "inji"
            resValue "string", "app_name", "Inji Wallet"
        }
        collab {
            applicationId "io.mosip.inji.collab"
            versionName defaultConfig.versionName
            dimension "inji"
            resValue "string", "app_name", "Inji Wallet Collab"
        }
        synergy {
            applicationId "io.mosip.inji.synergy"
            versionName defaultConfig.versionName
            dimension "inji"
            resValue "string", "app_name", "Inji Wallet Synergy"
        }
        mec {
            applicationId "io.mosip.inji.mec"
            versionName defaultConfig.versionName
            dimension "inji"
            resValue "string", "app_name", "Inji Wallet Mec"
        }
    }

    android.applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def datetime = new Date().format('yyyyMMdd_HHmm')
            def architecture = output.getFilter(com.android.build.OutputFile.ABI) ?: "universal"
            outputFileName = "Inji_${architecture}.apk"
        }
    }

    android.applicationVariants.all { variant ->
        variant.outputs.all { output ->
            if (variant.flavorName == "collab") {
                sonarqube {
                    androidVariant variant.name
                }
            }
        }
    }

    packagingOptions {
        pickFirst 'lib/arm64-v8a/liblog.so'
        pickFirst 'lib/armeabi-v7a/liblog.so'
        pickFirst 'lib/x86/liblog.so'
        pickFirst 'lib/x86_64/liblog.so'
        resources {
            excludes += "META-INF/*"
            excludes += "META-INF/spring/aot.factories"
        }
    }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3")
    implementation("io.mosip:inji-openid4vp:0.3.0-SNAPSHOT"){
        changing = true
        exclude group: 'org.bouncycastle', module: 'bcpkix-jdk15on'
        exclude group: 'org.bouncycastle', module: 'bcpkix-jdk18on'
        exclude group: 'io.mosip', module: 'vcverifier-jar'
        exclude group: 'com.google.crypto.tink', module: 'tink'
        exclude group: 'com.augustcellars.cose', module:'cose-java'
    }
    implementation("com.facebook.react:react-android")
    implementation 'com.facebook.soloader:soloader:0.10.1+'
    implementation("io.mosip:pixelpass-aar:0.7.0-SNAPSHOT")
    implementation("io.mosip:secure-keystore:0.3.0")
    implementation("io.mosip:tuvali:0.5.2")
    implementation("io.mosip:inji-vci-client:0.4.0-SNAPSHOT")
    implementation("com.google.code.gson:gson:2.10.1")
    implementation("io.mosip:vcverifier-aar:1.3.0-SNAPSHOT") {
        exclude group: 'org.bouncycastle', module: 'bcpkix-jdk15on'
    }


    def isGifEnabled = (findProperty('expo.gif.enabled') ?: "") == "true";
    def isWebpEnabled = (findProperty('expo.webp.enabled') ?: "") == "true";
    def isWebpAnimatedEnabled = (findProperty('expo.webp.animated') ?: "") == "true";

    // If your app supports Android versions before Ice Cream Sandwich (API level 14)
    // All fresco packages should use the same version
    if (isGifEnabled || isWebpEnabled) {
        implementation 'com.facebook.fresco:fresco:2.0.0'
        implementation 'com.facebook.fresco:imagepipeline-okhttp3:2.0.0'
        implementation 'com.squareup.okhttp3:okhttp-urlconnection:4.4.1'
    }

    if (isGifEnabled) {
        // For animated gif support
        implementation 'com.facebook.fresco:animated-gif:2.0.0'
    }

    if (isWebpEnabled) {
        // For webp support
        implementation 'com.facebook.fresco:webpsupport:2.0.0'
        if (isWebpAnimatedEnabled) {
            // Animated webp support
            implementation 'com.facebook.fresco:animated-webp:2.0.0'
        }
    }
    compileOnly project(':react-native-android-location-services-dialog-box')
    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"

    if (hermesEnabled.toBoolean()) {
        implementation("com.facebook.react:hermes-android")
    } else {
        implementation jscFlavor
    }
    implementation 'com.jakewharton.timber:timber:4.7.1'
}

task copyFaceSdkModel(type: Copy) {
    from '../../assets/models/'
    include 'faceModel.tflite'
    into 'src/main/assets/'
}

preBuild.dependsOn copyFaceSdkModel

task CaptureLibraryVersion {
    def libDef = project.configurations.getByName('implementation').allDependencies.matching {
        it.group.equals("io.mosip") && it.name.equals("tuvali")
    }
    if (libDef.size() > 0) {
        android.buildTypes.each {
            it.buildConfigField 'String', 'TUVALI_LIB_VERSION', "\"${libDef[0].version}\""
        }
    }
}

apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)

// https://github.com/oblador/react-native-vector-icons?tab=readme-ov-file#android-setup
apply from: file("../../node_modules/react-native-vector-icons/fonts.gradle")
apply from: "./eas-build.gradle"