buildscript {
  // Buildscript is evaluated before everything else so we can't use getExtOrDefault
  def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["TrackPlayer_kotlinVersion"]

  repositories {
    google()
    mavenCentral()
  }

  dependencies {
    classpath "com.android.tools.build:gradle:7.2.1"
    // noinspection DifferentKotlinGradleVersion
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
  }
}

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 plugin: "kotlinx-serialization"
apply plugin: "com.facebook.react"

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

def getExtOrIntegerDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["TrackPlayer_" + name]).toInteger()
}

def supportsNamespace() {
  def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
  def major = parsed[0].toInteger()
  def minor = parsed[1].toInteger()

  // Namespace support was added in 7.3.0
  return (major == 7 && minor >= 3) || major >= 8
}

android {
  if (supportsNamespace()) {
    namespace "com.doublesymmetry.trackplayer"

    sourceSets {
      main {
        manifest.srcFile "src/main/AndroidManifestNew.xml"
      }
    }
  }

  compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")

  defaultConfig {
    minSdkVersion getExtOrIntegerDefault("minSdkVersion")
    targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
  }

  buildFeatures {
    buildConfig true
  }

  buildTypes {
    release {
      minifyEnabled false
    }
  }

  lintOptions {
    disable "GradleCompatible"
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

  sourceSets {
    main {
      java.srcDirs += [
        "src/newarch",
        "${project.buildDir}/generated/source/codegen/java"
      ]
    }
  }
}

repositories {
  mavenCentral()
  google()
}

def kotlin_version = getExtOrDefault("kotlinVersion")

dependencies {
  //noinspection GradleDynamicVersion
  implementation "com.facebook.react:react-native:+"
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
  implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")

  implementation "androidx.media3:media3-exoplayer:1.9.2"
  implementation "androidx.media3:media3-exoplayer-hls:1.9.2"
  implementation "androidx.media3:media3-common:1.9.2"
  implementation "androidx.media3:media3-session:1.9.2"
  implementation "androidx.media3:media3-cast:1.9.2"
  implementation "androidx.mediarouter:mediarouter:1.7.0"

  // Testing
  testImplementation "junit:junit:4.13.2"
  testImplementation "org.robolectric:robolectric:4.11.1"
  testImplementation "io.mockk:mockk:1.13.8"
  testImplementation "androidx.test:core:1.5.0"
  testImplementation "androidx.media3:media3-test-utils:1.9.2"
}

react {
  jsRootDir = file("../src/")
  libraryName = "TrackPlayer"
  codegenJavaPackageName = "com.doublesymmetry.trackplayer"
}
