rootProject.allprojects {
    repositories {
        google()
        mavenCentral()
        maven { url 'https://developer.huawei.com/repo/' }
    }
}

apply plugin: 'com.android.library'

// Android dependencies
def DEFAULT_COMPILE_SDK_VERSION                     = 31
def DEFAULT_TARGET_SDK_VERSION                      = 30
def DEFAULT_APP_COMPAT_VERSION                      = "1.6.1"

// Plugin dependencies
def DEFAULT_PLAY_SERVICES_LOCATION_VERSION          = "20.0.0"
def DEFAULT_HMS_LOCATION_VERSION                    = "6.9.0.300"
def DEFAULT_OK_HTTP_VERSION                         = "3.12.13"
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 useAndroidX = (project.hasProperty("android.useAndroidX")) ? project.property("android.useAndroidX") : false

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

android {
    if (project.android.hasProperty("namespace")) {
        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
    }
}

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

dependencies {
    // Prefer appCompatVersion vs supportLibVersion.
    def appCompatVersion = safeExtGet('appCompatVersion', DEFAULT_APP_COMPAT_VERSION)
    def playServicesLocationVersion = safeExtGet('playServicesLocationVersion', safeExtGet('googlePlayServicesLocationVersion', DEFAULT_PLAY_SERVICES_LOCATION_VERSION))
    def hmsLocationVersion = safeExtGet('hmsLocationVersion', DEFAULT_HMS_LOCATION_VERSION);
    def okHttpVersion = safeExtGet('okHttpVersion', DEFAULT_OK_HTTP_VERSION)
    def androidPermissionsVersion = safeExtGet('androidPermissionsVersion', DEFAULT_ANDROID_PERMISSIONS_VERSION)
    def eventBusVersion = safeExtGet('eventBusVersion', DEFAULT_EVENTBUS_VERSION)
    def localBroadcastManagerVersion = safeExtGet('localBroadcastManagerVersion', DEFAULT_LOCAL_BROADCAST_MANAGER_VERSION)
    def lifeCycleRuntimeVersion = safeExtGet('lifeCycleRuntimeVersion', DEFAULT_LIFE_CYCLE_RUNTIME_VERSION)
    def lifeCycleExtensionsVersion = safeExtGet('lifeCycleExtensionsVersion', DEFAULT_LIFE_CYCLE_EXTENSIONS_VERSION)
    def logbackVersion = safeExtGet('logbackVersion', DEFAULT_LOGBACK_VERSION);
    def slf4jVersion = safeExtGet('slf4jVersion', DEFAULT_SLF4J_VERSION);
    
    implementation "com.facebook.react:react-native:${safeExtGet('reactNativeVersion', '+')}"

    def locationMajorVersion = playServicesLocationVersion.split('\\.')[0] as int
    if (locationMajorVersion >= 21) {
        api(group: 'com.transistorsoft', name:'tslocationmanager-v21', version: '+')
    } else {
        api(group: 'com.transistorsoft', name:'tslocationmanager', version: '+')
    }
    
    implementation "androidx.appcompat:appcompat:$appCompatVersion"
    implementation "com.google.android.gms:play-services-location:$playServicesLocationVersion"
    // HMS location
    implementation("com.huawei.hms:location:$hmsLocationVersion") {
      exclude(group: "com.huawei.hms", module: 'hianalytics')
    }

    implementation "org.greenrobot:eventbus:$eventBusVersion"
    implementation "com.squareup.okhttp3:okhttp:$okHttpVersion"
    // logback-android
    implementation "com.github.tony19:logback-android:$logbackVersion"
    implementation "org.slf4j:slf4j-api:$slf4jVersion"

    implementation "io.github.nishkarsh:android-permissions:$androidPermissionsVersion"
    // LifeCycleObserver
    implementation "androidx.lifecycle:lifecycle-runtime:$lifeCycleRuntimeVersion"
    implementation "androidx.lifecycle:lifecycle-extensions:$lifeCycleExtensionsVersion"
    
    // WorkManager
    def workVersion = safeExtGet('workVersion', DEFAULT_WORK_VERSION)
    def concurrentFuturesVersion = safeExtGet('concurrentFuturesVersion', DEFAULT_CONCURRENT_FUTURES_VERSION)
    implementation "androidx.work:work-runtime:$workVersion"
    implementation "androidx.concurrent:concurrent-futures:$concurrentFuturesVersion"
}
