package com.winsun.smsUtils;

import org.apache.commons.lang3.StringUtils;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class RequestParasUtil {

    public RequestParasUtil() {
    }

    /**
     * 加密，获取签名
     * @param plainText
     * @param appSecret
     * @return
     */
    public static String generateHmacSignature(String plainText, String appSecret) {
        String signature = null;
        try {
            long begin = System.currentTimeMillis();
            signature = HMACSHA1.getSignature(plainText, appSecret);
        } catch (Exception var5) {
            var5.printStackTrace();
        }
        return signature;
    }


    /**
     * 平台通用接口封装方式
     * @param requestData
     * @param appKey
     * @param appSecret
     * @param version
     * @param format
     * @param clientType
     */
    public static void setParas(Map<String, String> requestData, String appKey, String appSecret, String version, String format, String clientType) {
        if (requestData == null) {
        } else if (appKey != null && version != null && clientType != null) {
            format = format == null ? "json" : format;
            String cipherParas = generateXXTeaParasData(requestData, appSecret);
            requestData.clear();
            requestData.put("appKey", appKey);
            requestData.put("version", version);
            requestData.put("clientType", clientType);
            requestData.put("format", format);
            requestData.put("paras", cipherParas);
//            System.out.println("appSecret:"+appSecret);
            String plainSig = appKey + clientType + format + version + cipherParas;
            requestData.put("sign", generateHmacSignature(plainSig, appSecret));
        } else {
        }
    }

    private static String generateXXTeaParasData(Map<String, String> requestData, String appSecret) {
        try {
            StringBuffer xxTeaText = new StringBuffer();
            if (requestData != null && requestData.size() >= 1) {
                String name;
                String value;
                for(Iterator var4 = requestData.entrySet().iterator(); var4.hasNext(); xxTeaText.append(name + "=" + value + "&")) {
                    Map.Entry<String, String> entry = (Map.Entry)var4.next();
                    name = (String)entry.getKey();
                    value = "";
                    if ("returnURL".equals(name)) {
                        value = URLEncoder.encode((String)entry.getValue(), "utf-8");
                    } else {
                        value = (String)entry.getValue();
                    }
                }
                if (!requestData.containsKey("timeStamp")) {
                    xxTeaText.append("timeStamp=" + getTimeStamp());
                }

                return XXTea.encrypt(xxTeaText.toString(), "UTF-8", StringUtil.toHex(appSecret.getBytes()));
            } else {
                return null;
            }
        } catch (Exception var7) {
            var7.printStackTrace();
            return null;
        }
    }

    public static Map<String, String> parseXXTeaParasData(String xxTeaText, String appSecret) throws Exception {
        Map<String,String> requestDataMap = null;
        try {
            long begin = System.currentTimeMillis();
            String plainParasText = XXTea.decrypt(xxTeaText, "UTF-8", StringUtil.toHex(appSecret.getBytes()));
            requestDataMap = splitParameters(plainParasText);
            return requestDataMap;
        } catch (Exception var4) {
            var4.printStackTrace();
            throw new Exception("XXTea-decypt-error");
        }
    }

    private static Map<String, String> splitParameters(String paraStr) {
        Map<String, String> parameters = new HashMap<>();
        if (StringUtils.isNotBlank(paraStr)) {
            String[] array = paraStr.trim().split("&");
            String[] var6 = array;
            int var5 = array.length;

            for(int var4 = 0; var4 < var5; ++var4) {
                String temp = var6[var4];
                if (StringUtils.isNotBlank(temp)) {
                    temp = temp.trim();
                    int index = temp.indexOf("=");
                    if (index > 0) {
                        String key = temp.substring(0, index);
                        String value = temp.substring(index + 1);
                        if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(value)) {
                            parameters.put(key.trim(), value.trim());
                        }
                    }
                }
            }
        }

        return parameters;
    }

    public static String getTimeStamp() {
        return String.valueOf(System.currentTimeMillis());
    }
}
