//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.game.business.util.other;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.InputSource;

import java.io.StringReader;
import java.net.URLEncoder;
import java.util.*;

public class SignUtils {
    public SignUtils() {
    }

    public static boolean checkParamOld(Map<String, String> params, String key) {
        boolean result = false;
        if (params.containsKey("sign")) {
            String sign = params.get("sign");
            params.remove("sign");
            StringBuilder buf = new StringBuilder((params.size() + 1) * 10);
            buildPayParamsOld(buf, params, false);
            String preStr = buf.toString();
            String signRecieve = MD5.sign(preStr, "&key=" + key, "utf-8");
            result = sign.equalsIgnoreCase(signRecieve);
        }

        return result;
    }

    public static boolean checkParam(Map<String, String> params, String key) {
        boolean result = false;
        if (params.containsKey("sign")) {
            String sign = params.get("sign");
            params.remove("sign");
            StringBuilder buf = new StringBuilder((params.size() + 1) * 10);
            buildPayParams(buf, params, false);
            String preStr = buf.toString();
            String signRecieve = MD5.sign(preStr, "&key=" + key, "utf-8");
            result = sign.equalsIgnoreCase(signRecieve);
        }

        return result;
    }

    public static boolean checkParamKey(Map<String, String> params, String keyName, String key) {
        boolean result = false;
        if (params.containsKey("sign")) {
            String sign = (String)params.get("sign");
            params.remove("sign");
            StringBuilder buf = new StringBuilder((params.size() + 1) * 10);
            buildPayParams(buf, params, false);
            String preStr = buf.toString();
            String signRecieve = MD5.sign(preStr, "&" + keyName + "=" + key, "utf-8");
            result = sign.equalsIgnoreCase(signRecieve);
        }

        return result;
    }

    public static boolean checkParamNot(Map<String, String> params, String key) {
        boolean result = false;
        if (params.containsKey("sign")) {
            String sign = (String)params.get("sign");
            params.remove("sign");
            StringBuilder buf = new StringBuilder((params.size() + 1) * 10);
            buildPayParams(buf, params, false);
            String preStr = buf.toString();
            String signRecieve = MD5.sign(preStr, key, "utf-8");
            result = sign.equalsIgnoreCase(signRecieve);
        }

        return result;
    }

    public static boolean checkParamNotObject(Map<String, Object> params, String key) {
        boolean result = false;
        if (params.containsKey("sign")) {
            String sign = (String)params.get("sign");
            params.remove("sign");
            StringBuilder buf = new StringBuilder((params.size() + 1) * 10);
            buildPayParamsObject(buf, params, false);
            String preStr = buf.toString();
            String signRecieve = MD5.sign(preStr, key, "utf-8");
            result = sign.equalsIgnoreCase(signRecieve);
        }

        return result;
    }

    public static boolean checkParamObject(Map<String, Object> params, String keyName, String key) {
        boolean result = false;
        if (params.containsKey("sign")) {
            String sign = (String)params.get("sign");
            params.remove("sign");
            StringBuilder buf = new StringBuilder((params.size() + 1) * 10);
            buildPayParamsObject(buf, params, false);
            String preStr = buf.toString();
            String signRecieve = MD5.sign(preStr, "&" + keyName + "=" + key, "utf-8");
            result = sign.equalsIgnoreCase(signRecieve);
        }

        return result;
    }

    public static Map<String, String> paraFilterSign(Map<String, String> sArray) {
        Map<String, String> result = new HashMap(sArray.size());
        if (sArray != null && sArray.size() > 0) {
            Iterator var2 = sArray.keySet().iterator();

            while(var2.hasNext()) {
                String key = (String)var2.next();
                String value = (String)sArray.get(key);
                if (value != null && !value.equals("")) {
                    result.put(key, value);
                }
            }

            return result;
        } else {
            return result;
        }
    }

    public static Map<String, String> paraFilterObjectSign(Map<String, Object> sArray) {
        Map<String, String> result = new HashMap(sArray.size());
        if (sArray != null && sArray.size() > 0) {
            Iterator<String> var2 = sArray.keySet().iterator();

            while(var2.hasNext()) {
                String key = var2.next();
                if(sArray.get(key) != null){
                    String value = sArray.get(key).toString();
                    if (value != null && !value.equals("")) {
                        result.put(key, value);
                    }
                }
            }
            return result;
        } else {
            return result;
        }
    }

