def localProps = new Properties()
def localPropertiesFile = file("local.properties")
if (localPropertiesFile.exists()) {
    localProps.load(new InputStreamReader(new FileInputStream(localPropertiesFile), "UTF-8"))
}
def debugNativeLibraries = localProps.getProperty('NATIVE_DEBUG_ON', 'FALSE').toBoolean()

import java.nio.file.Paths

import org.apache.tools.ant.taskdefs.condition.Os
import org.apache.tools.ant.filters.ReplaceTokens

def reactProperties = new Properties()
file("$projectDir/../node_modules/react-native/ReactAndroid/gradle.properties").withInputStream { reactProperties.load(it) }

def BOOST_VERSION = reactProperties.getProperty("BOOST_VERSION")
def DOUBLE_CONVERSION_VERSION = reactProperties.getProperty("DOUBLE_CONVERSION_VERSION")
def FOLLY_VERSION = reactProperties.getProperty("FOLLY_VERSION")
def GLOG_VERSION = reactProperties.getProperty("GLOG_VERSION")
def REACT_VERSION = reactProperties.getProperty("VERSION_NAME").split("\\.")[1].toInteger()
def FBJNI_VERSION = '0.2.2'

// We download various C++ open-source dependencies into downloads.
// We then copy both the downloaded code and our custom makefiles and headers into third-party-ndk.
// After that we build native code from src/main/jni with module path pointing at third-party-ndk.

def downloadsDir = new File("$buildDir/downloads")
def thirdPartyNdkDir = new File("$buildDir/third-party-ndk")

def reactNative = new File("$projectDir/../node_modules/react-native")
def reactNativeThirdParty = new File("$reactNative/ReactAndroid/src/main/jni/third-party")

def HOME = System.getProperty("user.home")

def FOR_HERMES = "";
if(findProject(':app')) {
    FOR_HERMES = project(':app').ext.react.enableHermes;
}
else {
    FOR_HERMES = System.getenv("FOR_HERMES") == "True";
}
// You need to have following folders in this directory:
//   - boost_1_63_0
//   - double-conversion-1.1.6
//   - folly-deprecate-dynamic-initializer
//   - glog-0.3.5
def dependenciesPath = System.getenv("REACT_NATIVE_DEPENDENCIES")

// The Boost library is a very large download (>100MB).
// If Boost is already present on your system, define the REACT_NATIVE_BOOST_PATH env variable
// and the build will use that.
def boostPath = dependenciesPath ?: System.getenv("REACT_NATIVE_BOOST_PATH")

def follyReplaceContent = '''
  ssize_t r;
  do {
    r = open(name, flags, mode);
  } while (r == -1 && errno == EINTR);
  return r;
'''

buildscript {
    repositories {
        google()
        mavenCentral()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath('com.android.tools.build:gradle:4.2.2')
        classpath("de.undercouch:gradle-download-task:4.1.2")
        classpath "com.diffplug.spotless:spotless-plugin-gradle:5.15.0"
    }
}


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

if (project == rootProject) {
    apply from: 'spotless.gradle'
}

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
apply plugin: 'de.undercouch.download'


android {
    buildFeatures {
        prefab true
    }
    compileSdkVersion safeExtGet('compileSdkVersion', 30)
    defaultConfig {

        minSdkVersion safeExtGet('minSdkVersion', 16)
        targetSdkVersion safeExtGet('targetSdkVersion', 30)
        versionCode 1
        versionName "1.0"
        externalNativeBuild {
            cmake {
                arguments "-DANDROID_STL=c++_shared",
                             "-DNATIVE_DEBUG=${debugNativeLibraries}",
                             "-DREACT_NATIVE_TARGET_VERSION=${REACT_VERSION}",
                             "-DANDROID_TOOLCHAIN=clang",
                             "-DBOOST_VERSION=${BOOST_VERSION}",
                             "-DFOR_HERMES=${FOR_HERMES}"
                abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'
            }
        }

        buildConfigField("boolean", "IS_INTERNAL_BUILD", "false")
        buildConfigField("int", "EXOPACKAGE_FLAGS", "0")
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }

    lintOptions {
        abortOnError false
    }

    packagingOptions {
        println "Native libs debug enabled: ${debugNativeLibraries}"
        doNotStrip debugNativeLibraries ? "**/**/*.so" : ''
        excludes = ["**/libc++_shared.so", "**/libfbjni.so", "**/libjsi.so"]
    }

    tasks.withType(JavaCompile) {
        compileTask ->
            compileTask.dependsOn(packageNdkLibs)
    }

    configurations {
        extractHeaders
        extractSO
    }
}

task Log {
    print("building Reanimated2")
}

