import java.nio.file.Paths

buildscript {
  repositories {
    google()
    mavenCentral()
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:7.4.2'
    // noinspection DifferentKotlinGradleVersion
  }
}

apply plugin: 'com.android.library'

def getExtOrDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['OnnxruntimeModule_' + name]
}

def getExtOrIntegerDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties['OnnxruntimeModule_' + name]).toInteger()
}

def reactNativeArchitectures() {
  def value = project.getProperties().get("reactNativeArchitectures")
  return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
}

def resolveBuildType() {
  Gradle gradle = getGradle()
  String tskReqStr = gradle.getStartParameter().getTaskRequests()['args'].toString()
  return tskReqStr.contains('Release') ? 'release' : 'debug'
}

static def findNodeModules(baseDir) {
  def basePath = baseDir.toPath().normalize()
  while (basePath) {
    def nodeModulesPath = Paths.get(basePath.toString(), "node_modules")
    def reactNativePath = Paths.get(nodeModulesPath.toString(), "react-native")
    if (nodeModulesPath.toFile().exists() && reactNativePath.toFile().exists()) {
      return nodeModulesPath.toString()
    }
    basePath = basePath.getParent()
  }
  throw new GradleException("onnxruntime-react-native: Failed to find node_modules/ path!")
}

def nodeModules = findNodeModules(projectDir);

def readPackageJsonField(field) {
  // locate user's project dir
  def reactnativeRootDir = project.rootDir.parentFile
  // get package.json file in root directory
  def packageJsonFile = new File(reactnativeRootDir, 'package.json')
  if (packageJsonFile.exists()) {
    def packageJsonContents = packageJsonFile.getText()
    def packageJson = new groovy.json.JsonSlurper().parseText(packageJsonContents)
    return packageJson.get(field)
  } else {
    logger.warn("Could not find package.json file in the expected directory: ${reactnativeRootDir}. ${field} will not be enabled.")
  }
}

boolean ortExtensionsEnabled = readPackageJsonField('onnxruntimeExtensionsEnabled') == "true"
boolean useQnn = readPackageJsonField('onnxruntimeUseQnn') == "true"

def REACT_NATIVE_VERSION = ['node', '--print', "JSON.parse(require('fs').readFileSync(require.resolve('react-native/package.json'), 'utf-8')).version"].execute(null, rootDir).text.trim()
def REACT_NATIVE_MINOR_VERSION = REACT_NATIVE_VERSION.split("\\.")[1].toInteger()

android {
// This is needed by the new AndroidManifestNew.xml
  namespace "ai.onnxruntime.reactnative"
  compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')
  buildToolsVersion getExtOrDefault('buildToolsVersion')
  defaultConfig {
    minSdkVersion getExtOrIntegerDefault('minSdkVersion')
    targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    externalNativeBuild {
      cmake {
        cppFlags "-O2 -frtti -fexceptions -Wall -Wno-unused-variable -fstack-protector-all"
        if (REACT_NATIVE_MINOR_VERSION >= 71) {
          // fabricjni required c++_shared
          arguments "-DANDROID_STL=c++_shared",
            "-DNODE_MODULES_DIR=${nodeModules}",
            "-DORT_EXTENSIONS_ENABLED=${ortExtensionsEnabled}",
            "-DREACT_NATIVE_VERSION=${REACT_NATIVE_VERSION}",
            "-DUSE_QNN=${useQnn}",
            "-DUSE_NNAPI=${!useQnn}"
        } else {
          arguments "-DNODE_MODULES_DIR=${nodeModules}",
            "-DORT_EXTENSIONS_ENABLED=${ortExtensionsEnabled}",
            "-DREACT_NATIVE_VERSION=${REACT_NATIVE_VERSION}",
            "-DUSE_QNN=${useQnn}",
            "-DUSE_NNAPI=${!useQnn}"
        }
        abiFilters (*reactNativeArchitectures())
      }
    }
  }

  if (rootProject.hasProperty("ndkPath")) {
    ndkPath rootProject.ext.ndkPath
  }
  if (rootProject.hasProperty("ndkVersion")) {
    ndkVersion rootProject.ext.ndkVersion
  }

  buildFeatures {
    prefab true
  }

  externalNativeBuild {
    cmake {
      path "CMakeLists.txt"
    }
  }

  packagingOptions {
    pickFirst '**/libc++_shared.so'
    pickFirst '**/libfbjni.so'
    doNotStrip resolveBuildType() == 'debug' ? "**/**/*.so" : ''
    excludes = [
      "META-INF",
      "META-INF/**",
      "**/libjsi.so",
      "**/libfbjni.so",
      "**/libreact_nativemodule_core.so",
      "**/libturbomodulejsijni.so"
    ]
  }

  buildTypes {
    release {
      minifyEnabled false
    }
  }
  lintOptions {
    disable 'GradleCompatible'
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
  }

  sourceSets {
    main {
      java.srcDirs = ['src/main/java/']
//       A tricky situation where iOS still uses the AndroidManifest.xml file, but the Android use AndroidManifestNew.xml
      manifest.srcFile "src/main/AndroidManifestNew.xml"
      if (ortExtensionsEnabled) {
        java.exclude '**/OnnxruntimeExtensionsDisabled.java'
      } else {
        java.exclude '**/OnnxruntimeExtensionsEnabled.java'
      }
    }
  }

  configurations {
    extractLibs
  }
}

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 {
  //noinspection GradleDynamicVersion
  implementation "com.facebook.react:react-android:"+ REACT_NATIVE_VERSION
  api "org.mockito:mockito-core:2.28.2"

  implementation "junit:junit:4.12"

  if (useQnn) {
    extractLibs "com.microsoft.onnxruntime:onnxruntime-android-qnn:latest.integration@aar"
  } else {
    extractLibs "com.microsoft.onnxruntime:onnxruntime-android:latest.integration@aar"
  }

  if (VersionNumber.parse(REACT_NATIVE_VERSION) < VersionNumber.parse("0.71")) {
    extractLibs "com.facebook.fbjni:fbjni:+:headers"
    extractLibs "com.facebook.fbjni:fbjni:+"
  }

  // By default it will just include onnxruntime full aar package
  if (ortExtensionsEnabled) {
    implementation "com.microsoft.onnxruntime:onnxruntime-extensions-android:latest.integration@aar"
  }
}

task extractLibs {
  doLast {
    configurations.extractLibs.files.each {
      def file = it.absoluteFile
      copy {
        from zipTree(file)
        into "$buildDir/$file.name"
        include "**/*.h", "**/*.so"
      }
    }
  }
}

def nativeBuildDependsOn(dependsOnTask, variant) {
  def buildTasks = tasks.findAll({ task ->
      !task.name.contains("Clean") && (task.name.contains("externalNative") || task.name.contains("CMake")) })
  if (variant != null) {
    buildTasks = buildTasks.findAll({ task -> task.name.contains(variant) })
  }
  buildTasks.forEach { task -> task.dependsOn(dependsOnTask) }
}

afterEvaluate {
  nativeBuildDependsOn(extractLibs, null)
}
