package com.fs.erp.utils;

import cn.hutool.core.bean.BeanUtil;
import com.fs.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

@Slf4j
public class SignUtil {
    private SignUtil() {}

    /**
     * 获取签名 - 支持Map<String, Object>类型参数
     *
     * @param app_secret 密钥
     * @param params 参数Map，支持Object类型值
     * @return 签名字符串
     */
    public static String getSignNew(String app_secret, Map<String, Object> params) {
        try {
            String sortedStr = getSortedParamStrNew(params);
            String paraStr = app_secret + sortedStr;

            log.info("paraStr: {}",paraStr);
            return createSign(paraStr);
        } catch (UnsupportedEncodingException e) {
            log.warn("getSign UnsupportedEncodingException ", e);
        }

        return StringUtils.EMPTY;
    }

    public static String getSignNew(String app_secret, Object obj) {
        try {
            String sortedStr = getSortedParamStrNew(BeanUtil.beanToMap(obj));
            String paraStr = app_secret + sortedStr;

            return createSign(paraStr);
        } catch (UnsupportedEncodingException e) {
            log.warn("getSign UnsupportedEncodingException ", e);
        }

        return StringUtils.EMPTY;
    }

    /**
     * 构造自然排序请求参数 - 支持Map<String, Object>类型参数
     *
     * @param params 请求参数
     * @return 排序后的字符串
     */
    private static String getSortedParamStrNew(Map<String, Object> params) throws UnsupportedEncodingException {
        Set<String> sortedParams = new TreeSet<>(params.keySet());

        StringBuilder strB = new StringBuilder();
        // 排除sign和空值参数
        for (String key : sortedParams) {
            if ("sign".equalsIgnoreCase(key)) {
                continue;
            }

            Object valueObj = params.get(key);

            // 处理Object类型值，转换为字符串
            String value = valueObj != null ? valueObj.toString() : null;

            if (StringUtils.isNotEmpty(value)) {
                strB.append(key).append(value);
            }
        }
        return strB.toString();
    }

    /**
     * 生成新sign
     *
     * @param str 字符串
     * @return String
     */
    private static String createSign(String str) {
        if (str == null || str.length() == 0) {
            return null;
        }
        char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(str.getBytes("UTF-8"));

            byte[] md = mdTemp.digest();
            int j = md.length;
            char[] buf = new char[j * 2];
            int k = 0;
            int i = 0;
            while (i < j) {
                byte byte0 = md[i];
                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
                buf[k++] = hexDigits[byte0 & 0xf];
                i++;
            }
            return new String(buf);
        } catch (Exception e) {
            log.warn("create sign was failed", e);
            return null;
        }
    }

    /**
     * 原有方法保留，保持向后兼容
     */
    public static String getSign(String app_secret, Map<String, String> params) {
        try {
            String sortedStr = getSortedParamStr(params);
            String paraStr = app_secret + sortedStr;

            return createSign(paraStr);
        } catch (UnsupportedEncodingException e) {
            log.warn("getSign UnsupportedEncodingException ", e);
        }

        return StringUtils.EMPTY;
    }

    /**
     * 原有方法保留，保持向后兼容
     */
    private static String getSortedParamStr(Map<String, String> params) throws UnsupportedEncodingException {
        Set<String> sortedParams = new TreeSet<>(params.keySet());

        StringBuilder strB = new StringBuilder();
        // 排除sign和空值参数
        for (String key : sortedParams) {
            if ("sign".equalsIgnoreCase(key)) {
                continue;
            }

            String value = params.get(key);

            if (StringUtils.isNotEmpty(value)) {
                strB.append(key).append(value);
            }
        }
        return strB.toString();
    }
}