    public static Map<String, String> paraFilter(Map<String, String> sArray) {
        Map<String, String> result = new HashMap(sArray.size());
        if (sArray != null && sArray.size() > 0) {
            Iterator var2 = sArray.keySet().iterator();

            while(var2.hasNext()) {
                String key = (String)var2.next();
                String value = (String)sArray.get(key);
                if (value != null && !value.equals("") && !key.equalsIgnoreCase("sign")) {
                    result.put(key, value);
                }
            }

            return result;
        } else {
            return result;
        }
    }

    public static Map<String, Object> paraFilterObject(Map<String, Object> sArray) {
        Map<String, Object> result = new HashMap(sArray.size());
        if (sArray != null && sArray.size() > 0) {
            Iterator var2 = sArray.keySet().iterator();

            while(var2.hasNext()) {
                String key = var2.next().toString();
                String value = sArray.get(key).toString();
                if (value != null && !value.equals("") && !key.equalsIgnoreCase("sign")) {
                    result.put(key, value);
                }
            }

            return result;
        } else {
            return result;
        }
    }

    public static String payParamsToString(Map<String, String> payParams) {
        return payParamsToString(payParams, false);
    }

    public static String payParamsToString(Map<String, String> payParams, boolean encoding) {
        return payParamsToString(new StringBuilder(), payParams, encoding);
    }

    public static String payParamsToString(StringBuilder sb, Map<String, String> payParams, boolean encoding) {
        buildPayParams(sb, payParams, encoding);
        return sb.toString();
    }

    /**
     * @author
     * @param payParams
     * @return
     */
    public static void buildPayParamsOld(StringBuilder sb,Map<String, String> payParams,boolean encoding){
        List<String> keys = new ArrayList<String>(payParams.keySet());
        Collections.sort(keys);
        for(String key : keys){
            if(StringUtils.isEmpty(payParams.get(key))){
                continue;
            }
            sb.append(key).append("=");
            if(encoding){
                sb.append(urlEncode(payParams.get(key)));
            }else{
                sb.append(payParams.get(key));
            }
            sb.append("&");
        }
        sb.setLength(sb.length() - 1);
    }

    public static void buildPayParams(StringBuilder sb, Map<String, String> payParams, boolean encoding) {
        List<String> keys = new ArrayList(payParams.keySet());
        Collections.sort(keys);

        for(Iterator var4 = keys.iterator(); var4.hasNext(); sb.append("&")) {
            String key = (String)var4.next();
            if(StringUtils.isEmpty(payParams.get(key))){
                continue;
            }
            sb.append(key).append("=");
            if (encoding) {
                sb.append(urlEncode((String)payParams.get(key)));
            } else {
                sb.append((String)payParams.get(key));
            }
        }

        sb.setLength(sb.length() - 1);
    }

    public static void buildPayParamsObject(StringBuilder sb, Map<String, Object> payParams, boolean encoding) {
        List<String> keys = new ArrayList(payParams.keySet());
        Collections.sort(keys);

        for(Iterator var4 = keys.iterator(); var4.hasNext(); sb.append("&")) {
            String key = (String)var4.next();
            sb.append(key).append("=");
            if (encoding) {
                sb.append(urlEncode(payParams.get(key).toString()));
            } else {
                sb.append(payParams.get(key).toString());
            }
        }

        sb.setLength(sb.length() - 1);
    }

    public static String urlEncode(String str) {
        try {
            return URLEncoder.encode(str, "UTF-8");
        } catch (Throwable var2) {
            return str;
        }
    }

    public static Element readerXml(String body, String encode) throws DocumentException {
        SAXReader reader = new SAXReader(false);
        InputSource source = new InputSource(new StringReader(body));
        source.setEncoding(encode);
        Document doc = reader.read(source);
        Element element = doc.getRootElement();
        return element;
    }

    public static void main(String[] args) {
        String resultStr = "{\"transaction_id\":\"20230315002838545452\",\"amount\":\"100.0000\",\"datetime\":\"20230315003947\",\"orderid\":\"P01202303150028375450000\",\"returncode\":\"00\",\"sign\":\"BAF79ABCE136E6967A432FE72C8C4D04\",\"attach\":\"\",\"memberid\":\"10127\"}";
        Map<String, String> params =  JSONObject.parseObject(resultStr, Map.class);

        if (!SignUtils.checkParam(params, "gvj63m2vzv7txe7r6cu4g3b5f06nyvao")) {
            System.out.println("111");
        }
    }
}
