buildscript {
  def kotlin_version = '1.6.10'

  repositories {
    google()
    mavenCentral()
    jcenter()
    maven { url "https://jitpack.io" }
  }

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

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

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

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

android {
  compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')
  defaultConfig {
    minSdkVersion 21
    targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')

  }

  buildTypes {
    release {
      minifyEnabled false
    }
  }
  lintOptions {
    disable 'GradleCompatible'
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

ext {
    androidSdk = [
            compileVersion: 31,
            targetVersion : 31,
            minVersion    : 21,
    ]
    versions = [
            androidx_core     : "1.7.0",
            androidx_lifecycle: "2.4.0",
            preference_version: "1.2.0",
            autoService       : '1.0.1',
            dagger            : "2.27",
            groupie           : "2.9.0",
            junit             : "4.13.2",
            junitJupiter      : "5.5.0",
            coroutines        : "1.6.0",
            lint              : "30.0.1",
            protobuf          : "3.15.1",
    ]
    generated = [
            protoSrc: "$projectDir/protocol",
    ]
    deps = [
            auto              : [
                    'service'           : "com.google.auto.service:auto-service:${versions.autoService}",
                    'serviceAnnotations': "com.google.auto.service:auto-service-annotations:${versions.autoService}",
            ],
            coroutines        : [
                    "lib" : "org.jetbrains.kotlinx:kotlinx-coroutines-android:${versions.coroutines}",
                    "test": "org.jetbrains.kotlinx:kotlinx-coroutines-test: ${versions.coroutines}",
            ],
            timber            : "com.github.ajalt:timberkt:1.5.1",
            // lint
            lint              : "com.android.tools.lint:lint:${versions.lint}",
            lintApi           : "com.android.tools.lint:lint-api:${versions.lint}",
            lintChecks        : "com.android.tools.lint:lint-checks:${versions.lint}",
            lintTests         : "com.android.tools.lint:lint-tests:${versions.lint}",

            // tests
            junit             : "junit:junit:${versions.junit}",
            junitJupiterApi   : "org.junit.jupiter:junit-jupiter-api:${versions.junitJupiter}",
            junitJupiterEngine: "org.junit.jupiter:junit-jupiter-engine:${versions.junitJupiter}",
    ]
    annotations = [
    ]
}


repositories {
  mavenCentral()
  jcenter()
  google()
  maven { url "https://jitpack.io" }

  def found = false
  def defaultDir = null
  def androidSourcesName = 'React Native sources'

  if (rootProject.ext.has('reactNativeAndroidRoot')) {
    defaultDir = rootProject.ext.get('reactNativeAndroidRoot')
  } else {
    defaultDir = new File(
            projectDir,
            '/../../../node_modules/react-native/android'
    )
  }

  if (defaultDir.exists()) {
    maven {
      url defaultDir.toString()
      name androidSourcesName
    }

    logger.info(":${project.name}:reactNativeAndroidRoot ${defaultDir.canonicalPath}")
    found = true
  } else {
    def parentDir = rootProject.projectDir

    1.upto(5, {
      if (found) return true
      parentDir = parentDir.parentFile

      def androidSourcesDir = new File(
              parentDir,
              'node_modules/react-native'
      )

      def androidPrebuiltBinaryDir = new File(
              parentDir,
              'node_modules/react-native/android'
      )

      if (androidPrebuiltBinaryDir.exists()) {
        maven {
          url androidPrebuiltBinaryDir.toString()
          name androidSourcesName
        }

        logger.info(":${project.name}:reactNativeAndroidRoot ${androidPrebuiltBinaryDir.canonicalPath}")
        found = true
      } else if (androidSourcesDir.exists()) {
        maven {
          url androidSourcesDir.toString()
          name androidSourcesName
        }

        logger.info(":${project.name}:reactNativeAndroidRoot ${androidSourcesDir.canonicalPath}")
        found = true
      }
    })
  }

  if (!found) {
    throw new GradleException(
            "${project.name}: unable to locate React Native android sources. " +
                    "Ensure you have you installed React Native as a dependency in your project and try again."
    )
  }
}

def kotlin_version = getExtOrDefault('kotlinVersion')

dependencies {
  // noinspection GradleDynamicVersion
  api 'com.facebook.react:react-native:+'
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
  implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:${versions.coroutines}"
  implementation 'com.github.membraneframework:membrane-webrtc-android:3.2.0'
}
