package cn.stylefeng.team.generator.core.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;

public class TemplateFormatUtil {

    public static Integer toInt(Object value) {
        if (value instanceof Double) {
            BigDecimal bigDecimal = BigDecimal.valueOf((Double) value);
            return bigDecimal.intValue();
        } else {
            return Integer.valueOf(value.toString());
        }
    }

    public static String currentTime() {
        return DateUtil.formatDateTime(new Date());
    }

    public static String lowerFirst(String value) {
        return StrUtil.lowerFirst(value);
    }

    public static String cleanWhite(String value) {
        return StrUtil.cleanBlank(value);
    }

    public static Boolean windowsFlag() {
        String os = System.getProperty("os.name");
        return os.startsWith("Windows");
    }

    public static String toJson(Object obj) {
        return JSON.toJSONString(obj);
    }

    public static String emptyJoin(Collection<?> collection) {
        return CollectionUtil.join(collection, StrUtil.COMMA, "", "");
    }

    public static String join(Object... objects) {
        return ArrayUtil.join(objects, StrUtil.COMMA, "'", "'");
    }

    public static String join(Collection<?> collection) {
        return CollectionUtil.join(collection, StrUtil.COMMA, "'", "'");
    }

    public static String joinSqlFieldCodes(Collection<?> collection) {
        return CollectionUtil.join(collection, StrUtil.COMMA, "`", "`");
    }

    public static String like(Object param) {
        return StrUtil.concat(true, "'%", param.toString(), "%'");
    }

    public static String formatObjValidateName(String originName) {
        if (StrUtil.isBlank(originName)) {
            return originName;
        }
        if (!originName.contains(".")) {
            return "'" + originName + "'";
        }
        String[] objArray = originName.split("\\.");
        StringBuilder stringBuilder = new StringBuilder("[");
        for (int i = 0; i < objArray.length; i++) {
            stringBuilder.append("'").append(objArray[i]).append("', ");
        }
        String finalStr = StrUtil.removeSuffix(stringBuilder.toString(), ", ");
        return finalStr + "]";
    }

    public static String underLine(String originCode) {
        if (StrUtil.isBlank(originCode)) {
            return "";
        }
        return StrUtil.toUnderlineCase(originCode).toLowerCase();
    }

    public static String underLineUpperCase(String originCode) {
        if (StrUtil.isBlank(originCode)) {
            return "";
        }
        return StrUtil.toUnderlineCase(originCode).toUpperCase();
    }

    public static String camelCase(String originCode) {
        if (StrUtil.isBlank(originCode)) {
            return "";
        }
        return StrUtil.toCamelCase(originCode);
    }

    public static String camelCaseFirstUp(String originName) {
        if (StrUtil.isBlank(originName)) {
            return "";
        }

        return StrUtil.upperFirst(StrUtil.toCamelCase(originName));
    }

    public static String middleLine(String originCode) {
        if (StrUtil.isBlank(originCode)) {
            return "";
        }

        return underLine(originCode).replace("_", "-");
    }

    public static String enumCodeGenerate(String dictCode) {

        if (NumberUtil.isNumber(dictCode)) {
            return "ITEM_" + dictCode;
        }

        return StrUtil.toUnderlineCase(dictCode).toUpperCase();
    }

}
