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["RNPrinter_kotlinVersion"]

  repositories {
    google()
    mavenCentral()
  }

  dependencies {
    classpath "com.android.tools.build:gradle:7.2.2"
    // noinspection DifferentKotlinGradleVersion
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.20'
    classpath 'org.jetbrains.kotlin:kotlin-serialization:1.9.0'
  }
}

plugins {
  id 'org.jetbrains.kotlin.plugin.serialization' version '1.9.0' apply true
}

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

apply plugin: "com.android.library"
apply plugin: "kotlin-android"
apply plugin: 'kotlinx-serialization'

def appProject = rootProject.allprojects.find { it.plugins.hasPlugin('com.android.application') }

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

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

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

def isAGPVersionGreaterThan(version) {
  def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')[0].toInteger()
  return agpVersion > version
}

android {
  if (isAGPVersionGreaterThan(7)) {
    namespace "com.deckyfx.reactnative.printer"
  }

  compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")

  defaultConfig {
    minSdk getExtOrIntegerDefault("minSdkVersion")
    targetSdk getExtOrIntegerDefault("targetSdkVersion")
    buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
    externalNativeBuild {
      cmake {
        cppFlags ''
      }
      ndkBuild {
        cppFlags ''
      }
    }
    ndk {
      //noinspection ChromeOsAbiSupport
      abiFilters 'arm64-v8a', 'armeabi-v7a'
    }
  }

  buildTypes {
    release {
      minifyEnabled false
    }
  }

  lint {
    disable "GradleCompatible"
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
  externalNativeBuild {
    ndkBuild {
      // path file('src/main/cpp/Android.mk')
      // path file('src/main/cpp/Application.mk')
    }
    cmake {
      path "src/main/cpp/CMakeLists.txt"
    }
  }
}

repositories {
  mavenCentral()
  google()
  gradlePluginPortal() // Added this line to fix cannot resolve react-native module dependency
}

def kotlin_version = getExtOrDefault("kotlinVersion")

dependencies {
  // For < 0.71, this will be from the local maven repo
  // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
  //noinspection GradleDynamicVersion
  implementation 'com.facebook.react:react-native:+'
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
  implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1'
  implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.2'
  implementation 'org.apache.commons:commons-lang3:3.12.0'
  implementation 'androidx.work:work-runtime-ktx:2.8.1'
  implementation 'com.google.zxing:core:3.5.1'
}

if (isNewArchitectureEnabled()) {
  react {
    jsRootDir = file("../src/")
    libraryName = "RNPrinter"
    codegenJavaPackageName = "com.deckyfx.reactnative.printer"
  }
}
