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['NotificarePushUIModule_kotlinVersion']

  repositories {
    google()
    mavenCentral()
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:7.4.2'
    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['NotificarePushUIModule_' + name]
}

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

android {
  if (project.android.hasProperty("namespace")) {
    namespace 're.notifica.push.ui.react_native'
  }

  compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')
  defaultConfig {
    minSdkVersion 23
    targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
  }

  buildTypes {
    release {
      minifyEnabled false
    }
  }
  lint {
    disable 'GradleCompatible'
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
  kotlinOptions {
    freeCompilerArgs += [
      '-Xexplicit-api=strict',
      '-Xopt-in=re.notifica.InternalNotificareApi',
    ]
  }
}

repositories {
  google()
  mavenCentral()

  maven { url 'https://maven.notifica.re/releases' }
  maven { url 'https://maven.notifica.re/prereleases' }
  maven { url 'https://developer.huawei.com/repo' }

  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."
    )
  }
}

dependencies {
  // noinspection GradleDynamicVersion
  api 'com.facebook.react:react-native:+'

  def notificare_version = '3.9.0'
  api "re.notifica:notificare:$notificare_version"
  api "re.notifica:notificare-push-ui:$notificare_version"
  implementation "re.notifica:notificare-push-ui-gms:$notificare_version"
  implementation "re.notifica:notificare-push-ui-hms:$notificare_version"
}
