//import groovy.json.JsonSlurper

// android/build.gradle

// based on:
//
// * https://github.com/facebook/react-native/blob/0.60-stable/template/android/build.gradle
//   previous location:
//   - https://github.com/facebook/react-native/blob/0.58-stable/local-cli/templates/HelloWorld/android/build.gradle
//
// * https://github.com/facebook/react-native/blob/0.60-stable/template/android/app/build.gradle
//   previous location:
//   - https://github.com/facebook/react-native/blob/0.58-stable/local-cli/templates/HelloWorld/android/app/build.gradle

// These defaults should reflect the SDK versions used by
// the minimum React Native version supported.
def DEFAULT_COMPILE_SDK_VERSION = 34
def DEFAULT_BUILD_TOOLS_VERSION = '28.0.3'
def DEFAULT_MIN_SDK_VERSION = 23
def DEFAULT_TARGET_SDK_VERSION = 34

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

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
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        // This should reflect the Gradle plugin version used by
        // the minimum React Native version supported.
        classpath 'com.android.tools.build:gradle:4.1.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
    }
}

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

android {
    namespace 'com.reactlibrary'
    compileSdkVersion safeExtGet('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION)
    buildToolsVersion safeExtGet('buildToolsVersion', DEFAULT_BUILD_TOOLS_VERSION)
    defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion', DEFAULT_MIN_SDK_VERSION)
        targetSdkVersion safeExtGet('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)
        versionCode 1
        versionName "1.0"
    }
    lintOptions {
        abortOnError false
    }
//    packagingOptions {
//        pickFirst 'lib/x86/libc++_shared.so'
//        pickFirst 'lib/x86_64/libc++_shared.so'
//        pickFirst 'lib/arm64-v8a/libc++_shared.so'
//        pickFirst 'lib/armeabi-v7a/libc++_shared.so'
//    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
}

repositories {
    // ref: https://www.baeldung.com/maven-local-repository
    mavenCentral()
    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"
    }
    google()
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    //noinspection GradleDynamicVersion
    implementation 'com.facebook.react:react-native:+'  // From node_modules
    implementation "org.jetbrains.kotlin:kotlin-stdlib:1.6.10"
    implementation 'com.google.code.gson:gson:2.9.0'
    implementation(name: 'voip-callkit', ext: 'aar')
}

//def configureReactNativePom(def pom) {
//    def packageJson = new JsonSlurper().parseText(file('../package.json').text)
//
//    pom.project {
//        name packageJson.title
//        artifactId packageJson.name
//        version = packageJson.version
//        group = "com.reactlibrary"
//        description packageJson.description
//        url packageJson.repository.baseUrl
//
//        licenses {
//            license {
//                name packageJson.license
//                url packageJson.repository.baseUrl + '/blob/master/' + packageJson.licenseFilename
//                distribution 'repo'
//            }
//        }
//
//        developers {
//            developer {
//                id packageJson.author.username
//                name packageJson.author.name
//            }
//        }
//    }
//}

//afterEvaluate { project ->
    // some Gradle build hooks ref:
    // https://www.oreilly.com/library/view/gradle-beyond-the/9781449373801/ch03.html
//    task androidJavadoc(type: Javadoc) {
//        source = android.sourceSets.main.java.srcDirs
//        classpath += files(android.bootClasspath)
//        classpath += files(project.getConfigurations().getByName('compile').asList())
//        include '**/*.java'
//    }
//
//    task androidJavadocJar(type: Jar, dependsOn: androidJavadoc) {
//        classifier = 'javadoc'
//        from androidJavadoc.destinationDir
//    }
//
//    task androidSourcesJar(type: Jar) {
//        classifier = 'sources'
//        from android.sourceSets.main.java.srcDirs
//        include '**/*.java'
//    }
//
//    android.libraryVariants.all { variant ->
//        def name = variant.name.capitalize()
//        def javaCompileTask = variant.javaCompileProvider.get()
//
//        task "jar${name}"(type: Jar, dependsOn: javaCompileTask) {
//            from javaCompileTask.destinationDir
//        }
//    }
//
//    artifacts {
//        archives androidSourcesJar
//        archives androidJavadocJar
//    }

//    task installArchives(type: Upload) {
//        configuration = configurations.archives
//        repositories.mavenDeployer {
//            // Deploy to react-native-event-bridge/maven, ready to publish to npm
//            repository url: "file://${projectDir}/../android/maven"
//            configureReactNativePom pom
//        }
//    }
//}
