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

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

    repositories {
        mavenCentral()
    }

    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")
    }
}

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

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

def isNewArchitectureEnabled() {
    return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
}

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
  if (major == 7 && minor >= 3) {
    return true
  }

  return major >= 8
}

def useExoplayerIMA = safeExtGet("RNVUseExoplayerIMA", false)

println "useExoplayerIMA:" + useExoplayerIMA

// This string is used to define build path.
// As react native build output directory is react-native path of the module.
// We need to force a new path on each configuration change.
// If you add a new build parameter, please add the new value in this string
def configStringPath = (
        'useExoplayerIMA' + useExoplayerIMA \
).md5()

if (isNewArchitectureEnabled()) {
    apply plugin: "com.facebook.react"
}

android {
    if (supportsNamespace()) {
      namespace 'com.brentvatne.react'

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

    compileSdkVersion safeExtGet('compileSdkVersion', 31)
    buildToolsVersion safeExtGet('buildToolsVersion', '30.0.2')

    def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION
    if (agpVersion.tokenize('.')[0].toInteger() < 8) {
      compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
      }

      kotlinOptions {
        jvmTarget = JavaVersion.VERSION_11.majorVersion
      }
    }

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

        ndk {
            abiFilters(*reactNativeArchitectures())
        }
    }

    buildFeatures {
        buildConfig true
    }

    packagingOptions {
        exclude "**/libreact_render*.so"
    }

    buildDir 'buildOutput_' + configStringPath

    sourceSets {
        main {
            java {
                if (useExoplayerIMA) {
                    exclude 'com/google/ads/interactivemedia/v3/api'
                    exclude 'com/google/android/exoplayer2/ext/ima'
                }
            }
        }
    }

    sourceSets.main {
        java {
            if (isNewArchitectureEnabled()) {
                srcDirs += [
                    "src/fabric/java",
                    "${project.buildDir}/generated/source/codegen/java"
                ]
            } else {
                srcDirs += [
                    "src/oldarch/java"
                ]
            }
        }
    }
}

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

repositories {
    google()
    maven {
        // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
        url "$rootDir/../node_modules/react-native/android"
    }
    mavenCentral()
}

def kotlin_version = getExtOrDefault('kotlinVersion', project.properties['RNSAC_kotlinVersion'])

dependencies {
    implementation "com.facebook.react:react-native:${safeExtGet('reactNativeVersion', '+')}"
    implementation('com.google.android.exoplayer:exoplayer:2.18.1') {
        exclude group: 'com.android.support'
    }

    // All support libs must use the same version
    implementation "androidx.annotation:annotation:1.1.0"
    implementation "androidx.core:core:1.1.0"
    implementation "androidx.media:media:1.1.0"
    implementation "androidx.activity:activity:1.4.0"

    implementation('com.google.android.exoplayer:extension-okhttp:2.18.1') {
        exclude group: 'com.squareup.okhttp3', module: 'okhttp'
    }

    if (useExoplayerIMA) {
        implementation 'com.google.android.exoplayer:extension-ima:2.18.1'
    }
    implementation "com.squareup.okhttp3:okhttp:" + '$OKHTTP_VERSION'
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}