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['OnespanBridgeAndroid_kotlinVersion']

  repositories {
    google()
    mavenCentral()
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:4.2.2'
    // noinspection DifferentKotlinGradleVersion
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath 'com.google.gms:google-services:4.3.13'
  }
}

def isNewArchitectureEnabled() {
  return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-parcelize'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.google.gms.google-services'

if (isNewArchitectureEnabled()) {
  apply plugin: 'com.facebook.react'
}

def getExtOrDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['OnespanBridgeAndroid_' + name]
}

def getExtOrIntegerDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties['OnespanBridgeAndroid_' + name]).toInteger()
}

android {
  compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')

  defaultConfig {
    multiDexEnabled true
    minSdkVersion getExtOrIntegerDefault('minSdkVersion')
    targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
    buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
  }

  buildTypes {
    release {
      minifyEnabled false
    }
  }

  lintOptions {
    disable 'GradleCompatible'
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

  /*packagingOptions {
    resources {
      excludes += ['META-INF/lib_release.kotlin_module']
    }
  }*/
}

repositories {

  mavenCentral()
  google()

  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 {
  // necessaries dependencies
  implementation 'androidx.core:core-ktx:1.8.0'
  implementation 'androidx.appcompat:appcompat:1.4.2'
  implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.0'
  implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.0'
  implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.0'
  implementation 'androidx.biometric:biometric:1.1.0'
  implementation "org.jetbrains.kotlin:kotlin-stdlib:1.7.10"
  implementation 'com.google.android.gms:play-services-location:20.0.0'
  implementation "com.android.support:support-core-utils:28.0.0"

  implementation platform('com.google.firebase:firebase-bom:30.2.0')
  implementation 'com.google.firebase:firebase-analytics-ktx'
  implementation 'com.google.firebase:firebase-messaging'

  // koin
  implementation 'io.insert-koin:koin-android:3.2.0'

  //retrofit - gson - interceptor
  implementation 'com.squareup.retrofit2:retrofit:2.9.0'
  implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
  implementation 'com.squareup.okhttp3:logging-interceptor:4.10.0'

  //Local LIBs
  implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
  implementation files('sdk_orchestration_android:5.5.1.881')
  implementation files('sdk_digipass_android:4.30.0.614')
  implementation files('sdk_utilities_android:4.27.2.881')
  implementation files('sdk_cddc_android:4.25.2.654')
  implementation files('sdk_biometric_sensor_android:4.25.1.656')
  implementation files('sdk_notification_client_android:4.29.2.728')
  implementation files('sdk_dsapp_client_android:4.28.0.536')
  implementation files('sdk_device_binding_android:5.0.2.800')
  implementation files('sdk_secure_storage_android:4.28.0.862')

  implementation 'com.esotericsoftware:kryo:5.3.0'
  implementation 'org.bouncycastle:bcprov-ext-jdk15to18:1.70'

  //noinspection GradleDynamicVersion
  implementation "com.facebook.react:react-native:+"
  implementation 'com.google.android.material:material:1.6.1'
  implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
  // From node_modules

}

if (isNewArchitectureEnabled()) {
  react {
    jsRootDir = file("../src/")
    libraryName = "OnespanBridgeAndroid"
    codegenJavaPackageName = "com.reactnativeonespanbridgeandroid"
  }
}
