import groovy.json.JsonSlurper

repositories {
    google()
    mavenCentral()
}

ext {
    // MoEngage Versions
    moengageNativeBOMVersion = "2.2.2"
    moengagePluginBaseBOMVersion = "3.0.1"

    // AndroidX Support Libraries Versions
    androidXCore = "1.15.0"
    androidXCompact = "1.7.0"
    androidXLifecycle = "2.8.7"
}

dependencies {
    implementation(platform("com.moengage:plugin-base-bom:$moengagePluginBaseBOMVersion"))
    api("com.moengage:plugin-base")

    api(platform("com.moengage:android-bom:$moengageNativeBOMVersion"))
    api("com.moengage:moe-android-sdk")
    api("com.moengage:inapp")

    if (shouldIncludeModule("richNotification")) {
        api("com.moengage:rich-notification")
    }

    if (shouldIncludeModule("encryption")) {
        api("com.moengage:security")
        api("com.moengage:encrypted-storage")
    }

    if (shouldIncludeModule("pushAmp")) {
        api("com.moengage:push-amp")
    }

    if (shouldIncludeModule("hmsPushkit")) {
        api("com.moengage:hms-pushkit")
    } 

    if (shouldIncludeModule("deviceTrigger")) {
        api("com.moengage:realtime-trigger")
    }

    if (shouldIncludeModule("includeAndroidXRequiredLibraries")) {
        api("androidx.core:core:$androidXCore")
        api("androidx.appcompat:appcompat:$androidXCompact")
        api("androidx.lifecycle:lifecycle-process:$androidXLifecycle")
    }
}

def shouldIncludeModule(String moduleName) {
    try {
        File packageJsonFile = new File("$rootDir/../package.json")
        def packageJson = new JsonSlurper().parseText(packageJsonFile.text)
        def moengageJson = packageJson["moengage"]
        if (moengageJson != null) {
        return moengageJson[moduleName] == true
        }
        println("MoEngage modules not configured in package json")
    } catch(FileNotFoundException exception) {
        println("Default package json file not found")
    }
    return false
}