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

// Edge build plugins:
apply plugin: "com.google.gms.google-services"
apply plugin: "io.sentry.android.gradle"
apply plugin: "tech.formatter-kt.formatter"

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 {
    /* 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("../../index.ts")
    //
    //   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"]

    /* Autolinking */
    autolinkLibrariesWithApp()
}

/**
 * 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 = io.github.react-native-community:jsc-android-intl:2026004.+`
 *
 * 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.react-native-community:jsc-android:2026004.+'

// Edge addition for Sentry:
def sentrySlug = "SENTRY_ORGANIZATION_SLUG"
if (!sentrySlug.contains('SENTRY_ORGANIZATION')) {
  apply from: "../../node_modules/@sentry/react-native/sentry.gradle"
}

android {
    ndkVersion rootProject.ext.ndkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion
    compileSdk rootProject.ext.compileSdkVersion

    namespace "co.edgesecure.app"
    defaultConfig {
        applicationId "co.edgesecure.app"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 21000000
        versionName "99.99.99"

        // Edge addition:
        multiDexEnabled true
        ndk {
            abiFilters 'armeabi-v7a', 'arm64-v8a' // Exclude Intel
        }
    }

    signingConfigs {
        release {
        }
    }
    buildTypes {
        release {
            // 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"
        }
    }

    // Edge addition for Zcash:
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
        coreLibraryDesugaringEnabled true
    }

    // Edge hacks for zcash and piratechain conflicts:
    packagingOptions {
        resources {
            pickFirst 'compact_formats.proto'
            pickFirst 'darkside.proto'
            pickFirst 'service.proto'
        }
    }
}

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

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

    // Edge additions:
    implementation 'androidx.work:work-runtime:2.7.1'
    implementation 'com.google.guava:guava:29.0-android'

    // We need to keep this in sync with the version in react-native-firebase:
    implementation "com.google.firebase:firebase-messaging:23.0.0"

    // For animated GIF support
    // Should match the `fresco` verion in
    // node_modules/react-native/gradle/libs.versions.toml
    implementation "com.facebook.fresco:animated-gif:3.6.0"

    // For Zcash:
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.1.5'
}

// ---------------------------------------------------------------------
// Edge additions
// ---------------------------------------------------------------------

// Specify signing properties on the command line:
if (hasProperty('storeFile')) {
    println 'Generating a signed package'
    android.signingConfigs.release.storeFile     = file(storeFile)
    android.signingConfigs.release.storePassword = storePassword
    android.signingConfigs.release.keyAlias      = keyAlias
    android.signingConfigs.release.keyPassword   = keyPassword
} else {
    println 'Generating a debug package'
    android.signingConfigs.release.storeFile     = file("airbitz.keystore")
    android.signingConfigs.release.storePassword = "password"
    android.signingConfigs.release.keyAlias      = "airbitz"
    android.signingConfigs.release.keyPassword   = "password"
}

// Integrate react-native-vector-icons fonts
apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"

import io.sentry.android.gradle.extensions.InstrumentationFeature
import io.sentry.android.gradle.instrumentation.logcat.LogcatLevel

if (!sentrySlug.contains('SENTRY_ORGANIZATION')) {
  sentry {
      // Disables or enables debug log output, e.g. for for sentry-cli.
      // Default is disabled.
      debug = false

      // The slug of the Sentry organization to use for uploading proguard mappings/source contexts.
      org = "SENTRY_ORGANIZATION_SLUG"

      // The slug of the Sentry project to use for uploading proguard mappings/source contexts.
      projectName = "SENTRY_PROJECT_SLUG"

      // The authentication token to use for uploading proguard mappings/source contexts.
      // WARNING: Do not expose this token in your build.gradle files, but rather set an environment
      // variable and read it into this property.
      authToken = System.getenv("SENTRY_MAP_UPLOAD_AUTH_TOKEN")

      // The url of your Sentry instance. If you're using SAAS (not self hosting) you do not have to
      // set this. If you are self hosting you can set your URL here
      url = "SENTRY_MAP_UPLOAD_URL"

      // Disables or enables the handling of Proguard mapping for Sentry.
      // If enabled the plugin will generate a UUID and will take care of
      // uploading the mapping to Sentry. If disabled, all the logic
      // related to proguard mapping will be excluded.
      // Default is enabled.
      includeProguardMapping = true

      // Whether the plugin should attempt to auto-upload the mapping file to Sentry or not.
      // If disabled the plugin will run a dry-run and just generate a UUID.
      // The mapping file has to be uploaded manually via sentry-cli in this case.
      // Default is enabled.
      autoUploadProguardMapping = true

      // Experimental flag to turn on support for GuardSquare's tools integration (Dexguard and External Proguard).
      // If enabled, the plugin will try to consume and upload the mapping file produced by Dexguard and External Proguard.
      // Default is disabled.
      dexguardEnabled = false

      // Disables or enables the automatic configuration of Native Symbols
      // for Sentry. This executes sentry-cli automatically so
      // you don't need to do it manually.
      // Default is disabled.
      uploadNativeSymbols = true

      // Whether the plugin should attempt to auto-upload the native debug symbols to Sentry or not.
      // If disabled the plugin will run a dry-run.
      // Default is enabled.
      autoUploadNativeSymbols = true

      // Does or doesn't include the source code of native code for Sentry.
      // This executes sentry-cli with the --include-sources param. automatically so
      // you don't need to do it manually.
      // This option has an effect only when [uploadNativeSymbols] is enabled.
      // Default is disabled.
      includeNativeSources = true

      // Generates a JVM (Java, Kotlin, etc.) source bundle and uploads your source code to Sentry.
      // This enables source context, allowing you to see your source
      // code as part of your stack traces in Sentry.
      includeSourceContext = true

      // Configure additional directories to be included in the source bundle which is used for
      // source context. The directories should be specified relative to the Gradle module/project's
      // root. For example, if you have a custom source set alongside 'main', the parameter would be
      // 'src/custom/java'.
      additionalSourceDirsForSourceContext = []

      // Enable or disable the tracing instrumentation.
      // Does auto instrumentation for specified features through bytecode manipulation.
      // Default is enabled.
      tracingInstrumentation {
        enabled = true

        // Specifies a set of instrumentation features that are eligible for bytecode manipulation.
        // Defaults to all available values of InstrumentationFeature enum class.
        features = [InstrumentationFeature.DATABASE, InstrumentationFeature.FILE_IO, InstrumentationFeature.OKHTTP, InstrumentationFeature.COMPOSE]

        // Enable or disable logcat instrumentation through bytecode manipulation.
        // Default is enabled.
        logcat {
          enabled = false

          // Specifies a minimum log level for the logcat breadcrumb logging.
          // Defaults to LogcatLevel.WARNING.
          minLevel = LogcatLevel.WARNING
        }

        // The set of glob patterns to exclude from instrumentation. Classes matching any of these
        // patterns in the project's sources and dependencies JARs won't be instrumented by the Sentry
        // Gradle plugin.
        //
        // Don't include the file extension. Filtering is done on compiled classes and
        // the .class suffix isn't included in the pattern matching.
        //
        // Example usage:
        // ```
        // excludes = ['com/example/donotinstrument/**', '**/*Test']
        // ```
        //
        // Only supported when using Android Gradle plugin (AGP) version 7.4.0 and above.
        excludes = []
      }

      // Enable auto-installation of Sentry components (sentry-android SDK and okhttp, timber and fragment integrations).
      // Default is enabled.
      // Only available v3.1.0 and above.
      autoInstallation {
        enabled = false

        // Specifies a version of the sentry-android SDK and fragment, timber and okhttp integrations.
        //
        // This is also useful, when you have the sentry-android SDK already included into a transitive dependency/module and want to
        // align integration versions with it (if it's a direct dependency, the version will be inferred).
        //
        // NOTE: if you have a higher version of the sentry-android SDK or integrations on the classpath, this setting will have no effect
        // as Gradle will resolve it to the latest version.
        //
        // Defaults to the latest published Sentry version.
        sentryVersion = '7.9.0'
      }

      // Disables or enables dependencies metadata reporting for Sentry.
      // If enabled, the plugin will collect external dependencies and
      // upload them to Sentry as part of events. If disabled, all the logic
      // related to the dependencies metadata report will be excluded.
      //
      // Default is enabled.
      includeDependenciesReport = true

      // Whether the plugin should send telemetry data to Sentry.
      // If disabled the plugin won't send telemetry data.
      // This is auto disabled if running against a self hosted instance of Sentry.
      // Default is enabled.
      telemetry = true
  }
}