task createNativeDepsDirectories {
    downloadsDir.mkdirs()
    thirdPartyNdkDir.mkdirs()
}

task downloadBoost(dependsOn: createNativeDepsDirectories, type: Download) {
    src("https://github.com/react-native-community/boost-for-react-native/releases/download/v${BOOST_VERSION.replace("_", ".")}-0/boost_${BOOST_VERSION}.tar.gz")
    onlyIfNewer(true)
    overwrite(false)
    dest(new File(downloadsDir, "boost_${BOOST_VERSION}.tar.gz"))
}

task prepareBoost(dependsOn: boostPath ? [] : [downloadBoost], type: Copy) {
    from(boostPath ?: tarTree(resources.gzip(downloadBoost.dest)))
    from("$reactNativeThirdParty/boost/Android.mk")
    include("Android.mk", "boost_${BOOST_VERSION}/boost/**/*.hpp", "boost/boost/**/*.hpp")
    includeEmptyDirs = false
    into("$thirdPartyNdkDir/boost")
    doLast {
        file("$thirdPartyNdkDir/boost/boost").renameTo("$thirdPartyNdkDir/boost/boost_${BOOST_VERSION}")
    }
}

task downloadDoubleConversion(dependsOn: createNativeDepsDirectories, type: Download) {
    src("https://github.com/google/double-conversion/archive/v${DOUBLE_CONVERSION_VERSION}.tar.gz")
    onlyIfNewer(true)
    overwrite(false)
    dest(new File(downloadsDir, "double-conversion-${DOUBLE_CONVERSION_VERSION}.tar.gz"))
}

task prepareDoubleConversion(dependsOn: dependenciesPath ? [] : [downloadDoubleConversion], type: Copy) {
    from(dependenciesPath ?: tarTree(downloadDoubleConversion.dest))
    from("$reactNativeThirdParty/double-conversion/Android.mk")
    include("double-conversion-${DOUBLE_CONVERSION_VERSION}/src/**/*", "Android.mk")
    filesMatching("*/src/**/*", { fname -> fname.path = "double-conversion/${fname.name}" })
    includeEmptyDirs = false
    into("$thirdPartyNdkDir/double-conversion")
}

task downloadFolly(dependsOn: createNativeDepsDirectories, type: Download) {
    src("https://github.com/facebook/folly/archive/v${FOLLY_VERSION}.tar.gz")
    onlyIfNewer(true)
    overwrite(false)
    dest(new File(downloadsDir, "folly-${FOLLY_VERSION}.tar.gz"))
}

task prepareFolly(dependsOn: dependenciesPath ? [] : [downloadFolly], type: Copy) {
    from(dependenciesPath ?: tarTree(downloadFolly.dest))
    from("$reactNativeThirdParty/folly/Android.mk")
    include("folly-${FOLLY_VERSION}/folly/**/*", "Android.mk")
    eachFile { fname -> fname.path = (fname.path - "folly-${FOLLY_VERSION}/") }
    // Fixes problem with Folly failing to build on certain systems. See
    // https://github.com/software-mansion/react-native-reanimated/issues/1024
    filter { line -> line.replaceAll('return int\\(wrapNoInt\\(open, name, flags, mode\\)\\);', follyReplaceContent) }
    includeEmptyDirs = false
    into("$thirdPartyNdkDir/folly")
}

task downloadGlog(dependsOn: createNativeDepsDirectories, type: Download) {
    src("https://github.com/google/glog/archive/v${GLOG_VERSION}.tar.gz")
    onlyIfNewer(true)
    overwrite(false)
    dest(new File(downloadsDir, "glog-${GLOG_VERSION}.tar.gz"))
}

// Prepare glog sources to be compiled, this task will perform steps that normally should've been
// executed by automake. This way we can avoid dependencies on make/automake
task prepareGlog(dependsOn: dependenciesPath ? [] : [downloadGlog], type: Copy) {
    duplicatesStrategy = 'include'
    from(dependenciesPath ?: tarTree(downloadGlog.dest))
    from("$reactNativeThirdParty/glog/")
    include("glog-${GLOG_VERSION}/src/**/*", "Android.mk", "config.h")
    includeEmptyDirs = false
    filesMatching("**/*.h.in") {
        filter(ReplaceTokens, tokens: [
                ac_cv_have_unistd_h           : "1",
                ac_cv_have_stdint_h           : "1",
                ac_cv_have_systypes_h         : "1",
                ac_cv_have_inttypes_h         : "1",
                ac_cv_have_libgflags          : "0",
                ac_google_start_namespace     : "namespace google {",
                ac_cv_have_uint16_t           : "1",
                ac_cv_have_u_int16_t          : "1",
                ac_cv_have___uint16           : "0",
                ac_google_end_namespace       : "}",
                ac_cv_have___builtin_expect   : "1",
                ac_google_namespace           : "google",
                ac_cv___attribute___noinline  : "__attribute__ ((noinline))",
                ac_cv___attribute___noreturn  : "__attribute__ ((noreturn))",
                ac_cv___attribute___printf_4_5: "__attribute__((__format__ (__printf__, 4, 5)))"
        ])
        it.path = (it.name - ".in")
    }
    into("$thirdPartyNdkDir/glog")

    doLast {
        copy {
            from(fileTree(dir: "$thirdPartyNdkDir/glog", includes: ["stl_logging.h", "logging.h", "raw_logging.h", "vlog_is_on.h", "**/src/glog/log_severity.h"]).files)
            includeEmptyDirs = false
            into("$thirdPartyNdkDir/glog/exported/glog")
        }
    }
}



