import java.nio.file.Paths
import com.android.Version

def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION
def agpVersionMajor = agpVersion.tokenize('.')[0].toInteger()
def agpVersionMinor = agpVersion.tokenize('.')[1].toInteger()

buildscript {
  repositories {
    google()
    mavenCentral()
  }

  dependencies {
    classpath("com.android.tools.build:gradle:8.7.3")
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}"
  }
}

def isNewArchitectureEnabled() {
  // To opt-in for the New Architecture, you can either:
  // - Set `newArchEnabled` to true inside the `gradle.properties` file
  // - Invoke gradle with `-newArchEnabled=true`
  // - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
  return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}

def resolveBuildType() {
    Gradle gradle = getGradle()
    String tskReqStr = gradle.getStartParameter().getTaskRequests()['args'].toString()

    return tskReqStr.contains('Release') ? 'release' : 'debug'
}

apply plugin: 'com.android.library'
apply plugin: "org.jetbrains.kotlin.android"

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

def safeExtGet(prop, fallback) {
  rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

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

static def findNodeModules(baseDir) {
  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 nodeModulesPath = Paths.get(basePath.toString(), "node_modules")
    def reactNativePath = Paths.get(nodeModulesPath.toString(), "react-native")
    if (nodeModulesPath.toFile().exists() && reactNativePath.toFile().exists()) {
      return nodeModulesPath.toString()
    }
    basePath = basePath.getParent()
  }
  throw new GradleException("react-native-quick-crypto: Failed to find node_modules/ path!")
}

def nodeModules = findNodeModules(projectDir)

repositories {
  google()
  mavenCentral()
}

android {
  compileSdkVersion safeExtGet("compileSdkVersion", 31)

  if ((agpVersionMajor == 7 && agpVersionMinor >= 3) || agpVersionMajor >= 8) {
    // Namespace support was added in 7.3.0
    namespace "com.margelo.quickcrypto"

    sourceSets {
      main {
        manifest.srcFile "src/main/AndroidManifestNew.xml"
      }
    }
  }

  if (agpVersionMajor >= 8) {
      buildFeatures {
          buildConfig = true
      }
  }

  // Used to override the NDK path/version on internal CI or by allowing
  // users to customize the NDK path/version from their root project (e.g. for M1 support)
  if (rootProject.hasProperty("ndkPath")) {
    ndkPath rootProject.ext.ndkPath
  }
  if (rootProject.hasProperty("ndkVersion")) {
    ndkVersion rootProject.ext.ndkVersion
  }

  buildFeatures {
    prefab true
  }

  defaultConfig {
    minSdkVersion safeExtGet('minSdkVersion', 23)
    targetSdkVersion safeExtGet('targetSdkVersion', 31)
    versionCode 1
    versionName "1.0"
    buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
    externalNativeBuild {
      cmake {
        cppFlags "-O2 -frtti -fexceptions -Wall -Wno-unused-variable -fstack-protector-all -DANDROID"
        arguments "-DANDROID_STL=c++_shared"
        abiFilters (*reactNativeArchitectures())
      }
    }
  }

  externalNativeBuild {
    cmake {
      path "CMakeLists.txt"
    }
  }

  packagingOptions {
    doNotStrip resolveBuildType() == 'debug' ? "**/**/*.so" : ''
    excludes = [
            "META-INF",
            "META-INF/**",
            "**/libc++_shared.so",
            "**/libfbjni.so",
            "**/libreactnativejni.so",
            "**/libjsi.so",
            "**/libreact_nativemodule_core.so",
            "**/libturbomodulejsijni.so",
            "**/MANIFEST.MF",
            "**/libreactnative.so",
    ]
  }

  buildTypes {
    release {
      minifyEnabled false
    }
    debug {
      packagingOptions {
        doNotStrip '**/*.so'
      }
      minifyEnabled false
      debuggable true
      jniDebuggable true
      renderscriptDebuggable true
    }
  }

  lintOptions {
    disable 'GradleCompatible'
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

}

repositories {
  mavenCentral()
  google()
}

dependencies {
  //noinspection GradleDynamicVersion
  implementation "com.facebook.react:react-android:+"

  // Add a dependency on OpenSSL
  implementation 'io.github.ronickg:openssl:3.3.2'
}

// Resolves "LOCAL_SRC_FILES points to a missing file, Check that libfb.so exists or that its path is correct".
tasks.whenTaskAdded { task ->
  if (task.name.contains("configureCMakeDebug")) {
    rootProject.getTasksByName("packageReactNdkDebugLibs", true).forEach {
      task.dependsOn(it)
    }
  }
  // We want to add a dependency for both configureCMakeRelease and configureCMakeRelWithDebInfo
  if (task.name.contains("configureCMakeRel")) {
    rootProject.getTasksByName("packageReactNdkReleaseLibs", true).forEach {
      task.dependsOn(it)
    }
  }
}
