// android/build.gradle

// based on:
//
// * https://github.com/facebook/react-native/blob/0.60-stable/template/android/build.gradle
//   original 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
//   original location:
//   - https://github.com/facebook/react-native/blob/0.58-stable/local-cli/templates/HelloWorld/android/app/build.gradle

def DEFAULT_COMPILE_SDK_VERSION = 31
def DEFAULT_BUILD_TOOLS_VERSION = '29.0.3'
def DEFAULT_MIN_SDK_VERSION = 23
def DEFAULT_TARGET_SDK_VERSION = 31

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

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
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
    ext.kotlin_version = '1.5.0'
    if (project == rootProject) {
        repositories {
            google()
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.4.1'
        }
    }
}

apply plugin: 'com.android.library'

android {
    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
    }
    buildFeatures {
        viewBinding true
    }
}

repositories {
    // ref: https://www.baeldung.com/maven-local-repository
    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()
    jcenter()
}

dependencies {
    implementation 'com.facebook.react:react-native:+'  // From node_modules
    implementation(name: 'trueIDSDK', ext: 'aar')
    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"

    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0'
    //WindowManager
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'com.squareup.retrofit2:retrofit:2.6.2'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'

    def camerax_version = '1.1.0-beta02'
    implementation "androidx.camera:camera-core:$camerax_version"
    // CameraX Camera2 extensions
    implementation "androidx.camera:camera-camera2:$camerax_version"
    // CameraX Lifecycle library
    implementation "androidx.camera:camera-lifecycle:$camerax_version"
    // CameraX View class
    implementation "androidx.camera:camera-view:$camerax_version"
    // end trueID implementations
    api('androidx.exifinterface:exifinterface:1.3.2')
    implementation "com.squareup.okhttp3:logging-interceptor:3.6.0"
    implementation 'com.squareup.okhttp3:okhttp:3.6.0'
    implementation 'com.google.protobuf:protobuf-lite:3.0.1'
    implementation 'com.github.devsideal:VectorChildFinder:1.0.0'
    implementation 'androidx.databinding:viewbinding:7.1.2'
    // Face features
    implementation 'com.google.mlkit:face-detection:16.1.5'
    implementation 'com.google.guava:guava:27.1-android'
    implementation(name: 'facetec-sdk-9.6.66', ext: 'aar')

    //NFC
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'com.google.mlkit:text-recognition:16.0.0'
    implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.27'
    implementation 'org.jmrtd:jmrtd:0.7.18'
    implementation 'com.github.mhshams:jnbis:1.1.0'
    implementation 'com.github.amcs99:card-reader-lite:1.0.1'
    implementation 'org.bouncycastle:bcpkix-jdk15on:1.65'

    // end trueID implementations
    implementation 'com.google.code.gson:gson:2.8.7'
    implementation 'com.google.zxing:core:3.3.3'
}

def configureReactNativePom(def pom) {
    def packageJson = new groovy.json.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
            }
        }
    }
}

configurations {
    implementation {
        canBeResolved = true
    }
}
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('implementation').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.maven {
            // Deploy to react-native-event-bridge/maven, ready to publish to npm
            url = uri("${rootProject.projectDir}/maven-repo")

        }
    }
}
