def safeAppExtGet(prop, fallback) {
    def appProject = rootProject.allprojects.find { it.plugins.hasPlugin('com.android.application') }
    appProject?.ext?.has(prop) ? appProject.ext.get(prop) : fallback
}

def resolveReactNativeDirectory() {
    def reactNativeLocation = safeAppExtGet("REACT_NATIVE_NODE_MODULES_DIR", null)
    if (reactNativeLocation != null) {
        return file(reactNativeLocation)
    }

    // monorepo workaround
    // react-native can be hoisted or in project's own node_modules
    def reactNativeFromProjectNodeModules = file("${rootProject.projectDir}/../node_modules/react-native")
    if (reactNativeFromProjectNodeModules.exists()) {
        return reactNativeFromProjectNodeModules
    }

    def reactNativeFromNodeModulesWithReanimated = file("${projectDir}/../../react-native")
    if (reactNativeFromNodeModulesWithReanimated.exists()) {
        return reactNativeFromNodeModulesWithReanimated
    }

    throw new GradleException(
            "Unable to resolve react-native location in node_modules. You should project extension property (in `app/build.gradle`) `REACT_NATIVE_NODE_MODULES_DIR` with path to react-native."
    )
}

def reactNativeRootDir = resolveReactNativeDirectory()

def reactProperties = new Properties()
file("$reactNativeRootDir/ReactAndroid/gradle.properties").withInputStream { reactProperties.load(it) }

def REACT_NATIVE_VERSION = reactProperties.getProperty("VERSION_NAME")
def REACT_NATIVE_MINOR_VERSION = REACT_NATIVE_VERSION.startsWith("0.0.0-") ? 1000 : REACT_NATIVE_VERSION.split("\\.")[1].toInteger()

buildscript {
    ext.safeExtGet = { prop, fallback ->
        rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
    }
    ext.IS_ROOT = project == rootProject

    // 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.

    if (IS_ROOT) {
        repositories {
            google()
            mavenCentral()
        }
        dependencies {
            classpath "com.android.tools.build:gradle:7.3.1"
        }
    }
}

apply plugin: 'com.android.library'

android {
    namespace "com.github.douglasjunior.reactNativeAppLifecycle"
    compileSdkVersion safeExtGet("compileSdkVersion", 33)
    defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion', 21)
        //noinspection OldTargetApi
        targetSdkVersion safeExtGet('targetSdkVersion', 33)
    }
    lintOptions {
        abortOnError false
    }
}

repositories {
    mavenCentral()
    mavenLocal()
    if (REACT_NATIVE_MINOR_VERSION < 71) {
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$reactNativeRootDir/android"
        }
        maven {
            // Android JSC is installed from npm
            url "$reactNativeRootDir/../jsc-android/dist"
        }
    }
    google()
}

dependencies {
    if (REACT_NATIVE_MINOR_VERSION >= 71) {
        implementation "com.facebook.react:react-android${IS_ROOT ? ':+' : ''}"
        // version substituted by RNGP
    } else {
        // noinspection GradleDynamicVersion
        implementation "com.facebook.react:react-native:+" // From node_modules
    }

    implementation "androidx.lifecycle:lifecycle-process:${safeExtGet('lifecycleVersion', '2.6.2')}"
}
