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['Readium_kotlinVersion']
  ext.readium_version = '2.4.1'

  repositories {
    google()
    mavenCentral()
  }

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

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

android {
  compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')
  defaultConfig {
    minSdkVersion 21
    targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
    versionCode 1
    versionName "1.0"
  }

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

  buildFeatures {
    viewBinding true
  }
}

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 {
  // noinspection GradleDynamicVersion
  api "com.facebook.react:react-native:+"
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

  // readium deps
  implementation "org.readium.kotlin-toolkit:readium-shared:$readium_version"
  implementation "org.readium.kotlin-toolkit:readium-streamer:$readium_version"
  implementation ("org.readium.kotlin-toolkit:readium-navigator:$readium_version") {
    // this package doesn't provide audio playback, so remove exoplayer
    // transitive deps to avoid potential collisions with other projects.
    exclude group: "com.google.android.exoplayer"
  }

  // other deps
  implementation "androidx.core:core-ktx:1.7.0"
  implementation "androidx.activity:activity-ktx:1.4.0"
  implementation "androidx.appcompat:appcompat:1.4.1"
  implementation "androidx.browser:browser:1.4.0"
  implementation "androidx.cardview:cardview:1.0.0"
  implementation "androidx.constraintlayout:constraintlayout:2.1.3"
  implementation "androidx.fragment:fragment-ktx:1.4.0"
  implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.4.0"
  implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.0"
  implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0"
  implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
  implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
  implementation "androidx.paging:paging-runtime-ktx:3.1.0"
  implementation "androidx.recyclerview:recyclerview:1.2.1"
  implementation "androidx.viewpager2:viewpager2:1.0.0"
  implementation "androidx.webkit:webkit:1.4.0"
  implementation "com.google.android.material:material:1.5.0"
  implementation "com.jakewharton.timber:timber:5.0.1"
  // AM NOTE: needs to stay this version for now (June 24,2020)
  //noinspection GradleDependency
  implementation "com.squareup.picasso:picasso:2.71828"
  implementation "joda-time:joda-time:2.10.13"
  implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0"
  // AM NOTE: needs to stay this version for now (June 24,2020)
  //noinspection GradleDependency
  implementation "org.jsoup:jsoup:1.14.3"

  // Room database
  final roomVersion = "2.4.1"
  implementation "androidx.room:room-runtime:$roomVersion"
  implementation "androidx.room:room-ktx:$roomVersion"
  annotationProcessor "androidx.room:room-compiler:$roomVersion"

  implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
  //noinspection LifecycleAnnotationProcessorWithJava8
  annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.4.0"

  testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
  testImplementation "junit:junit:4.13.2"

  androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0"
  androidTestImplementation "androidx.test:runner:1.4.0"
}
