plugins {
    id "com.android.library"
    id "com.facebook.react"
}

// Android dependencies
def DEFAULT_COMPILE_SDK_VERSION                     = 35
def DEFAULT_TARGET_SDK_VERSION                      = 35

// Plugin dependencies
def DEFAULT_PLAY_SERVICES_LOCATION_VERSION          = "21.3.0"
def DEFAULT_TSLOCATIONMANAGER_VERSION               = "4.4.+"
def DEFAULT_OK_HTTP_VERSION                         = "4.12.0"
def DEFAULT_ANDROID_PERMISSIONS_VERSION             = "2.1.6"
def DEFAULT_EVENTBUS_VERSION                        = "3.3.1"
def DEFAULT_LOCAL_BROADCAST_MANAGER_VERSION         = "1.0.0"
def DEFAULT_LIFE_CYCLE_RUNTIME_VERSION              = "2.4.1"
def DEFAULT_LIFE_CYCLE_EXTENSIONS_VERSION           = "2.2.0"
def DEFAULT_LOGBACK_VERSION                         = "2.0.1"
def DEFAULT_SLF4J_VERSION                           = "1.7.36"
def DEFAULT_WORK_VERSION                            = "2.8.1"
def DEFAULT_CONCURRENT_FUTURES_VERSION              = "1.1.0"

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

// Guards against the missing-symbol crash that occurs when an app pins an `ext.tslocationmanagerVersion`
// older than the SDK this plugin's bridge adapter was built against. Returns `minVersion` (with a
// warning) when the pinned version's entire range is below it; otherwise returns the pin unchanged.
// Dynamic pins are handled by comparing the pin's ceiling to the minimum's floor, so "4.2.+" is
// overridden while "4.+"/"+" are respected. Drop-in identical across the four plugin SDKs.
def ensureTSLocationManagerVersion(String configured, String minVersion) {
    def parse = { String v, boolean asCeiling ->
        def parts = (v ?: "").trim().tokenize(".")
        def out = [0, 0, 0]
        boolean wild = false
        for (int i = 0; i < 3; i++) {
            if (wild) { out[i] = asCeiling ? Integer.MAX_VALUE : 0; continue }
            def p = (i < parts.size()) ? parts[i] : null
            if (p != null && p.isInteger()) {
                out[i] = p as int
            } else if (p != null) {          // "+" wildcard or qualifier
                wild = true
                out[i] = asCeiling ? Integer.MAX_VALUE : 0
            }                                // missing trailing component stays 0
        }
        return out
    }
    def c = parse(configured, true)          // ceiling of the app's pin
    def f = parse(minVersion, false)         // floor of the required minimum
    def cmp = (c[0] <=> f[0]) ?: (c[1] <=> f[1]) ?: (c[2] <=> f[2])
    if (cmp < 0) {
        println("[tslocationmanager] ⚠️  ext.tslocationmanagerVersion '${configured}' is older than the '${minVersion}' required by this plugin release and has been overridden to '${minVersion}'. The native bridge adapter references symbols added in '${minVersion}'; an older SDK causes missing-symbol build failures / runtime crashes. Update or remove ext.tslocationmanagerVersion in your root build.gradle to silence this warning.")
        return minVersion
    }
    return configured
}

react {
    libraryName = "react-native-background-geolocation"
    codegenJavaPackageName = "com.transistorsoft.rnbackgroundgeolocation"
}
android {
    namespace("com.transistorsoft.rnbackgroundgeolocation")

    compileSdkVersion safeExtGet('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION)

    defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion', 16)
        targetSdkVersion safeExtGet('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)
        consumerProguardFiles 'proguard-rules.pro'
        versionCode 1
        versionName "1.0"
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    sourceSets {
        main {
            // Where Codegen put NativeBackgroundGeolocationSpec.java
            java.srcDir("$buildDir/generated/source/codegen/java")
        }
    }
    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }
}

repositories{
    maven {
        url './libs'
    }
}

dependencies {
    def playServicesLocationVersion = safeExtGet('playServicesLocationVersion', safeExtGet('googlePlayServicesLocationVersion', DEFAULT_PLAY_SERVICES_LOCATION_VERSION))
    def tslocationmanagerVersion = ensureTSLocationManagerVersion(safeExtGet('tslocationmanagerVersion', DEFAULT_TSLOCATIONMANAGER_VERSION), DEFAULT_TSLOCATIONMANAGER_VERSION)
    implementation "com.facebook.react:react-native:${safeExtGet('reactNativeVersion', '+')}"

    def locationMajorVersion = playServicesLocationVersion.split('\\.')[0] as int
    if (locationMajorVersion >= 21) {
        api "com.transistorsoft:tslocationmanager:$tslocationmanagerVersion"
    } else {
        api "com.transistorsoft:tslocationmanager-gms20:$tslocationmanagerVersion"
    }
        
    implementation "com.google.android.gms:play-services-location:$playServicesLocationVersion"
    implementation 'org.greenrobot:eventbus:3.3.1'

    // Bridge-translation unit tests (JVM/Robolectric) — not shipped to consumers.
    testImplementation "junit:junit:4.13.2"
    testImplementation "org.robolectric:robolectric:4.14.1"
}
