import com.android.Version
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

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['RePack_kotlinVersion']

    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath("com.android.tools.build:gradle:7.3.1")
        // noinspection DifferentKotlinGradleVersion
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

static def supportsNamespace() {
    def parsed = Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
    def major = parsed[0].toInteger()
    def minor = parsed[1].toInteger()

    // Namespace support was added in 7.3.0
    return (major == 7 && minor >= 3) || major >= 8
}

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

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

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

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

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

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

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

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

def REACT_NATIVE_BUILD_FROM_SOURCE = findProject(":packages:react-native:ReactAndroid") != null
def REACT_NATIVE_DIR = REACT_NATIVE_BUILD_FROM_SOURCE
        ? findProject(":packages:react-native:ReactAndroid").getProjectDir().parent
        : file(providers.exec {
    workingDir(rootDir)
    commandLine("node", "--print", "require.resolve('react-native/package.json')")
}.standardOutput.asText.get().trim()).parent

def reactProperties = new Properties()
file("$REACT_NATIVE_DIR/ReactAndroid/gradle.properties").withInputStream { reactProperties.load(it) }

def REACT_NATIVE_VERSION = reactProperties.getProperty("VERSION_NAME")
def REACT_NATIVE_MINOR_VERSION = REACT_NATIVE_VERSION.startsWith("0.0.0-") ? 1000 : REACT_NATIVE_VERSION.split("\\.")[1].toInteger()
def IS_NEW_ARCHITECTURE_ENABLED = isNewArchitectureEnabled()

android {
    if (supportsNamespace()) {
        namespace "com.callstack.repack"

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

    compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')

    if (rootProject.hasProperty("ndkPath")) {
        ndkPath rootProject.ext.ndkPath
    }
    if (rootProject.hasProperty("ndkVersion")) {
        ndkVersion rootProject.ext.ndkVersion
    }

    buildFeatures {
        prefab true
    }

    defaultConfig {
        minSdkVersion getExtOrIntegerDefault('minSdkVersion')
        targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
        versionCode 1
        versionName "1.0"
        buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", IS_NEW_ARCHITECTURE_ENABLED.toString()
        externalNativeBuild {
            cmake {
                cppFlags "-O2 -frtti -fexceptions -Wall -Wno-unused-variable -fstack-protector-all"
                arguments "-DANDROID_STL=c++_shared",
                          "-DREACT_NATIVE_DIR=${REACT_NATIVE_DIR}",
                          "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
                abiFilters(*reactNativeArchitectures())
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
        }
    }

    lintOptions {
        disable 'GradleCompatible'
    }

    compileOptions {
        if (REACT_NATIVE_MINOR_VERSION < 69) {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    }

    kotlinOptions {
        if (REACT_NATIVE_MINOR_VERSION < 69) {
            jvmTarget = JavaVersion.VERSION_1_8
        } else if (REACT_NATIVE_MINOR_VERSION < 73) {
            jvmTarget = JavaVersion.VERSION_11
        }
    }

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

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

    // Force rebuild when changing architecture
    tasks.withType(KotlinCompile) {
        inputs.property 'newArchEnabled', IS_NEW_ARCHITECTURE_ENABLED
    }

    sourceSets.main {
        if (IS_NEW_ARCHITECTURE_ENABLED) {
            java.srcDirs += [
                    "src/newarch",
                    // This is needed to build Kotlin project with NewArch enabled
                    "${project.buildDir}/generated/source/codegen/java"
            ]
        } else {
            java.srcDirs += ["src/oldarch"]
        }

        // CallInvoker
        if (REACT_NATIVE_MINOR_VERSION <= 73) {
            java.srcDirs += "src/versioned/CallInvoker/73"
        } else {
            java.srcDirs += "src/versioned/CallInvoker/latest"
        }
    }
}

repositories {
    mavenCentral()
    google()
}

def kotlin_version = getExtOrDefault('kotlinVersion')

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    //noinspection GradleDynamicVersion
    implementation 'com.facebook.react:react-android:+'
    implementation "com.squareup.okhttp3:okhttp:4.9.2"
    implementation "com.squareup.okhttp3:okhttp-urlconnection:4.9.2"
    implementation 'com.nimbusds:nimbus-jose-jwt:10.8'
}
