import groovy.json.JsonSlurper
import java.nio.file.Paths

buildscript {

  def kotlin_version = rootProject.ext.has('kotlinVersion') ? rootProject.ext.get('kotlinVersion') : project.properties['MlkitOcr_kotlinVersion']

  ext.safeExtGet = { prop ->
    rootProject.ext.has(prop) ? rootProject.ext.get(prop) : project.properties["MlkitOcr_${prop}"]
  }

  repositories {
    google()
    gradlePluginPortal()
    mavenCentral()
  }

  dependencies {
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
  }
}

def getExtOrDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["MlkitOcr_${name}"]
}

def isNewArchitectureEnabled() {
  return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}

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
}

def findNodeModulePath(packageName) {
  // Don't start in the project dir, as its path ends with node_modules/react-native-mlkit-ocr/android
  // we want to go two levels up, so we end up in the first_node modules and eventually
  // search upwards if the package is not found there
  return findNodeModulePath(projectDir.toPath().parent.parent.toFile(), packageName)
}

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

if (isNewArchitectureEnabled()) {
  apply plugin: 'com.facebook.react'
}

android {
  compileSdkVersion safeExtGet('compileSdkVersion')

  // Used to override the NDK path/version on internal CI or by allowing
  // users to customize the NDK path/version from their root project (e.g. for M1 support)
  if (rootProject.hasProperty("ndkPath")) {
    ndkPath rootProject.ext.ndkPath
  }
  if (rootProject.hasProperty("ndkVersion")) {
    ndkVersion rootProject.ext.ndkVersion
  }

  defaultConfig {
    minSdkVersion getExtOrDefault('minSdkVersion')
    targetSdkVersion getExtOrDefault('targetSdkVersion')
    buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
    versionCode 110
    versionName "1.1.0"
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

  lintOptions {
    abortOnError false
  }

  sourceSets {
    main {
      if (isNewArchitectureEnabled()) {
        java.srcDirs += ["src/newarch",
                         "${project.buildDir}/generated/source/codegen/java"]
      } else {
        java.srcDirs += ["src/oldarch"]
      }
    }
  }
}

repositories {
  maven {
    // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
    url "${rootDir}/../node_modules/react-native/android"
  }
  mavenCentral()
  google()
}

def kotlin_version = getExtOrDefault('kotlinVersion')

dependencies {

  if (isNewArchitectureEnabled()) {
    implementation project(":ReactAndroid")
  } else {
    implementation 'com.facebook.react:react-native:+'
  }

  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

  implementation 'com.google.android.gms:play-services-mlkit-text-recognition:18.0.2'
}

if (isNewArchitectureEnabled()) {
  react {
    reactNativeDir = rootProject.file(findNodeModulePath(rootProject.rootDir, "react-native") ?: "../node_modules/react-native/")
    jsRootDir = file("../src/")
    codegenDir = rootProject.file(findNodeModulePath(rootProject.rootDir, "react-native-codegen") ?: "../node_modules/react-native-codegen/")
    libraryName = "mlkitocr"
    codegenJavaPackageName = "com.reactnativemlkitocr"
  }

  // Resolves "LOCAL_SRC_FILES points to a missing file, Check that libfb.so exists or that its path is correct".
  tasks.whenTaskAdded { task ->
    if (task.name.contains("configureCMakeDebug")) {
      rootProject.getTasksByName("packageReactNdkDebugLibs", true).forEach {
        task.dependsOn(it)
      }
    }
    // We want to add a dependency for both configureCMakeRelease and configureCMakeRelWithDebInfo
    if (task.name.contains("configureCMakeRel")) {
      rootProject.getTasksByName("packageReactNdkReleaseLibs", true).forEach {
        task.dependsOn(it)
      }
    }
  }
}