/**
 * Finds the path of the installed npm package with the given name using Node's
 * module resolution algorithm, which searches "node_modules" directories up to
 * the file system root. This handles various cases, including:
 *
 *   - Working in the open-source RN repo:
 *       Gradle: /path/to/react-native/ReactAndroid
 *       Node module: /path/to/react-native/node_modules/[package]
 *
 *   - Installing RN as a dependency of an app and searching for hoisted
 *     dependencies:
 *       Gradle: /path/to/app/node_modules/react-native/ReactAndroid
 *       Node module: /path/to/app/node_modules/[package]
 *
 *   - Working in a larger repo (e.g., Facebook) that contains RN:
 *       Gradle: /path/to/repo/path/to/react-native/ReactAndroid
 *       Node module: /path/to/repo/node_modules/[package]
 *
 * The search begins at the given base directory (a File object). The returned
 * path is a string.
 */
static def findNodeModulePath(baseDir, packageName) {
    def basePath = baseDir.toPath().normalize()
    // Node's module resolution algorithm searches up to the root directory,
    // after which the base path will be null
    while (basePath) {
        def candidatePath = Paths.get(basePath.toString(), "node_modules", packageName)
        if (candidatePath.toFile().exists()) {
            return candidatePath.toString()
        }
        basePath = basePath.getParent()
    }
    return null
}

static def getNdkBuildName() {
    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        return "ndk-build.cmd"
    } else {
        return "ndk-build"
    }
}

def findNdkBuildFullPath() {
    // we allow to provide full path to ndk-build tool
    if (hasProperty("ndk.command")) {
        return property("ndk.command")
    }
    // or just a path to the containing directory
    if (hasProperty("ndk.path")) {
        def ndkDir = property("ndk.path")
        return new File(ndkDir, getNdkBuildName()).getAbsolutePath()
    }

    if (System.getenv("ANDROID_NDK") != null) {
        def ndkDir = System.getenv("ANDROID_NDK")
        return new File(ndkDir, getNdkBuildName()).getAbsolutePath()
    }

    if (hasProperty("ndkDirectory")) {
        def ndkDir = android.ndkDirectory ? android.ndkDirectory.absolutePath : null

        if (ndkDir) {
            return new File(ndkDir, getNdkBuildName()).getAbsolutePath()
        }
    }

    def Properties properties = new Properties()
    if (rootProject.file("local.properties").exists()) {
        properties.load(project.rootProject.file("local.properties").newDataInputStream())
        def ndkDir=properties.getProperty("ndk.dir", null);
        if(ndkDir)
        {
            return new File(ndkDir, getNdkBuildName()).getAbsolutePath()
        }
    }
    return null
}

def getNdkBuildFullPath() {
    def ndkBuildFullPath = findNdkBuildFullPath()
    if (ndkBuildFullPath == null) {
        throw new GradleScriptException(
                "ndk-build binary cannot be found, check if you've set " +
                        "\$ANDROID_NDK environment variable correctly or if ndk.dir is " +
                        "setup in local.properties",
                null)
    }
    if (!new File(ndkBuildFullPath).canExecute()) {
        throw new GradleScriptException(
                "ndk-build binary " + ndkBuildFullPath + " doesn't exist or isn't executable.\n" +
                        "Check that the \$ANDROID_NDK environment variable, or ndk.dir in local.properties, is set correctly.\n" +
                        "(On Windows, make sure you escape backslashes in local.properties or use forward slashes, e.g. C:\\\\ndk or C:/ndk rather than C:\\ndk)",
                null)
    }
    return ndkBuildFullPath
}

