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['KeyboardController_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"
  }
}

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

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

def isNewArchitectureEnabled() {
  return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}

def isEdgeToEdgeEnabled() {
  return project.hasProperty("edgeToEdgeEnabled") && project.edgeToEdgeEnabled == "true"
}

def reactNativeArchitectures() {
  def value = project.getProperties().get("reactNativeArchitectures")
  return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply from: "$projectDir/react-native-helpers.gradle"

if (isNewArchitectureEnabled()) {
  apply plugin: 'com.facebook.react'
}

android {
  def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION
  if (agpVersion.tokenize('.')[0].toInteger() >= 7) {
    namespace "com.reactnativekeyboardcontroller"
    buildFeatures {
      buildConfig true
    }
  }

  compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')
  defaultConfig {
    minSdkVersion getExtOrIntegerDefault('minSdkVersion')
    targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
    versionCode 1
    versionName "1.0"
    buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
    buildConfigField "boolean", "IS_EDGE_TO_EDGE_ENABLED", isEdgeToEdgeEnabled().toString()
    ndk {
      abiFilters (*reactNativeArchitectures())
    }
  }

  sourceSets {
    main {
      if (isNewArchitectureEnabled()) {
        java.srcDirs += [
          'src/fabric',
          'build/generated/source/codegen/java'
        ]
      } else {
        java.srcDirs += ['src/paper']
      }

      if (project.ext.shouldUseBaseReactPackage()) {
        java.srcDirs += ['src/base']
      } else {
        java.srcDirs += ['src/turbo']
      }
    }
  }

  buildTypes {
    release {
      minifyEnabled false
    }
  }
  lintOptions {
    warningsAsErrors true
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

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')
def reactNativeVersion = getExtOrDefault('reactNativeVersion')

dependencies {
  if (project.ext.shouldConsumeReactNativeFromMavenCentral()) {
    // noinspection GradleDynamicVersion
    implementation "com.facebook.react:react-android:$reactNativeVersion"
  } else {
    // noinspection GradleDynamicVersion
    implementation "com.facebook.react:react-native:$reactNativeVersion"
  }
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
  implementation 'androidx.core:core-ktx:1.5.0-beta03'
  implementation 'androidx.dynamicanimation:dynamicanimation-ktx:1.0.0-alpha03'

  // dependencies for testing
  testImplementation 'junit:junit:4.13.2'
  testImplementation 'org.robolectric:robolectric:4.11.1'
  testImplementation 'androidx.test:core:1.5.0'
  testImplementation "androidx.test:core-ktx:1.5.0"
}
