// Copyright (C) 2024 Acoustic, L.P. All rights reserved.
//
// NOTICE: This file contains material that is confidential and proprietary to
// Acoustic, L.P. and/or other developers. No license is granted under any intellectual or
// industrial property rights of Acoustic, L.P. except as may be provided in an agreement with
// Acoustic, L.P. Any unauthorized copying or distribution of content from this file is
// prohibited.
//
//
//  Created by Omar Hernandez on 5/9/25.
//

buildscript {
  repositories {
    google()
    mavenCentral()
  }

  dependencies {
    classpath "com.android.tools.build:gradle:8.10.0"
  }
}

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

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

apply plugin: "com.android.library"
apply plugin: 'org.jetbrains.kotlin.android'
apply from: '../nitrogen/generated/android/AcousticConnectRN+autolinking.gradle'

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

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

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

android {
  namespace = "com.acousticconnectrn"

  ndkVersion = getExtOrDefault("ndkVersion")
  compileSdkVersion = getExtOrIntegerDefault("compileSdkVersion")

  defaultConfig {
    minSdkVersion getExtOrIntegerDefault("minSdkVersion")
    targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
    buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()

    externalNativeBuild {
      cmake {
        cppFlags "-frtti -fexceptions -Wall -Wextra -fstack-protector-all"
        arguments "-DANDROID_STL=c++_shared"
        abiFilters (*reactNativeArchitectures())

        buildTypes {
          debug {
            cppFlags "-O1 -g"
          }
          release {
            cppFlags "-O2"
          }
        }
      }
    }
  }

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

  packagingOptions {
    excludes = [
        "META-INF",
        "META-INF/**",
        "**/libc++_shared.so",
        "**/libfbjni.so",
        "**/libjsi.so",
        "**/libfolly_json.so",
        "**/libfolly_runtime.so",
        "**/libglog.so",
        "**/libhermes.so",
        "**/libhermes-executor-debug.so",
        "**/libhermes_executor.so",
        "**/libreactnative.so",
        "**/libreactnativejni.so",
        "**/libturbomodulejsijni.so",
        "**/libreact_nativemodule_core.so",
        "**/libjscexecutor.so"
    ]
  }

  buildFeatures {
    buildConfig = true
    prefab = true
  }

  buildTypes {
    release {
      minifyEnabled false
    }
  }

  lintOptions {
    disable "GradleCompatible"
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

  sourceSets {
    main {
      if (isNewArchitectureEnabled()) {
        java.srcDirs += [
          // React Codegen files
          "${project.buildDir}/generated/source/codegen/java"
        ]
      }
    }
  }
}

repositories {
  mavenCentral()
  google()
}

allprojects {
    repositories {
        google()
        mavenCentral()
        maven { url="https://s01.oss.sonatype.org/content/groups/staging" }
        
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url="$rootDir/../node_modules/react-native/android"
        }
    }
}

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:+"

  // Add a dependency on NitroModules
  implementation project(":react-native-nitro-modules")

  // Connect SDK libraries
  implementation 'io.github.go-acoustic:connect:10.4.29'
  // implementation 'io.github.go-acoustic:connect:+'
  // Connect SDK libraries
  // api 'io.github.go-acoustic:connect:+'
  // api 'io.github.go-acoustic:connect:10.4.29'
}

if (isNewArchitectureEnabled()) {
  react {
    jsRootDir = file("../src/")
    libraryName = "AcousticConnectRN"
    codegenJavaPackageName = "com.acousticconnectrn"
  }
}
