package com.tofly.third.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ntp.TimeStamp;

import java.security.MessageDigest;
import java.util.*;

@Slf4j
public class SignUtil {

    public static boolean checkSign(String appId, String timestamp, String requestSign, String appSecret) {
        SortedMap<String, String> sortedMap = new TreeMap<>();
        sortedMap.put("appId", appId);
        sortedMap.put("timestamp", timestamp);
        String appSign = SignUtil.createSign(sortedMap, appSecret);
        return requestSign.equalsIgnoreCase(appSign);
    }

    /**
     * 生成签名sign
     * 加密前：appId=wx123456789&timestamp=1583332804914&key=7214fefff0cf47d7950cb2fc3b5d670a
     * 加密后：E2B30D3A5DA59959FA98236944A7D9CA
     */
    public static String createSign(SortedMap<String, String> params, String key) {
        StringBuilder sb = new StringBuilder();
        Set<Map.Entry<String, String>> es = params.entrySet();
        Iterator<Map.Entry<String, String>> it = es.iterator();
        //生成
        while (it.hasNext()) {
            Map.Entry<String, String> entry = it.next();
            String k = entry.getKey();
            String v = entry.getValue();
            if (StrUtil.isNotEmpty(v) && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k).append("=").append(v).append("&");
            }
        }
        sb.append("key=").append(key);
        String sign = MD5(sb.toString()).toUpperCase();
        return sign;
    }

    /**
     * 校验签名
     */
    public static Boolean isCorrectSign(SortedMap<String, String> params, String key, String requestSign) {
        String sign = createSign(params, key);
        requestSign = requestSign.toUpperCase();
        log.info("通过用户发送数据获取新签名：{}", sign);
        return requestSign.equals(sign);
    }

    /**
     * md5常用工具类
     */
    public static String MD5(String data) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] array = md5.digest(data.getBytes("UTF-8"));
            StringBuilder sb = new StringBuilder();
            for (byte item : array) {
                sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
            }
            return sb.toString().toUpperCase();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 生成uuid
     */
    public static String generateUUID() {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32);
        return uuid;
    }

//    public static void main(String[] args) {
//        String appId = "qdjc";
//        String appSecret = "f650d7f5da084075972ab2f0a97bcdbd";
//        long l = System.currentTimeMillis();
//        String timestamp = String.valueOf(l);
//
//        System.out.println("appId：" + appId);
//        System.out.println("appSecret：" + appSecret);
//        System.out.println("timestamp：" + timestamp);
//
//        SortedMap<String, String> sortedMap = new TreeMap<>();
//        sortedMap.put("appId", appId);
//        sortedMap.put("timestamp", timestamp);
//        String requestSign = SignUtil.createSign(sortedMap, appSecret);
//        System.out.println("sign：" + requestSign);
//    }
}
