import java.nio.file.Paths
import org.gradle.internal.jvm.Jvm


buildscript {
  // The Android Gradle plugin is only required when opening the android folder stand-alone.
  // This avoids unnecessary downloads and potential conflicts when the library is included as a
  // module dependency in an application project.
  if (project == rootProject) {
    repositories {
      google()
      mavenCentral()
    }

    dependencies {
      classpath("com.android.tools.build:gradle:7.0.3")
    }
  }
}

plugins {
  id "io.invertase.gradle.build" version "1.5"
}

apply plugin: 'com.android.library'

// https://github.com/facebook/react-native/blob/a70354df12ef71aec08583cca4f1fed5fb77d874/ReactAndroid/build.gradle#L168-L201
def findNodeModulePath(baseDir, packageName) {
  def basePath = baseDir.toPath().normalize()
  // Node's module resolution algorithm searches up to the root directory,
  // after which the base path will be null
  while (basePath) {
    def candidatePath = Paths.get(basePath.toString(), "node_modules", packageName)
    if (candidatePath.toFile().exists()) {
      return candidatePath.toString()
    }
    basePath = basePath.getParent()
  }
  return null
}

def notifeeDir = findNodeModulePath(projectDir, "@ammarahm-ed/notifee-react-native") ?: "$rootDir/../node_modules/@ammarahm-ed/notifee-react-native"
println ":${project.name} @notifee/react-native found at $notifeeDir"

if (project.hasProperty('reactNativeProjects')) {
  reactNativeProjects.each { dependent ->
    project.evaluationDependsOn(dependent)
  }
} else {
  project.evaluationDependsOn(':app')
}

project.ext {
  set('react-native', [
    versions: [
      android: [
        minSdk    : 20,
        targetSdk : 33,
        compileSdk: 33,
        // optional as gradle.buildTools comes with one by default
        // overriding here though to match the version RN uses
        // buildTools: "29.0.2"
      ],
    ],
  ])
}

android {
  defaultConfig {
    multiDexEnabled true
    javaCompileOptions {
      annotationProcessorOptions {
        arguments = ["room.schemaLocation":
                             "$projectDir/schemas".toString(),
                             eventBusIndex: 'app.notifee.core.EventBusIndex']
      }
    }
  }

  lintOptions {
    disable 'GradleCompatible'
    abortOnError false
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
  buildTypes {
    release {
      consumerProguardFiles 'proguard-rules.pro'
    }
  }
  compileSdkVersion = 33
  buildToolsVersion = '33.0.0'
}

repositories {
  google()
  mavenCentral()
}

dependencies {
  // needed to enforce version and avoid clashes
  // https://github.com/square/okhttp/blob/master/docs/changelog_3x.md
  implementation 'com.squareup.okhttp3:okhttp:3.12.13' // okhttp must stay on 3.12.x to support minSdkVersion < 21

  implementation 'androidx.annotation:annotation:1.3.0' // https://developer.android.com/jetpack/androidx/releases/annotation
  implementation "com.squareup.okhttp3:okhttp:3.12.12" // okhttp must stay on 3.12.x to support minSdkVersion < 21
  implementation 'androidx.concurrent:concurrent-futures:1.1.0' // https://developer.android.com/jetpack/androidx/releases/concurrent
  implementation 'androidx.work:work-runtime:2.7.1' // https://developer.android.com/jetpack/androidx/releases/work
  implementation 'com.facebook.fresco:fresco:2.6.0' // https://github.com/facebook/fresco/releases

  implementation("com.google.guava:guava:31.1-android") // https://github.com/google/guava
  implementation 'androidx.core:core:1.6.0'

  def room_version = '2.4.3' // https://developer.android.com/jetpack/androidx/releases/room
  implementation "androidx.room:room-runtime:$room_version"
  annotationProcessor "androidx.room:room-compiler:$room_version"

  def eventbus_version = '3.2.0' // https://github.com/greenrobot/EventBus/releases
  api "org.greenrobot:eventbus:$eventbus_version"
  annotationProcessor "org.greenrobot:eventbus-annotation-processor:$eventbus_version"

  testImplementation 'junit:junit:4.13.2' // https://github.com/junit-team/junit4/releases
  androidTestImplementation 'androidx.test.ext:junit:1.1.3' // https://developer.android.com/jetpack/androidx/releases/test
  androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' // see above
  androidTestImplementation "androidx.room:room-testing:$room_version"
  androidTestImplementation "androidx.test:monitor:1.4.0"
}

ReactNative.shared.applyPackageVersion()
ReactNative.shared.applyDefaultExcludes()
ReactNative.module.applyAndroidVersions()
ReactNative.module.applyReactNativeDependency("api")
