import java.nio.file.Paths

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

  repositories {
    google()
    mavenCentral()
  }

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

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

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

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

rootProject.ext {
  fwNativeVersion = getExtOrDefault("fwNativeVersion")
}

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.fireworksdk.bridge"

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

  compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")

  defaultConfig {
    minSdkVersion getExtOrIntegerDefault("minSdkVersion")
    targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
    manifestPlaceholders["fw_player_launch_mode"] = getExtOrDefault('fwPlayerLaunchMode')
  }

  buildTypes {
    debug {
      buildConfigField 'int', 'LOG_LEVEL', '7'
      consumerProguardFiles 'proguard-rules.pro', "devsupport.pro"
    }
    release {
      buildConfigField 'int', 'LOG_LEVEL', '7'
      minifyEnabled false
      consumerProguardFiles 'proguard-rules.pro'
    }
  }

  lintOptions {
    disable "GradleCompatible"
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

  buildFeatures {
    viewBinding = true
    buildConfig = true
  }

  lintOptions {
    disable 'GradleCompatible'
  }
}

repositories {
  mavenLocal()
  mavenCentral()
  google()

  maven {
    setUrl("https://central.sonatype.com/repository/maven-snapshots/")
    mavenContent {
      snapshotsOnly()
    }
  }
}

dependencies {
  // implementation "com.facebook.react:react-android"
  // api "com.facebook.react:react-native:+"
  implementation "com.facebook.react:react-android:0.78.2"

  def kotlin_version = getExtOrDefault("kotlinVersion")
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

  def fireworkSdkVersion = getExtOrDefault('fwNativeVersion')
  implementation "com.firework:sdk:$fireworkSdkVersion"
  implementation "com.firework.feature:uiKit:$fireworkSdkVersion"
  implementation "com.firework.feature.player:pager:$fireworkSdkVersion"

  testImplementation "junit:junit:4.13.2"
  // Real org.json implementation for JVM unit tests (android.jar only ships stubs)
  testImplementation "org.json:json:20230618"
}
