import java.nio.file.Paths
import com.android.Version
import groovy.json.JsonSlurper


buildscript {
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:8.3.0'
    }
}

def reactNativeArchitectures() {
    def value = rootProject.getProperties().get("reactNativeArchitectures")
    return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
}

def isNewArchitectureEnabled() {
    return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
}

def safeExtGet(prop, fallback) {
    rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

def getNpmVersion() {
    def inputFile = file("$rootDir/../package.json")
    def jsonPackage = new JsonSlurper().parseText(inputFile.text)

    return jsonPackage["version"]
}

static def findNodeModules(baseDir) {
    def basePath = baseDir.toPath().normalize()
    // Node"s module resolution algorithm searches up to the root directory,
    // after which the base path will be null
    while (basePath) {
        def nodeModulesPath = Paths.get(basePath.toString(), "node_modules")
        def reactNativePath = Paths.get(nodeModulesPath.toString(), "react-native")
        if (nodeModulesPath.toFile().exists() && reactNativePath.toFile().exists()) {
            return nodeModulesPath.toString()
        }
        basePath = basePath.getParent()
    }
    throw new GradleException("@xbenjii/react-native-braintree-dropin-ui: Failed to find node_modules/ path!")
}

// https://developers.braintreepayments.com/guides/3d-secure/migration/android/v3
rootProject.allprojects {
    repositories {
        maven {
            url "https://cardinalcommerceprod.jfrog.io/artifactory/android"
            credentials {
                username 'braintree_team_sdk'
                password 'AKCp8jQcoDy2hxSWhDAUQKXLDPDx6NYRkqrgFLRc3qDrayg6rrCbJpsKKyMwaykVL8FWusJpp'
            }
        }
    }
}

apply plugin: "com.android.library"
if (isNewArchitectureEnabled()) {
    apply plugin: "com.facebook.react"
}

def nodeModules = findNodeModules(projectDir)
logger.warn("[@xbenjii/react-native-braintree-dropin-ui] node_modules found at $nodeModules")

android {
    if (rootProject.hasProperty("ndkPath")) {
        ndkPath rootProject.ext.ndkPath
    }
    if (rootProject.hasProperty("ndkVersion")) {
        ndkVersion rootProject.ext.ndkVersion
    }

    namespace "com.xbenjii.RNBraintreeDropIn"

    defaultConfig {
        minSdkVersion safeExtGet("minSdkVersion", 21)
        compileSdkVersion safeExtGet("compileSdkVersion", 34)
        targetSdkVersion safeExtGet("targetSdkVersion", 34)
        versionCode 1
        versionName getNpmVersion()
        buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    buildFeatures {
        buildConfig true
    }
}

repositories {
    mavenCentral()
    google()
}

dependencies {
    implementation 'com.braintreepayments.api:drop-in:6.16.0'
    implementation 'com.braintreepayments.api:google-pay:4.39.0'
    implementation "com.facebook.react:react-android:+"
    implementation 'org.codehaus.groovy:groovy-json:3.0.22'
}