import java.nio.file.Paths
import com.android.Version

def agpVersion = Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')[0].toInteger()
def androidManifestPath = agpVersion >= 7 ? 'src/main/AndroidManifest.xml' : 'src/hasNamespace/AndroidManifest.xml'

buildscript {
  def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["VisionCamera_kotlinVersion"]

  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
    mavenCentral()
    google()
  }

  dependencies {
    classpath "com.android.tools.build:gradle:8.7.2"
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
  }
}

def resolveBuildType() {
    Gradle gradle = getGradle()
    String tskReqStr = gradle.getStartParameter().getTaskRequests()["args"].toString()

    return tskReqStr.contains("Release") ? "release" : "debug"
}

def isNewArchitectureEnabled() {
  // To opt-in for the New Architecture, you can either:
  // - Set `newArchEnabled` to true inside the `gradle.properties` file
  // - Invoke gradle with `-newArchEnabled=true`
  // - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
  return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}

if (isNewArchitectureEnabled()) {
  apply plugin: "com.facebook.react"
}
apply plugin: "com.android.library"
apply plugin: "kotlin-android"

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

def safeExtGetBool(prop, fallback) {
  Boolean.parseBoolean("${safeExtGet(prop, fallback)}")
}

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

static def findNodeModules(baseDir) {
  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 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("react-native-vision-camera: Failed to find node_modules/ path!")
}

logger.warn("[VisionCamera] Thank you for using VisionCamera ❤️")
logger.warn("[VisionCamera] If you enjoy using VisionCamera, please consider sponsoring this project: https://github.com/sponsors/mrousavy")

def nodeModules = findNodeModules(projectDir)
logger.warn("[VisionCamera] node_modules found at $nodeModules")

def enableFrameProcessors = safeExtGetBool('VisionCamera_enableFrameProcessors', true)
logger.warn("[VisionCamera] VisionCamera_enableFrameProcessors is set to $enableFrameProcessors!")

def hasWorklets = findProject(":react-native-worklets-core") != null
if (hasWorklets) {
  logger.warn("[VisionCamera] react-native-worklets-core found, Frame Processors are ${enableFrameProcessors ? "enabled" : "disabled"}!")
} else {
  logger.warn("[VisionCamera] react-native-worklets-core not found, Frame Processors are disabled!")
  enableFrameProcessors = false
}

def enableCodeScanner = safeExtGetBool('VisionCamera_enableCodeScanner', false)
logger.warn("[VisionCamera] VisionCamera_enableCodeScanner is set to $enableCodeScanner!")

repositories {
  google()
  mavenCentral()
}

android {
  if (agpVersion >= 7) {
    namespace "com.mrousavy.camera"
  }

  // 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
  }

  buildFeatures {
    prefab true
    prefabPublishing true
  }

  prefab {
    VisionCamera {
      headers "${project.buildDir}/headers/visioncamera/"
    }
  }

  defaultConfig {
    minSdkVersion safeExtGet("minSdkVersion", 21)
    compileSdkVersion safeExtGet("compileSdkVersion", 34)
    targetSdkVersion safeExtGet("targetSdkVersion", 34)
    versionCode 1
    versionName "1.0"
    buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()

    externalNativeBuild {
      cmake {
        cppFlags "-O2 -frtti -fexceptions -Wall -Wno-unused-variable -fstack-protector-all"
        arguments "-DANDROID_STL=c++_shared",
                "-DNODE_MODULES_DIR=${nodeModules}",
                "-DENABLE_FRAME_PROCESSORS=${enableFrameProcessors ? "ON" : "OFF"}"
        abiFilters (*reactNativeArchitectures())
      }
    }
  }

  sourceSets {
    main {
      manifest.srcFile androidManifestPath
    }
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

  externalNativeBuild {
    cmake {
      path "CMakeLists.txt"
    }
  }
  packagingOptions {
    doNotStrip resolveBuildType() == "debug" ? "**/**/*.so" : ""
    excludes = [
            "META-INF",
            "META-INF/**",
            "**/libc++_shared.so",
            "**/libfbjni.so",
            "**/libjsi.so",
            "**/libfolly_json.so",
            "**/libfolly_runtime.so",
            "**/libglog.so",
            "**/libhermes.so",
            "**/libhermes-executor-debug.so",
            "**/libhermes_executor.so",
            "**/libreactnative.so",
            "**/libreactnativejni.so",
            "**/libturbomodulejsijni.so",
            "**/libreact_nativemodule_core.so",
            "**/libjscexecutor.so"
    ]
  }
}

dependencies {
  //noinspection GradleDynamicVersion
  implementation "com.facebook.react:react-android:+"

  // CameraX dependency
  def camerax_version = "1.5.0-alpha03"
  implementation "androidx.camera:camera-core:${camerax_version}"
  implementation "androidx.camera:camera-camera2:${camerax_version}"
  implementation "androidx.camera:camera-lifecycle:${camerax_version}"
  implementation "androidx.camera:camera-video:${camerax_version}"
  implementation "androidx.camera:camera-view:${camerax_version}"
  implementation "androidx.camera:camera-extensions:${camerax_version}"

  // Some Coroutines extension functions
  implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.9.0"

  if (enableCodeScanner) {
    // User enabled code-scanner, so we bundle the 2.4 MB model in the app.
    implementation 'com.google.mlkit:barcode-scanning:17.3.0'
  } else {
    // Fall-back to just including the code for the CodeScanner to avoid the 2.4 MB bundle in the app.
    // On devices with Google Play Services, this can also download the CodeScanner model on-demand.
    implementation "com.google.android.gms:play-services-mlkit-barcode-scanning:18.3.1"
  }

  if (enableFrameProcessors) {
    // Frame Processor integration (optional)
    implementation project(":react-native-worklets-core")
  }
}

task deleteCmakeCache() {
  doFirst {
    delete "${projectDir}/.cxx"
    delete "${nodeModules}/react-native-vision-camera/android/.cxx"
    delete "${nodeModules}/react-native-vision-camera/android/build"
  }
}

task prepareHeaders(type: Copy) {
  from fileTree('./src/main/cpp').filter { it.isFile() }
  include "*.h"
  into "${project.buildDir}/headers/visioncamera/react-native-vision-camera/"
  includeEmptyDirs = false
}

preBuild.dependsOn(prepareHeaders)

tasks.configureEach { task ->
  // C++ build
  if (task.name.contains("configureCMakeDebug")) {
    rootProject.getTasksByName("packageReactNdkDebugLibs", true).forEach {
      task.dependsOn(it)
    }
  }
  if (task.name.contains("configureCMakeRel")) {
    rootProject.getTasksByName("packageReactNdkReleaseLibs", true).forEach {
      task.dependsOn(it)
    }
  }
  // C++ clean
  if (task.name.contains("clean")) {
    task.dependsOn(deleteCmakeCache)
  }
}
