// Android build script for react-native-webrtc-kaleidoscope.

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

group = 'com.simiancraft.kaleidoscope'
// Single source of truth for the version is package.json (the iOS podspec reads
// it the same way), so the three platforms cannot drift apart.
version = new groovy.json.JsonSlurper().parseText(file("../package.json").text).version

def expoModulesCorePlugin = new File(project(":expo-modules-core").projectDir.absolutePath, "ExpoModulesCorePlugin.gradle")
if (expoModulesCorePlugin.exists()) {
  apply from: expoModulesCorePlugin
  applyKotlinExpoModulesCorePlugin()
}

android {
  namespace 'com.simiancraft.kaleidoscope'
  compileSdkVersion safeExtGet('compileSdkVersion', 34)

  defaultConfig {
    minSdkVersion safeExtGet('minSdkVersion', 24)
    targetSdkVersion safeExtGet('targetSdkVersion', 34)
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
  }

  kotlinOptions {
    jvmTarget = '17'
  }

  // JVM unit tests (testDebugUnitTest) for pure-logic units like the composite
  // layer-stack parser. returnDefaultValues makes android.util.Log a no-op stub
  // instead of throwing; the real org.json on the test classpath (below) shadows
  // the stubbed android.jar one so JSON parsing actually runs.
  testOptions {
    unitTests.returnDefaultValues = true
  }
}

dependencies {
  implementation project(':expo-modules-core')

  // react-native-webrtc is a peer dependency: link against its types
  // (ProcessorProvider, VideoFrameProcessor, VideoFrameProcessorFactoryInterface,
  // and the org.webrtc.* surface it bundles) at compile time only; the consumer
  // app provides the runtime via autolinking.
  //
  // Two forks ship the same classes under `com.oney.WebRTCModule.videoEffects.*`:
  //   - react-native-webrtc/react-native-webrtc (upstream)
  //   - livekit/react-native-webrtc (LiveKit fork, required by @livekit/react-native)
  // Pick whichever the consumer's autolinking surfaced; throw if neither is present.
  def rnWebrtcCandidates = [':react-native-webrtc', ':livekit_react-native-webrtc']
  def rnWebrtcProject = rnWebrtcCandidates.find { rootProject.findProject(it) != null }
  if (rnWebrtcProject == null) {
    throw new GradleException(
      'react-native-webrtc-kaleidoscope: install either react-native-webrtc ' +
      'or @livekit/react-native-webrtc as a peer dependency.'
    )
  }
  compileOnly project(rnWebrtcProject)

  // Person/background mask: MediaPipe Tasks ImageSegmenter (Mask.kt), the same
  // model family the web and iOS sides run.
  implementation 'com.google.mediapipe:tasks-vision:0.10.14'

  // Unit tests (JVM, testDebugUnitTest). The real org.json shadows the stubbed
  // android.jar copy so CompositeLayers' JSONArray/JSONObject parsing runs for real.
  testImplementation 'junit:junit:4.13.2'
  testImplementation 'org.json:json:20231013'
}

// Helper: gracefully read root project ext properties with a default.
def safeExtGet(prop, fallback) {
  rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}
