// Android library module for react-native-video-pipeline.
//
// Routed to the Kotlin `HybridVideoPipeline` class via the nitrogen-generated
// OnLoad.cpp (nitro.json autolinks the HybridObject by its `kotlin:` key, so
// the registration is a JNI factory that default-constructs the Kotlin class).
// The Kotlin class at T040 throws typed "not implemented yet on Android"
// errors from every Spec method — subsequent tasks (T041+) replace each throw
// with Media3 Transformer / MediaMetadataRetriever / MediaCodecList logic.

apply plugin: "com.android.library"
apply plugin: "org.jetbrains.kotlin.android"

def getExtOrDefault(name, defaultValue) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : defaultValue
}

// Honour the `reactNativeArchitectures` Gradle property (set in the consuming
// app's gradle.properties, or via -P) so this module's native build targets the
// same ABI set as react-native-nitro-modules and the app. Without this the
// `com.android.library` module builds all four ABIs regardless — and if a build
// narrows the ABI set (e.g. emulator-only x86_64 in CI), NitroModules ships only
// that ABI's libnitromodules.so while this module still tries to link an arm64
// libNitroVideoPipeline.so against a missing prefab → "undefined symbol:
// margelo::nitro::JHybridObject". Unset (the default for a plain consumer) keeps
// the previous all-ABI behaviour.
def reactNativeArchitectures() {
  def value = rootProject.findProperty("reactNativeArchitectures")
  def parsed = value ? value.split(",").collect { it.trim() }.findAll { it } : null
  return parsed ? parsed : ["armeabi-v7a", "arm64-v8a", "x86", "x86_64"]
}

android {
  namespace "com.nightlybuildgroup.videopipeline"

  compileSdkVersion getExtOrDefault("compileSdkVersion", 35)
  ndkVersion getExtOrDefault("ndkVersion", "26.1.10909125")
  buildToolsVersion getExtOrDefault("buildToolsVersion", "35.0.0")

  defaultConfig {
    minSdkVersion getExtOrDefault("minSdkVersion", 24)
    targetSdkVersion getExtOrDefault("targetSdkVersion", 34)
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    ndk {
      abiFilters(*reactNativeArchitectures())
    }

    externalNativeBuild {
      cmake {
        cppFlags "-O2 -frtti -fexceptions -Wall -Werror -std=c++20"
        arguments "-DANDROID_STL=c++_shared",
                  "-DANDROID_TOOLCHAIN=clang",
                  "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
      }
    }
  }

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

  buildFeatures {
    prefab true
  }

  // Keep resource / test folders minimal at the skeleton stage — T041+
  // wires in real sources as each task adds them.
  sourceSets {
    main {
      java.srcDirs += ["src/main/java"]
      manifest.srcFile "src/main/AndroidManifest.xml"
    }
    androidTest {
      java.srcDirs += ["src/androidTest/java"]
    }
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
  }

  kotlinOptions {
    jvmTarget = "17"
  }

  packaging {
    // The Nitro shared lib ships its own `libc++_shared.so`; exclude ours so
    // the app doesn't see two copies.
    jniLibs {
      excludes += ["**/libc++_shared.so"]
      // The instrumented-test APK pulls the RN/JSI prefab .so's (libfbjni,
      // libjsi, libreactnative, …) from both this module's prefab copy and the
      // react-android transform — take the first so the androidTest merge
      // doesn't fail on the duplicates. Affects only apk/test-apk assembly,
      // never the shipped AAR (the consuming app does its own packaging).
      pickFirsts += ["**/*.so"]
    }
  }
}

repositories {
  mavenCentral()
  google()
}

dependencies {
  implementation "com.facebook.react:react-android"
  implementation project(":react-native-nitro-modules")
  implementation "org.jetbrains.kotlin:kotlin-stdlib"

  // Media3 Transformer is the canonical Android engine for trim + transform +
  // re-encode (docs/architecture.md). The render-with-transform path uses it:
  // it owns the decode→effects→encode pipeline (no hand-rolled codec pump),
  // preserves audio by default, trims via ClippingConfiguration, and
  // transmuxes when no re-encode is needed.
  def media3Version = getExtOrDefault("media3Version", "1.5.1")
  implementation "androidx.media3:media3-transformer:$media3Version"
  implementation "androidx.media3:media3-effect:$media3Version"
  implementation "androidx.media3:media3-common:$media3Version"

  // On-device (instrumented) tests for the pure-Kotlin media surface
  // (Transcoder / OverlayTextRasterizer / SynthesizeRunner). See
  // src/androidTest. Not shipped — androidTest deps never enter the AAR.
  androidTestImplementation "androidx.test.ext:junit:1.1.5"
  androidTestImplementation "androidx.test:runner:1.5.2"
}

// Pull in the nitrogen-generated Kotlin + CMake extensions. The autolinker
// adds the generated kotlin/ source tree to this module and wires the shared
// / android-specific C++ sources into NitroVideoPipeline's CMake target.
apply from: "../nitrogen/generated/android/NitroVideoPipeline+autolinking.gradle"