task prepareHermes() {
    def hermesPackagePath = findNodeModulePath(projectDir, "hermes-engine")
    if (!hermesPackagePath) {
        throw new GradleScriptException("Could not find the hermes-engine npm package", null)
    }

    def hermesAAR = file("$hermesPackagePath/android/hermes-debug.aar")
    if (!hermesAAR.exists()) {
        throw new GradleScriptException("The hermes-engine npm package is missing \"android/hermes-debug.aar\"", null)
    }

    def soFiles = zipTree(hermesAAR).matching({ it.include "**/*.so" })

    copy {
        from soFiles
        from "$reactNative/ReactAndroid/src/main/jni/first-party/hermes/Android.mk"
        into "$thirdPartyNdkDir/hermes"
    }
}

task prepareJSC {
    doLast {
        def jscPackagePath = findNodeModulePath(projectDir, "jsc-android")
        if (!jscPackagePath) {
            throw new GradleScriptException("Could not find the jsc-android npm package", null)
        }

        def jscDist = file("$jscPackagePath/dist")
        if (!jscDist.exists()) {
            throw new GradleScriptException("The jsc-android npm package is missing its \"dist\" directory", null)
        }

        def jscAAR = fileTree(jscDist).matching({ it.include "**/android-jsc/**/*.aar" }).singleFile
        def soFiles = zipTree(jscAAR).matching({ it.include "**/*.so" })

        def headerFiles = fileTree(jscDist).matching({ it.include "**/include/*.h" })

        copy {
            from(soFiles)
            from(headerFiles)
            from("$reactNative/ReactAndroid/src/main/jni/third-party/jsc/Android.mk")

            filesMatching("**/*.h", { it.path = "JavaScriptCore/${it.name}" })

            includeEmptyDirs(false)
            into("$thirdPartyNdkDir/jsc")
        }
    }
}

task extractAARHeaders {
    doLast {
        configurations.extractHeaders.files.each {
            def file = it.absoluteFile
            def packageName = file.name.tokenize('-')[0]
            copy {
                from zipTree(file)
                into "$reactNative/ReactAndroid/src/main/jni/first-party/$packageName/headers"
                include "**/*.h"
            }
        }
    }
}

task extractSOFiles {
    doLast {
        configurations.extractSO.files.each {
            def file = it.absoluteFile
            def packageName = file.name.tokenize('-')[0]
            copy {
                from zipTree(file)
                into "$reactNative/ReactAndroid/src/main/jni/first-party/$packageName/"
                include "jni/**/*.so"
            }
        }
    }
}


task packageNdkLibs(type: Copy) {
    from("$buildDir/reanimated-ndk/all")
    include("**/libreanimated.so")
    if(REACT_VERSION < 64) {
        include("**/libturbomodulejsijni.so")
    }
    into("$projectDir/src/main/jniLibs")
}

repositories {
    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()
    // FIXME jcenter is deprecated and will be removed soon, however some modules still doesn't migrate to maven central
    jcenter() {
        content {
            includeModule("com.facebook.fbjni", "fbjni-java-only")
            includeModule("com.facebook.yoga", "proguard-annotations")

        }
    }
}

dependencies {
    //noinspection GradleDynamicVersion
    implementation 'com.facebook.fbjni:fbjni:' + FBJNI_VERSION
    implementation 'com.facebook.react:react-native:+' // From node_modules
    implementation "androidx.transition:transition:1.1.0"
    extractHeaders("com.facebook.fbjni:fbjni:" + FBJNI_VERSION + ":headers")
    extractSO("com.facebook.fbjni:fbjni:" + FBJNI_VERSION)

    def rnAAR = fileTree("${rootDir}/../node_modules/react-native/android").matching({ it.include "**/**/*.aar" }).singleFile
    def jscAAR = fileTree("${rootDir}/../node_modules/jsc-android/dist/org/webkit/android-jsc").matching({ it.include "**/**/*.aar" }).singleFile
    extractSO(files(rnAAR, jscAAR))
}

task downloadNdkBuildDependencies {
    if (!boostPath) {
        dependsOn(downloadBoost)
    }
    dependsOn(downloadDoubleConversion)
    dependsOn(downloadFolly)
    dependsOn(downloadGlog)
}

task prepareThirdPartyNdkHeaders(dependsOn:[downloadNdkBuildDependencies, prepareBoost, prepareDoubleConversion, prepareFolly, prepareGlog]) {
}

tasks.whenTaskAdded { task ->
    if (task.name.contains('externalNativeBuild')) {
        task.dependsOn(prepareThirdPartyNdkHeaders)
        extractAARHeaders.dependsOn(prepareThirdPartyNdkHeaders)
        extractSOFiles.dependsOn(prepareThirdPartyNdkHeaders)
        task.dependsOn(extractAARHeaders)
        task.dependsOn(extractSOFiles)
    }
}

