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

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    def taskRequests = getGradle().getStartParameter().getTaskRequests().toString().toLowerCase()
    def isPlay = !taskRequests.contains("foss")

    ext {
        buildToolsVersion = "29.0.3"
        minSdkVersion = 23
        compileSdkVersion = 30
        targetSdkVersion = 30
        ndkVersion = "20.1.5948944"
        glideVersion = "4.11.0"
        kotlin_version = "1.3.50"
        supportLibVersion = "28.0.0"
        libre_build = !(isPlay.toBoolean())
        jitsi_url = isPlay ? "https://github.com/RocketChat/jitsi-maven-repository/raw/master/releases" : "https://github.com/RocketChat/jitsi-maven-repository/raw/libre/releases"
        jitsi_version = isPlay ? "3.6.0" : "3.6.0-libre"
    }

    repositories {
        mavenLocal()
        google()
        jcenter()
        maven {
            url 'https://maven.fabric.io/public'
        }
    }

    dependencies {
        if (isPlay) {
            classpath 'com.google.gms:google-services:4.2.0'
            classpath 'com.google.firebase:firebase-crashlytics-gradle:2.0.0'
            classpath 'com.bugsnag:bugsnag-android-gradle-plugin:5.+'
        }
        classpath 'com.android.tools.build:gradle:4.1.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        mavenLocal()
        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")
        }

        maven {
            url "$rootDir/../node_modules/detox/Detox-android"
        }

        maven {
            url jitsi_url
        }

        google()
        jcenter()
        maven { url 'https://maven.google.com' }
        maven { url 'https://www.jitpack.io' }
    }
}

subprojects { subproject ->
    afterEvaluate {
        if ((subproject.plugins.hasPlugin('android') || subproject.plugins.hasPlugin('android-library'))) {
            android {
                compileSdkVersion 30
                buildToolsVersion "29.0.3"
                defaultConfig {
                    targetSdkVersion 30
                }
                variantFilter { variant ->
                    def names = variant.flavors*.name
                    if (names.contains("reactNative59")) {
                        setIgnore(true)
                    }
                }
            }
        }
    }
}
