def DEFAULT_COMPILE_SDK_VERSION = 34
def DEFAULT_MIN_SDK_VERSION = 21
def DEFAULT_TARGET_SDK_VERSION = 34

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

apply plugin: 'com.android.library'
apply plugin: "kotlin-android"

def getReactNativeVersion() {
    def packageJsonFile = file("$rootDir/../node_modules/react-native/package.json")
    if (packageJsonFile.exists()) {
        def packageJson = new groovy.json.JsonSlurper().parseText(packageJsonFile.text)
        return packageJson.version
    }
    return "0.82.0"
}

static def getMajor(String version) {
    if (version == "unknown") return 0
    def parts = version.tokenize('.')
    return parts.size() > 0 ? parts[0].toInteger() : 0
}

static def getMinor(String version) {
    if (version == "unknown") return 0
    def parts = version.tokenize('.')
    return parts.size() > 1 ? parts[1].toInteger() : 0
}

def reactNativeVersion = getReactNativeVersion()
def versionNumber = getMajor(reactNativeVersion) * 100 + getMinor(reactNativeVersion)

// Starting from React Native 0.76, the New Architecture is enabled by default and some
// project templates no longer write an explicit `newArchEnabled` line in gradle.properties.
// So we only trust an explicit `newArchEnabled` property when the consumer app defines one;
// otherwise we fall back to the React Native version's own default (new arch for >= 0.76,
// old arch for earlier versions, matching the historical opt-in default).
def isNewArchitectureEnabled = rootProject.hasProperty("newArchEnabled")
    ? rootProject.getProperty("newArchEnabled") == "true"
    : versionNumber >= 76
if (isNewArchitectureEnabled) {
    apply plugin: "com.facebook.react"
}

buildscript {
    def kotlinVersion = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["kotlinVersion"]
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.5.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
    }
}

static def supportsNamespace() {
    def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
    def major = parsed[0].toInteger()
    def minor = parsed[1].toInteger()
    return (major == 7 && minor >= 3) || major >= 8
}

android {
    if (supportsNamespace()) {
        namespace "com.dynamsoft.reactnativelib"
    }
    sourceSets {
        main {
            if (supportsNamespace()) {
                manifest.srcFile "src/main/AndroidManifest_noPackage.xml"
            }
            java.srcDirs += isNewArchitectureEnabled ? ["src/newarch"] : ["src/oldarch"]
        }
    }

    compileSdkVersion safeExtGet('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION)
    defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion', DEFAULT_MIN_SDK_VERSION)
        targetSdkVersion safeExtGet('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)
        versionCode 1
        versionName "1.0"

        if (versionNumber < 79) {
            externalNativeBuild {
                cmake {
                    arguments "-DANDROID_STL=c++_shared"
                }
            }
        }
        buildConfigField("boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled.toString())
    }
    lintOptions {
        abortOnError false
    }

    buildFeatures {
        buildConfig true
    }

    if (versionNumber < 79) {
        buildFeatures {
            prefab true
        }
        externalNativeBuild {
            cmake {
                path "src/main/cpp/CMakeLists.txt"
            }
        }
    } else {
        buildTypes {
            release {
                sourceSets {
                    main {
                        if(versionNumber >= 86) {
                            jniLibs.srcDirs = ["src/main/dysJniLibsFor86/release"]
                        } else if (versionNumber >= 82) {
                            jniLibs.srcDirs = ["src/main/dysJniLibsFor82/release"]
                        } else if (versionNumber >= 81) {
                            jniLibs.srcDirs = ["src/main/dysJniLibsFor81/release"]
                        } else {
                            jniLibs.srcDirs = ["src/main/dysJniLibs/release"]
                        }
                    }
                }
            }
            debug {
                sourceSets {
                    main {
                        if(versionNumber >= 86) {
                            jniLibs.srcDirs = ["src/main/dysJniLibsFor86/debug"]
                        } else if (versionNumber >= 82) {
                            jniLibs.srcDirs = ["src/main/dysJniLibsFor82/debug"]
                        } else if (versionNumber >= 81) {
                            jniLibs.srcDirs = ["src/main/dysJniLibsFor81/debug"]
                        } else {
                            jniLibs.srcDirs = ["src/main/dysJniLibs/debug"]
                        }
                    }
                }
            }
        }
    }

}

repositories {
    google()
    mavenCentral()
    maven {
        // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
        url "$rootDir/../node_modules/react-native/android"
    }
    maven {
        // Android JSC is installed from npm
        url "$rootDir/../node_modules/jsc-android/dist"
    }
}

rootProject.allprojects {
    repositories {
        maven {
            url "https://download2.dynamsoft.com/maven/aar"
        }
    }
}


dependencies {
    def kotlinVersion = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["kotlinVersion"]

    //noinspection GradleDynamicVersion
    implementation 'com.facebook.react:react-native:+'
    compileOnly "com.facebook.react:react-android:+"

    //noinspection GradleDependency

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3"

    implementation "com.dynamsoft:capturevisionbundle:3.6.2000"
}
