import groovy.json.JsonSlurper


def getProjectRoot = { ->
    def projectRoot = "$rootDir/../../"
    if (System.getProperties().projectRoot != null) {
        projectRoot = System.getProperties().projectRoot
    }
    return projectRoot
}

def getBuildPath = { ->
    def relativeBuildToApp = "platforms"
    if (System.getProperties().appBuildPath != null) {
        relativeBuildToApp = System.getProperties().appBuildPath
    }
    return relativeBuildToApp
}

def USER_PROJECT_ROOT = getProjectRoot()
def PLATFORMS_ANDROID = getBuildPath() + "/android"
def PLUGIN_NAME = "{{pluginName}}"



def dependenciesJson = file("${USER_PROJECT_ROOT}/${PLATFORMS_ANDROID}/dependencies.json")
def appDependencies = new JsonSlurper().parseText(dependenciesJson.text)
def pluginData = appDependencies.find { it.name == PLUGIN_NAME }
def nativescriptDependencies = appDependencies.findAll{pluginData.name == it.name}

def getDepPlatformDir = { dep ->
        file("$USER_PROJECT_ROOT/$PLATFORMS_ANDROID/${dep.directory}/$PLATFORMS_ANDROID")
}


def applyIncludeSettingsGradlePlugin = {
     nativescriptDependencies.each { dep ->
            def includeSettingsGradlePath = "${getDepPlatformDir(dep)}/include-settings.gradle"
            if (file(includeSettingsGradlePath).exists()) {
                apply from: includeSettingsGradlePath
            }
    }
}

applyIncludeSettingsGradlePlugin()