int MILLIS_IN_MINUTE = 1000 * 60
int minutesSinceEpoch = System.currentTimeMillis() / MILLIS_IN_MINUTE

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

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

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

  // Namespace support was added in 7.3.0
  if (major == 7 && minor >= 3) {
    return true
  }

  return major >= 8
}

def parseMajorVersion(String versionString) {
    if (!versionString) {
        return null
    }

    def matcher = (versionString =~ /(\d+)/)
    if (matcher.find()) {
        return matcher[0][0].toInteger()
    }

    return null
}

def resolveInstalledExpoMajorVersion(File projectDir) {
    try {
        def stdout = new ByteArrayOutputStream()
        def stderr = new ByteArrayOutputStream()
        def result = project.exec {
            workingDir projectDir
            commandLine 'node', '--print', "require('expo/package.json').version"
            standardOutput = stdout
            errorOutput = stderr
            ignoreExitValue true
        }

        if (result.exitValue != 0) {
            return null
        }

        return parseMajorVersion(stdout.toString().trim())
    } catch (Exception ignored) {
        return null
    }
}

// Reads node_modules/expo/package.json the way Node would resolve it (walking
// up from the app directory, so hoisted monorepo installs work) — a file read
// instead of a `node` process at configuration time.
def readInstalledExpoMajorVersion(File startDir) {
    File dir = startDir
    while (dir != null) {
        File expoPackageJson = new File(dir, 'node_modules/expo/package.json')
        if (expoPackageJson.isFile()) {
            try {
                return parseMajorVersion(
                    new groovy.json.JsonSlurper().parseText(expoPackageJson.text).version)
            } catch (Exception ignored) {
                return null
            }
        }
        dir = dir.parentFile
    }
    return null
}

def checkProjectInfo() {
    def hasExpoModulesCore = rootProject.subprojects.any { it.name == 'expo-modules-core' }
    def packageJsonFile = new File(rootProject.projectDir.parentFile, 'package.json')
    
    def hasExpoDependency = false
    def projectVersion = '1.0.0' // Default version

    if (packageJsonFile.exists()) {
        def packageJson = new groovy.json.JsonSlurper().parseText(packageJsonFile.text)
        projectVersion = packageJson.version ?: '1.0.0' // Get project version

        // Check for expo dependency and version >= 50. Only resolved when
        // expo-modules-core is part of the build and expo is declared; every
        // other project pays nothing beyond this package.json read.
        String expoVersionString = packageJson.dependencies?.expo ?: packageJson.devDependencies?.expo
        Integer expoMajorVersion = null
        if (hasExpoModulesCore && expoVersionString) {
            // For pnpm catalogs (e.g. "catalog:native") the declared version is
            // not numeric: prefer the installed package, then the declaration,
            // and spawn `node` only as the last resort.
            expoMajorVersion = readInstalledExpoMajorVersion(packageJsonFile.parentFile)
                ?: parseMajorVersion(expoVersionString)
                ?: resolveInstalledExpoMajorVersion(packageJsonFile.parentFile)
        }

        hasExpoDependency = expoMajorVersion != null && expoMajorVersion >= 50
    }
    
    def isExpo = hasExpoModulesCore && hasExpoDependency
    
    // Return a map containing both pieces of information
    return [isExpo: isExpo, version: projectVersion]
}

// Get project info map
def projectInfo = checkProjectInfo()
// Extract info into variables
def projectVersion = projectInfo.version
def expoProject = projectInfo.isExpo

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

if (expoProject) {
    group = 'expo.modules.pushy'
    version = projectVersion

    def expoModulesCorePlugin = new File(project(":expo-modules-core").projectDir.absolutePath, "ExpoModulesCorePlugin.gradle")
    apply from: expoModulesCorePlugin
    applyKotlinExpoModulesCorePlugin()
    // useExpoPublishing()
    useCoreDependencies()
} else {
    group = 'cn.reactnative.modules.update'
    version = projectVersion
}

android {
    if (supportsNamespace()) {
        namespace "cn.reactnative.modules.update"

        sourceSets {
            main {
                manifest.srcFile "src/main/AndroidManifestNew.xml"
            }
        }
    }
    compileSdkVersion safeExtGet('compileSdkVersion', 34)
    buildToolsVersion safeExtGet('buildToolsVersion', '34.0.0')
    defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion', 21)
        targetSdkVersion safeExtGet('targetSdkVersion', 34)
        consumerProguardFiles "proguard.pro"
        buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
    }
    
    sourceSets {
        main {
            // let gradle pack the shared library into apk
            jniLibs.srcDirs = ['./lib']
            if (isNewArchitectureEnabled()) {
                java.srcDirs += ['src/newarch']
            } else {
                java.srcDirs += ['src/oldarch']
            }
            
            // The Expo module lives in the default src/main/java tree and is
            // only compiled when the host is an Expo (SDK 50+) project.
            if (!expoProject) {
                java.exclude 'expo/modules/pushy/**'
            }
        }
    }

    // AGP 9 disables the resValues feature for libraries by default; the
    // pushy_build_time resource below needs it back on. The block exists
    // since AGP 4, so guard like supportsNamespace() does for old projects.
    def agpMajor = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')[0].toInteger()
    if (agpMajor >= 4) {
        buildFeatures {
            resValues true
        }
    }

    buildTypes {
        release {
            resValue("string", "pushy_build_time", "${minutesSinceEpoch}")
        }
        debug {
            resValue("string", "pushy_build_time", "0")
        }
    }
    if (agpMajor >= 7) {
        lint {
            abortOnError false
        }
    } else {
        lintOptions {
            abortOnError false
        }
    }

    testOptions {
        // Pure-JVM unit tests (android/src/test): android.* calls return
        // defaults instead of throwing, org.json comes from the real library.
        unitTests.returnDefaultValues = true
    }
}

dependencies {
    implementation 'com.facebook.react:react-native:+'
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.json:json:20240303'
}
if (isNewArchitectureEnabled()) {
    react {
        jsRootDir = file("../lib/")
        libraryName = "update"
        codegenJavaPackageName = "cn.reactnative.modules.update"
    }
}
