import java.lang.reflect.Method

apply plugin: 'com.android.library'
apply from: 'lebian_cust.gradle'
def lebian_internal_only = false
if (rootProject.extensions.getByName('ext').properties.containsKey('lebian_internal_only')) {
    if (rootProject.extensions.getByName('ext').properties.get('lebian_internal_only')) {
        lebian_internal_only = true
        println("[LEBIAN] lebian_internal_only true")
        apply from: 'internal_use_only'
    }
}

boolean enable_regeng = getValueFromSettings("use_regeng").toBoolean()
boolean enable_fenbao = getValueFromSettings("use_streaming").toBoolean()

android {
    compileSdkVersion 29

    defaultConfig {
        println "[LEBIAN] cust cfg=[enable_fenbao=${enable_fenbao}, enable_regeng=${enable_regeng}]"
        minSdkVersion 8
        targetSdkVersion 26

        manifestPlaceholders = [
                "MainChId": lebian.meta_MainChId,
                "ClientChId": lebian.meta_ClientChId,
                "LEBIAN_SECID": lebian.meta_LEBIAN_SECID,
                "LBCloudId": lebian.meta_LBCloudId,
                "LEBIAN_APPLICATION": 'com.excelliance.open.LBApplication',
                "screenOrientation": lebian.screenOrientation,
                "lb_target_pkgname": (lebian.packageName ? lebian.packageName :'${applicationId}'),
                "lb_task_Affinity": (lebian.taskAffinity ? lebian.taskAffinity :'${applicationId}')
        ]
        consumerProguardFiles 'consumer-rules.pro'
    }

    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }

    buildTypes {
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), "proguard-rules.pro"
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), "proguard-rules.pro"
        }
    }

    sourceSets {
        main {
            if (!lebian_internal_only) {
                if (enable_fenbao && enable_regeng) {
                    manifest.srcFile 'src/regeng/AndroidManifest.xml'
                    assets.srcDirs += 'src/fenbao-with-regeng/assets'
                } else if (enable_fenbao) {
                    assets.srcDirs += 'src/fenbao-lite/assets'
                }else{
                    manifest.srcFile 'src/regeng/AndroidManifest.xml'
                    assets.srcDirs += 'src/regeng/assets'
                }
            }
        }
    }

    if (rootProject.extensions.getByName('ext').properties.containsKey('lebian_internal_only') 
            && rootProject.extensions.getByName('ext').properties.get('lebian_internal_only')) {
        ndkVersion '16.1.4479499'
    }
}

afterEvaluate {
    if (null != getRootProject().findProject(":lebianbase")) {
        tasks.getByName('assembleRelease').dependsOn(':sdkapk:buildBase')
        tasks.getByName('assembleDebug').dependsOn(':sdkapk:buildBaseDebug')
    }
}

dependencies {
    api fileTree(include: ['*.jar'], dir: 'libs')
}

def getValueFromSettings(taget) {
    def ret
    def xmlFile = "${projectDir}/src/main/assets/lebian/globalSettings.xml"
    def xp
    try {
        xp = Class.forName("groovy.xml.XmlParser").newInstance()
    } catch (ClassNotFoundException e) {
        xp = Class.forName("groovy.util.XmlParser").newInstance()
    }
    Method parse = xp.class.getDeclaredMethod("parse", String.class)
    print "parse is "+parse
    def result = parse.invoke(xp, (String)xmlFile);
    result.setting.each {
        def key = it.attributes().get("key")
        if (key == taget) {
            ret = it.attributes().get("value")
            println ret
        }
    }
    return ret
}