buildscript {

  repositories {
    google()
    mavenCentral()
  }

  dependencies {
    classpath "com.android.tools.build:gradle:7.2.1"
  }
}

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

// Version configuration through environment variables or gradle properties
def ttsdk_ttlivepull_rtc_version
def envVersion = System.getenv('TTSDK_TTLIVEPULL_RTC_VERSION_FOR_LIVE_PULL')
if (envVersion) {
  ttsdk_ttlivepull_rtc_version = envVersion
} else if (rootProject.hasProperty('TTSDK_TTLIVEPULL_RTC_VERSION_FOR_LIVE_PULL')) {
  ttsdk_ttlivepull_rtc_version = rootProject.property('TTSDK_TTLIVEPULL_RTC_VERSION_FOR_LIVE_PULL')
} else {
  // Fallback to versions from package.json
  def packageJson = file("../package.json")
  def slurper = new groovy.json.JsonSlurper()
  def parsedJson = slurper.parseText(packageJson.text)
  def isBp = parsedJson.name.startsWith("@byteplus")
  
  if (isBp) {
    ttsdk_ttlivepull_rtc_version = parsedJson.nativeVersion.byteplus.android
  } else {
    ttsdk_ttlivepull_rtc_version = parsedJson.nativeVersion.volc.android
  }
}

// VolcApiEngine version configuration
def volc_api_engine_version
def volcApiEnvVersion = System.getenv('VOLC_API_ENGINE_VERSION_FOR_LIVE_PULL')
if (volcApiEnvVersion) {
  volc_api_engine_version = volcApiEnvVersion
} else if (rootProject.hasProperty('VOLC_API_ENGINE_VERSION_FOR_LIVE_PULL')) {
  volc_api_engine_version = rootProject.property('VOLC_API_ENGINE_VERSION_FOR_LIVE_PULL')
} else {
  volc_api_engine_version = "1.6.6"  // Default version
}

println "[LIVE PULL GRADLE INFO]: Using ttsdk_ttlivepull_rtc version: $ttsdk_ttlivepull_rtc_version"
println "[LIVE PULL GRADLE INFO]: Using volc_api_engine version: $volc_api_engine_version"

apply plugin: "com.android.library"
apply from: "https://ve-vos.volccdn.com/script/vevos-repo-base.gradle"

if (isNewArchitectureEnabled()) {
  println "[LIVE PULL GRADLE INFO]: newArchitectureEnabled"
  apply plugin: "com.facebook.react"
}

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

def getExtOrIntegerDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["LivePull_" + 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.volcengine.velive.rn.pull"

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

  compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")


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

    consumerProguardFiles 'proguard-rules.pro'
    //注入配置，运行时判断
    buildConfigField("boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString())
  }
  
  sourceSets {
      main {
          if (isNewArchitectureEnabled()) {
              java.srcDirs += ['src/newarch']
          } else {
              java.srcDirs += ['src/oldarch']
          }
      }
  }
    
  buildTypes {
    release {
      minifyEnabled false
    }
  }

  lintOptions {
    disable "GradleCompatible"
  }

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

repositories {
  mavenCentral()
  google()
  maven {
    url "https://artifact.bytedance.com/repository/Volcengine/" // volc public maven repo
  }
}


dependencies {
  // For < 0.71, this will be from the local maven repo
  // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
  // noinspection GradleDynamicVersion
    implementation "com.facebook.react:react-native:+"

    //TODO 改成开发runtime
    implementation "com.volcengine:VolcApiEngine:$volc_api_engine_version"
    implementation "com.bytedanceapi:ttsdk-ttlivepull_premium:$ttsdk_ttlivepull_rtc_version"
}
