package com.gic.haoban.manage.service.util;

import com.alibaba.fastjson.JSONObject;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.*;

public class SignatureGetterUtil {
    private String secret;

    public SignatureGetterUtil(String secret) {
        this.secret = secret;
    }

    public String get(String jsonStr) throws Exception {
        Map<String, Object> jsonDict = new HashMap<>();
        jsonDict = JSONObject.parseObject(jsonStr, jsonDict.getClass());
        List<String> paramList = new ArrayList<>();
        json2List(jsonDict, paramList);
        Collections.sort(paramList);
        return hmacSha256(list2String(paramList));
    }

    private String list2String(List<String> paramList) {
        String str = "";
        for (String param : paramList) {
            if (param.startsWith("sig=")) {
                continue; // ignore sig self
            }
            str += param + "&";
        }
        return str.substring(0, str.length() - 1);
    }

    private String hmacSha256(String data) throws Exception {
        Mac mac = Mac.getInstance("HmacSHA256");
        SecretKeySpec secretKeySpec = new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256");
        mac.init(secretKeySpec);
        byte[] signature = mac.doFinal(data.getBytes("UTF-8"));
        return Base64.getEncoder().encodeToString(signature);
    }

    private void json2List(Map<String, Object> jsonDict, List<String> paramList) {
        for (Map.Entry<String, Object> entry : jsonDict.entrySet()) {
            String k = entry.getKey();
            Object v = entry.getValue();
            if (v instanceof List) {
                for (Object item : (List<?>) v) {
                    json2List((Map<String, Object>) item, paramList);
                }
            } else if (v instanceof Map){
                json2List((Map)v,paramList);
            }else {
                paramList.add(k + "=" + v);
            }
        }
    }
    /**
     * 模拟生成随机 nonce 字符串
     *
     * @return 随机字符串
     */
    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    public static String generateRandomString(int length) {
        SecureRandom random = new SecureRandom();
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(CHARACTERS.length());
            char randomChar = CHARACTERS.charAt(randomIndex);
            sb.append(randomChar);
        }
        return sb.toString();
    }

}
