package com.qmai.android.plugin


import org.gradle.api.Plugin
import org.gradle.api.Project

/***
 * 编译加上传到oss中
 */
class OssAndPygerPlugin implements Plugin<Project> {
    @Override
    void apply(Project project) {
        MultiUploadBean ossUploadFileBean = project.getExtensions().create("uploadOssAndPyger", MultiUploadBean.class)
        PygerBean pygerBean = project.getExtensions().create("BuildAndPygerInfo", PygerBean.class)
        project.afterEvaluate {
            Map<String, String> params = System.getenv()
            String type = "release"
            String flavor = "arm64"
            if (params != null) {
                if (params.get("build_type") != null)
                    type = params.get("build_type")
                if (params.get("abi_flter") != null)
                    flavor = params.get("abi_flter")
            }
            type = firstLetterLower(type)
            flavor = firstLetterLower(flavor)
            String dirName = "${project.layout.buildDirectory.asFile.get()}/outputs/apk/$flavor/$type/"
            String apkName = "qmsd_${type}_${flavor}" +
                    "_${ossUploadFileBean.versionCode}_${ossUploadFileBean.versionName}.apk"
            def uploadFile = new File(dirName + apkName)
            print("-OssAndPygerPlugin--dirName apkName:${dirName + apkName}")
            project.getTasks().register("uploadApkOss", OssUploadTask.class, uploadFile, ossUploadFileBean.ossPath, type,
                    flavor, ossUploadFileBean.appName)
            project.getTasks().register("uploadApkPyger",
                    PygerUploadTask.class, uploadFile,
                    type, pygerBean.key, flavor, ossUploadFileBean.appName)
        }
    };

    String firstLetterLower(String str) {
        if (str == null || str.isEmpty()) {
            return str; // 处理空字符串或 null 的情况
        }
        char[] cs = str.toCharArray();
        if (Character.isUpperCase(cs[0])) {
            cs[0] += 32; // 将首字母转换为小写
        }
        return String.valueOf(cs);
    }
}
