def DEFAULT_COMPILE_SDK_VERSION = 34
def DEFAULT_MIN_SDK_VERSION = 21
def DEFAULT_TARGET_SDK_VERSION = 34

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

buildscript {
    ext {
        kotlin_version = '1.9.22'
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.5.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

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

android {
    compileSdkVersion safeExtGet('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION)

    defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion', DEFAULT_MIN_SDK_VERSION)
        targetSdkVersion safeExtGet('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
        }
    }
    lintOptions {
        disable 'GradleCompatible'
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = '17'
    }
    namespace "com.appboxo.react"
}

repositories {
    mavenCentral()
    google()

    def found = false
    def defaultDir = null
    def androidSourcesName = 'React Native sources'

    if (rootProject.ext.has('reactNativeAndroidRoot')) {
        defaultDir = rootProject.ext.get('reactNativeAndroidRoot')
    } else {
        defaultDir = new File(
                projectDir,
                '/../../../node_modules/react-native/android'
        )
    }

    if (defaultDir.exists()) {
        maven {
            url defaultDir.toString()
            name androidSourcesName
        }

        logger.info(":${project.name}:reactNativeAndroidRoot ${defaultDir.canonicalPath}")
        found = true
    } else {
        def parentDir = rootProject.projectDir

        1.upto(5, {
            if (found) return true
            parentDir = parentDir.parentFile

            def androidSourcesDir = new File(
                    parentDir,
                    'node_modules/react-native'
            )

            def androidPrebuiltBinaryDir = new File(
                    parentDir,
                    'node_modules/react-native/android'
            )

            if (androidPrebuiltBinaryDir.exists()) {
                maven {
                    url androidPrebuiltBinaryDir.toString()
                    name androidSourcesName
                }

                logger.info(":${project.name}:reactNativeAndroidRoot ${androidPrebuiltBinaryDir.canonicalPath}")
                found = true
            } else if (androidSourcesDir.exists()) {
                maven {
                    url androidSourcesDir.toString()
                    name androidSourcesName
                }

                logger.info(":${project.name}:reactNativeAndroidRoot ${androidSourcesDir.canonicalPath}")
                found = true
            }
        })
    }
    if (!found) {
        throw new GradleException(
                "${project.name}: unable to locate React Native android sources. " +
                        "Ensure you have you installed React Native as a dependency in your project and try again."
        )
    }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    //noinspection GradleDynamicVersion
    implementation 'com.facebook.react:react-native:+'  // From node_modules
    implementation('io.boxo.sdk:boxo-android:1.45.1')
}