apply from: "${rootDir.path}/gradle/util/utils.gradle"
apply from: "${rootDir.path}/gradle/config/configBuild.gradle"

gradle.ext {
    applicationId = 'com.blankj.androidutilcode'
    appName = 'Util'

    compileSdkVersion = 27
    minSdkVersion = 14
    targetSdkVersion = 27
    versionCode = 1_024_006
    versionName = '1.24.6'// E.g. 1.9.72 => 1,009,072

    // lib version
    kotlin_version = '1.3.10'
    support_version = '27.1.1'
    leakcanary_version = '1.6.3'

    dep = [:]

    depConfig = [
            plugin_gradle              : new DepConfig("com.android.tools.build:gradle:3.4.0"),
            plugin_kotlin              : new DepConfig("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"),
            plugin_maven               : new DepConfig("com.github.dcendents:android-maven-gradle-plugin:2.1"),// 上传到 maven
            plugin_bintray             : new DepConfig("com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4"),// 上传到 bintray
            plugin_traute              : new DepConfig("tech.harmonysoft:traute-gradle:1.1.10"),// 注解转非空判断

            plugin_bus                 : new DepConfig(false/*是否本地调试*/, "", "com.blankj:bus-gradle-plugin:1.8", true),

            buildSrc                   : new DepConfig(":buildSrc"),
            bus_gradle_plugin          : new DepConfig(":bus-gradle-plugin", true),
            launcher_app               : new DepConfig(":launcher:app"),
            launcher_pkg               : new DepConfig(":launcher:pkg"),
            lib_base                   : new DepConfig(":lib:base"),
            lib_common                 : new DepConfig(":lib:common"),

            subutil_app                : new DepConfig(":subutil:app"),
            subutil_export             : new DepConfig(":subutil:export"),
            subutil_lib                : new DepConfig(":subutil:lib"),
            subutil_pkg                : new DepConfig(":subutil:pkg"),

            utilcode_app               : new DepConfig(":utilcode:app"),
            utilcode_export            : new DepConfig(":utilcode:export"),
            utilcode_lib               : new DepConfig(true/*是否本地调试*/, ":utilcode:lib", "com.blankj:utilcode:$versionName"),
            utilcode_pkg               : new DepConfig(":utilcode:pkg"),

            support_appcompat_v7       : new DepConfig("com.android.support:appcompat-v7:$support_version"),
            support_design             : new DepConfig("com.android.support:design:$support_version"),
            support_multidex           : new DepConfig("com.android.support:multidex:1.0.2"),

            constraint                 : new DepConfig("com.android.support.constraint:constraint-layout:1.1.3"),
            kotlin                     : new DepConfig("org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"),

            leakcanary_android         : new DepConfig("com.squareup.leakcanary:leakcanary-android:$leakcanary_version"),
            leakcanary_android_no_op   : new DepConfig("com.squareup.leakcanary:leakcanary-android-no-op:$leakcanary_version"),
            leakcanary_support_fragment: new DepConfig("com.squareup.leakcanary:leakcanary-support-fragment:$leakcanary_version"),

            free_proguard              : new DepConfig("com.blankj:free-proguard:1.0.1"),
            swipe_panel                : new DepConfig("com.blankj:swipe-panel:1.1"),

            gson                       : new DepConfig("com.google.code.gson:gson:2.8.2"),
            glide                      : new DepConfig("com.github.bumptech.glide:glide:4.7.1"),
            retrofit                   : new DepConfig("com.squareup.retrofit2:retrofit:2.4.0"),
            javassist                  : new DepConfig("org.javassist:javassist:3.24.0-GA"),
            commons_io                 : new DepConfig("commons-io:commons-io:2.5"),

            junit                      : new DepConfig("junit:junit:4.12"),
            robolectric                : new DepConfig("org.robolectric:robolectric:4.2"),
    ]

    fn = [
            includeDep: this.&includeDep,
    ]
}

def includeDep() {// 根据 config 来 include 各 module
    gradle.ext.Utils.log("includeDep -> depConfig = " + gradle.ext.Utils.object2String(gradle.ext.depConfig))
    gradle.ext.depConfig.each { String name, DepConfig config ->
        if (config.isApply) {
            if (config.useLocal) {
                include config.localPath
            }
        }
    }
}

class DepConfig {
    boolean useLocal // 是否使用本地的
    String localPath // 本地路径
    String remotePath// 远程路径
    boolean isApply  // 是否应用
    String path      // 最后的路径
    String isDebug   // 是否调试
    String groupId
    String artifactId
    String version

    DepConfig(String path) {
        this(path, true)
    }

    DepConfig(String path, boolean isApply) {
        if (path.startsWith(":")) {
            this.useLocal = true
            this.localPath = path
            this.isApply = isApply
        } else {
            this.useLocal = false
            this.remotePath = path
            this.isApply = isApply
        }
        this.path = path
    }

    DepConfig(boolean useLocal, String localPath, String remotePath) {
        this(useLocal, localPath, remotePath, true)
    }

    DepConfig(boolean useLocal, String localPath, String remotePath, boolean isApply) {
        this.useLocal = useLocal
        this.localPath = localPath
        this.remotePath = remotePath
        this.isApply = isApply
        this.path = useLocal ? localPath : remotePath
    }

    String getGroupId() {
        String[] splits = remotePath.split(":")
        return splits.length == 3 ? splits[0] : null
    }

    String getArtifactId() {
        String[] splits = remotePath.split(":")
        return splits.length == 3 ? splits[1] : null
    }

    String getVersion() {
        String[] splits = remotePath.split(":")
        return splits.length == 3 ? splits[2] : null
    }


    @Override
    String toString() {
        return "DepConfig { " +
                "useLocal = " + useLocal + ", " +
                "path = " + path + ", " +
                "isApply = " + isApply + "" +
                " }"
    }
}
//./gradlew clean :utilcode:lib:bintrayUpload