buildscript {
  ext.Aviation = [
    kotlinVersion: "2.0.21",
    ndkVersion: "27.1.12297006",
    minSdkVersion: 24,
    compileSdkVersion: 36,
    targetSdkVersion: 36
  ]

  ext.getExtOrDefault = { prop ->
    if (rootProject.ext.has(prop)) {
      return rootProject.ext.get(prop)
    }

    return Aviation[prop]
  }

  repositories {
    google()
    mavenCentral()
  }

  dependencies {
    classpath "com.android.tools.build:gradle:8.7.2"
    // noinspection DifferentKotlinGradleVersion
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion')}"
  }
}

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

apply plugin: "com.android.library"
apply plugin: "kotlin-android"
apply from: '../nitrogen/generated/android/aviation+autolinking.gradle'

apply plugin: "com.facebook.react"

android {
  namespace "com.margelo.nitro.aviation"

  testOptions {
    unitTests {
      // Robolectric needs the merged resources/manifest to build its sandbox.
      includeAndroidResources = true
      returnDefaultValues = true
    }
  }

  ndkVersion getExtOrDefault("ndkVersion")
  compileSdkVersion getExtOrDefault("compileSdkVersion")

  defaultConfig {
    minSdkVersion getExtOrDefault("minSdkVersion")
    targetSdkVersion getExtOrDefault("targetSdkVersion")

    externalNativeBuild {
      cmake {
        cppFlags "-frtti -fexceptions -Wall -fstack-protector-all"
        arguments "-DANDROID_STL=c++_shared", "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
        abiFilters (*reactNativeArchitectures())

        buildTypes {
          debug {
            cppFlags "-O1 -g"
          }
          release {
            cppFlags "-O2"
          }
        }
      }
    }
  }

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

  packagingOptions {
    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"
    ]
  }

  buildFeatures {
    buildConfig true
    prefab true
  }

  buildTypes {
    release {
      minifyEnabled false
    }
  }

  lint {
    disable "GradleCompatible"
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

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

  // Media3 (ExoPlayer)
  def media3Version = "1.6.1"
  implementation "androidx.media3:media3-common:$media3Version"
  implementation "androidx.media3:media3-exoplayer:$media3Version"
  implementation "androidx.media3:media3-exoplayer-hls:$media3Version"
  implementation "androidx.media3:media3-exoplayer-dash:$media3Version"
  implementation "androidx.media3:media3-session:$media3Version"
  implementation "androidx.media3:media3-ui:$media3Version"
  implementation "androidx.media3:media3-datasource-okhttp:$media3Version"

  // OkHttp (used by media3-datasource-okhttp for custom headers/DRM)
  implementation "com.squareup.okhttp3:okhttp:4.12.0"

  // Guava (ListenableFuture for MediaSession callbacks)
  implementation "com.google.guava:guava:33.4.0-android"

  // JVM unit tests. ExoPlayerDecoderAdapter is a plain class implementing the
  // DecoderPort interface — no Nitro HybridObject base, so no JNI init — which
  // is why it is testable on the host JVM where the engine is not. Robolectric
  // supplies the Context/Looper and media3's test-utils supply a FakeMediaSource
  // so nothing has to decode real media. See UNIT_TESTING.md.
  testImplementation "junit:junit:4.13.2"
  testImplementation "org.robolectric:robolectric:4.14.1"
  testImplementation "androidx.test:core:1.6.1"
  testImplementation "androidx.media3:media3-test-utils:$media3Version"
  testImplementation "androidx.media3:media3-test-utils-robolectric:$media3Version"
}
