buildscript {
    ext.getExtOrDefault = {name, fallback ->
        return rootProject.ext.has(name) ? rootProject.ext.get(name) : fallback
    }

    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath("com.android.tools.build:gradle:7.3.1")
        // noinspection DifferentKotlinGradleVersion
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion', '1.8.10')}"
    }
}

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

rootProject.allprojects {
    repositories {
        maven {
            name = 'Central Portal Snapshots'
            url = 'https://central.sonatype.com/repository/maven-snapshots/'
            content {
                // This efficiently tells Gradle to only look for this specific dependency here
                includeModule('io.getstream', 'stream-webrtc-android')
            }
        }
    }
}

repositories {
    mavenCentral()
    google()
}

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

def supportsNamespace() {
    def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
    def major = parsed[0].toInteger()
    def minor = parsed[1].toInteger()

    // Namespace support was added in 7.3.0
    return (major == 7 && minor >= 3) || major >= 8
}

android {
    if (supportsNamespace()) {
        namespace "com.oney.WebRTCModule"

        sourceSets {
            main {
                manifest.srcFile "src/main/AndroidManifestNew.xml"
            }
        }
    }

    compileSdkVersion safeExtGet('compileSdkVersion', 24)
    buildToolsVersion safeExtGet('buildToolsVersion', "23.0.1")

    defaultConfig {
        minSdkVersion 24
        targetSdkVersion safeExtGet('targetSdkVersion', 24)
        versionCode 1
        versionName "1.0"
        consumerProguardFiles 'consumer-rules.pro'
    }

    // WebRTC requires Java 8 features
    // https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/discuss-webrtc/V1h2uQMDCkA/RA-uzncVAAAJ
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

def kotlin_version = getExtOrDefault('kotlinVersion', '1.8.10')

println "Building Stream WebRTC React Native module with Kotlin version: $kotlin_version"

dependencies {
    api 'io.getstream:stream-video-webrtc-android:137.0.1'
    implementation "com.facebook.react:react-android:+"
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation "androidx.core:core:1.7.0"
}
