def defaultCompileSDKVersion = 35
def defaultBuildToolsVersion = '35.0.0'
def defaultMinSDKVersion = 21
def defaultTargetSDKVersion = 35

def getRootProjectProperty(property, fallback) {
    rootProject.ext.has(property) ? rootProject.ext.get(property) : fallback
}

def overrideGmsVersion = getRootProjectProperty('overrideGmsVersion', '')
def overrideFirebaseVersion = getRootProjectProperty('overrideFirebaseVersion', '')
def overrideKotlinVersion = getRootProjectProperty('overrideKotlinVersion', '')
def withCryptorMigration = getRootProjectProperty('withCryptorMigration', false)
def withWebRTCUI = getRootProjectProperty('withWebRTCUI', false)

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'

buildscript {
    // The Android Gradle plugin is only required when opening the android folder stand-alone.
    // This avoids unnecessary downloads and potential conflicts when the library is included as a
    // module dependency in an application project.
    // ref: https://docs.gradle.org/current/userguide/tutorial_using_tasks.html#sec:build_script_external_dependencies
    if (project == rootProject) {
        repositories {
            google()
            mavenLocal()
            mavenCentral {
                // We don't want to fetch react-native from Maven Central as there are
                // older versions over there.
                content {
                    excludeGroup "com.facebook.react"
                }
            }
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:8.5.2'
        }
    }
}

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'

android {
    namespace "org.infobip.reactlibrary.mobilemessaging"
    compileSdkVersion getRootProjectProperty('compileSdkVersion', defaultCompileSDKVersion)
    buildToolsVersion getRootProjectProperty('buildToolsVersion', defaultBuildToolsVersion)
    defaultConfig {
        minSdkVersion getRootProjectProperty('minSdkVersion', defaultMinSDKVersion)
        targetSdkVersion getRootProjectProperty('targetSdkVersion', defaultTargetSDKVersion)
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            // Applying ProGuard rules for application classes that will be serialized/deserialized over Gson
            consumerProguardFiles 'infobip-mm-proguard-rules.pro'
        }
    }

    lintOptions {
        abortOnError false
    }
}

repositories {
    // ref: https://www.baeldung.com/maven-local-repository
    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"
    }
    mavenCentral {
        // We don't want to fetch react-native from Maven Central as there are
        // older versions over there.
        content {
            excludeGroup "com.facebook.react"
        }
    }
    google()
    mavenLocal()
}

dependencies {
    def mmVersion = '13.9.6'
    //react and mm dependencies clash
    if (!overrideKotlinVersion.empty) {
        constraints {
            implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$overrideKotlinVersion")
        }
    }
    implementation 'com.facebook.react:react-native:+'
    implementation "androidx.annotation:annotation:1.1.0"

    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.exifinterface:exifinterface:1.3.6'
    implementation 'androidx.databinding:viewbinding:8.0.2'
    implementation 'com.google.android.gms:play-services-base:18.2.0'

    implementation "com.infobip:infobip-mobile-messaging-android-resources:$mmVersion@aar"
    implementation "com.infobip:infobip-mobile-messaging-android-chat-sdk:$mmVersion@aar"
    implementation "com.infobip:infobip-mobile-messaging-android-inbox-sdk:$mmVersion@aar"
    implementation ("com.infobip:infobip-mobile-messaging-android-sdk:$mmVersion@aar") {
        transitive = true;

        if (!overrideGmsVersion.empty) {
            exclude group: 'com.google.android.gms', module: 'play-services-location'
        }
        if (!overrideFirebaseVersion.empty) {
            exclude group: 'com.google.firebase', module: 'firebase-messaging'
        }
    }
    if (withWebRTCUI.toBoolean()) {
        implementation implementation("com.infobip:infobip-rtc-ui:$mmVersion") {
            transitive = true
        }
    }
    if (!overrideGmsVersion.empty) {
        implementation "com.google.android.gms:play-services-location:$overrideGmsVersion"
    }
    if (!overrideFirebaseVersion.empty) {
        implementation "com.google.firebase:firebase-messaging:$overrideFirebaseVersion"
    }

    if (withCryptorMigration.toBoolean()) {
        implementation "com.infobip:infobip-mobile-messaging-android-cryptor-migration:$mmVersion@aar"
    }

}
