// 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 = '28.0.3'
def DEFAULT_MIN_SDK_VERSION = 21
def DEFAULT_TARGET_SDK_VERSION = 28

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
    if (project == rootProject) {
        repositories {
            google()
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.5.3'
            classpath 'com.google.gms:google-services:4.3.10'
        }
    }
}

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'

android {
    compileSdkVersion 33
    buildToolsVersion "30.0.3"
    namespace "com.afrihost.firebase.notifications"
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 33
        versionCode 1
        versionName "1.0"
    }
    lintOptions {
        abortOnError false
    }
}

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

    def found = false
    def parentDir = rootProject.projectDir
    def androidSourcesName = 'React Native sources'
    def androidPrebuiltBinaryName = 'React Native prebuilt binary'

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

        // Running React Native from sources locally or for ExpoKit
        def androidSourcesDir = new File(
                parentDir,
                'node_modules/react-native'
        )

        // Official releases of React Native come with a prebuilt version of Android sources
        // in ./android, e.g. react-native/android/**/react-native-0.57.1.aar

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

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

            println "${project.name}: using ${androidPrebuiltBinaryName} from ${androidPrebuiltBinaryDir.toString()}"
            found = true
        } else if (androidSourcesDir.exists()) {
            maven {
                url androidSourcesDir.toString()
                name androidSourcesName
            }

            println "${project.name}: using ${androidSourcesName} from ${androidSourcesDir.toString()}"
            found = true
        }
    })

    if (!found) {
         throw new FileNotFoundException(
                 "${project.name}: unable to locate React Native android sources or prebuilt binary. " +
                         "Ensure you have you installed React Native as a dependency in your project and try again."
        )
    }
}

dependencies {
    implementation 'androidx.work:work-runtime-ktx:2.7.1'
    //noinspection GradleDynamicVersion
    implementation 'com.facebook.react:react-native:+'  // From node_modules
    // Add the Firebase SDK for Google Analytics
    implementation platform('com.google.firebase:firebase-bom:29.1.0')
    implementation 'com.google.firebase:firebase-messaging'
    implementation "me.leolin:ShortcutBadger:1.1.22@aar"

    // Declare the dependency for the Firebase SDK for Google Analytics
    implementation 'com.google.firebase:firebase-analytics'


    // Add the SDKs for any other Firebase products you want to use in your app
    // For example, to use Firebase Authentication and Cloud Firestore
//    implementation 'com.google.firebase:firebase-auth:19.2.0'
//    implementation 'com.google.firebase:firebase-firestore:21.3.1'

    // Getting a "Could not find" error? Make sure that you've added
    // Google's Maven repository to your root-level build.gradle file
    //compileOnly 'me.leolin:ShortcutBadger:1.1.22@aar'
}

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.afrihost.firebase.notifications"
        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)
        project.getConfigurations().getByName('implementation').setCanBeResolved(true)
        include '**/*.java'
    }

    task androidJavadocJar(type: Jar, dependsOn: androidJavadoc) {
        archiveClassifier = 'javadoc'
        from androidJavadoc.destinationDir
    }

    task androidSourcesJar(type: Jar) {
        archiveClassifier = 'javadoc'
        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
    }

    publishing {
        repositories {
            maven {
                url = uri("${rootProject.projectDir}/maven-repo")
            }
        }
    }
}
