package com.crm.rely.backend.util;

import com.google.common.base.Strings;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

public class StringUtil {

    /**
     * 驼峰转换为下划线
     *
     * @param camelCaseName
     * @return
     */
    public static String underscoreName(String camelCaseName) {
        StringBuilder result = new StringBuilder();
        if (camelCaseName != null && camelCaseName.length() > 0) {
            result.append(camelCaseName.substring(0, 1).toLowerCase());
            for (int i = 1; i < camelCaseName.length(); i++) {
                char ch = camelCaseName.charAt(i);
                if (Character.isUpperCase(ch)) {
                    result.append("_");
                    result.append(Character.toLowerCase(ch));
                } else {
                    result.append(ch);
                }
            }
        }
        return result.toString();
    }

    public static String unionStrings(String s1, String s2) {
        Set<String> set = new LinkedHashSet<>(); // 保持顺序 & 去重
        if (s1 != null && !s1.isEmpty()) {
            set.addAll(Arrays.asList(s1.split(",")));
        }
        if (s2 != null && !s2.isEmpty()) {
            set.addAll(Arrays.asList(s2.split(",")));
        }
        return String.join(",", set);
    }

    /**
     * 下划线转换为驼峰
     *
     * @param underscoreName
     * @return
     */
    public static String camelCaseName(String underscoreName) {
        StringBuilder result = new StringBuilder();
        if (underscoreName != null && underscoreName.length() > 0) {
            boolean flag = false;
            for (int i = 0; i < underscoreName.length(); i++) {
                char ch = underscoreName.charAt(i);
                if ("_".charAt(0) == ch) {
                    flag = true;
                } else {
                    if (flag) {
                        result.append(Character.toUpperCase(ch));
                        flag = false;
                    } else {
                        result.append(ch);
                    }
                }
            }
        }
        return result.toString();
    }

    /**
     * 获取xlsx后缀文件名称
     * 如果前缀为null或者'' 则随机生成uuid(UUIDUtil.getUUID())
     *
     * @param fileNamePrefix 文件前缀
     * @return 返回文件名称 fileNamePrefix_yyyyMMddHHmmss.xlsx
     */
    public static String getXlsxFileName(String fileNamePrefix) {

        return getXlsxFileName(fileNamePrefix, true);
    }

    public static String getXlsxFileName(String fileNamePrefix, boolean isPrefix) {
        if (Strings.isNullOrEmpty(fileNamePrefix)) {
            fileNamePrefix = UUIDUtil.getUUID();
        }

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(fileNamePrefix.trim());
        if (isPrefix) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            stringBuffer.append("_");
            stringBuffer.append(sdf.format(new Date()));
        }

        stringBuffer.append(".xlsx");
        String fileName = stringBuffer.toString();
        return fileName;
    }

    public static String getCsvFileName(String fileNamePrefix, boolean isPrefix) {
        if (Strings.isNullOrEmpty(fileNamePrefix)) {
            fileNamePrefix = UUIDUtil.getUUID();
        }

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(fileNamePrefix.trim());
        if (isPrefix) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            stringBuffer.append("_");
            stringBuffer.append(sdf.format(new Date()));
        }

        stringBuffer.append(".csv");
        String fileName = stringBuffer.toString();
        return fileName;
    }

    public static String secretString(String orient) {
        if (StringUtils.isEmpty(orient)) {
            return "";
        }
        char[] chars = orient.toCharArray();

        int show;
        if (chars.length <= 5) {
            show = 1;
        } else {
            show = 2;
        }
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            if (i < show || (i + show) >= chars.length) {
                stringBuffer.append(chars[i]);
            } else {
                stringBuffer.append("*");
            }
        }

        return stringBuffer.toString();
    }

    /**
     * 随机选择字符串并转换成Long
     *
     * @param s 带,的字符串
     * @return Long
     */
    public static Long selectRandomStringConvertToLong(String s) {
        if (Strings.isNullOrEmpty(s)) {
            return null;
        }

        if (s.contains(",")) {
            List<String> stringList = Arrays.asList(s.split(","));
            if (stringList == null || stringList.isEmpty()) {
                return null;
            }

            // 过滤掉空串、非数字项
            List<String> filtered = stringList.stream()
                    .map(String::trim)
                    .filter(str -> !str.isEmpty() && str.matches("\\d+"))
                    .collect(Collectors.toList());

            if (filtered.isEmpty()) {
                return null;
            }

            String randomString = filtered.get(new Random().nextInt(filtered.size()));
            return Long.valueOf(randomString);
        }

        s = s.trim();
        if (!s.matches("\\d+")) {
            return null;
        }

        return Long.valueOf(s);
    }

    public static void main(String[] args) {
        String s1 = "1,6,5";
        String s2 = "7,8";

        System.out.println(unionStrings(s1, s2));


    }
}
