package org.springblade.openinterface.facedevice.qinlin.util;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;

import java.util.*;

import static cn.hutool.crypto.SecureUtil.md5;

/**
 * @author zwx
 * @create 2021/1/6 10:16
 */
public class QinLinSignUtil {

    private QinLinSignUtil() {

    }

    public static class Signing {
        private String version;
        private Integer nonce;
        private Long timestamp;
        private String appid;
        private String secret;
        private String body;

        Signing(final String version, final Integer nonce, final Long timestamp, final String appid, final String secret, final String body) {
            this.version = version;
            this.nonce = nonce;
            this.timestamp = timestamp;
            this.appid = appid;
            this.secret = secret;
            this.body = body;
        }

        public static QinLinSignUtil.Signing.SigningBuilder builder() {
            return new QinLinSignUtil.Signing.SigningBuilder();
        }

        public String getVersion() {
            return this.version;
        }

        public Integer getNonce() {
            return this.nonce;
        }

        public Long getTimestamp() {
            return this.timestamp;
        }

        public String getAppid() {
            return this.appid;
        }

        public String getSecret() {
            return this.secret;
        }

        public String getBody() {
            return this.body;
        }

        public void setVersion(final String version) {
            this.version = version;
        }

        public void setNonce(final Integer nonce) {
            this.nonce = nonce;
        }

        public void setTimestamp(final Long timestamp) {
            this.timestamp = timestamp;
        }

        public void setAppid(final String appid) {
            this.appid = appid;
        }

        public void setSecret(final String secret) {
            this.secret = secret;
        }

        public void setBody(final String body) {
            this.body = body;
        }

        public String toString() {
            return "QinLinSignUtil.Signing(version=" + this.getVersion() + ", nonce=" + this.getNonce() + ", timestamp=" + this.getTimestamp() + ", appid=" + this.getAppid() + ", secret=" + this.getSecret() + ", body=" + this.getBody() + ")";
        }

        public static class SigningBuilder {
            private String version;
            private Integer nonce;
            private Long timestamp;
            private String appid;
            private String secret;
            private String body;

            SigningBuilder() {
            }

            public QinLinSignUtil.Signing.SigningBuilder version(final String version) {
                this.version = version;
                return this;
            }

            public QinLinSignUtil.Signing.SigningBuilder nonce(final Integer nonce) {
                this.nonce = nonce;
                return this;
            }

            public QinLinSignUtil.Signing.SigningBuilder timestamp(final Long timestamp) {
                this.timestamp = timestamp;
                return this;
            }

            public QinLinSignUtil.Signing.SigningBuilder appid(final String appid) {
                this.appid = appid;
                return this;
            }

            public QinLinSignUtil.Signing.SigningBuilder secret(final String secret) {
                this.secret = secret;
                return this;
            }

            public QinLinSignUtil.Signing.SigningBuilder body(final String body) {
                this.body = body;
                return this;
            }

            public QinLinSignUtil.Signing build() {
                return new QinLinSignUtil.Signing(this.version, this.nonce, this.timestamp, this.appid, this.secret, this.body);
            }

            public String toString() {
                return "QinLinSignUtil.Signing.SigningBuilder(version=" + this.version + ", nonce=" + this.nonce + ", timestamp=" + this.timestamp + ", appid=" + this.appid + ", secret=" + this.secret + ", body=" + this.body + ")";
            }
        }
    }

    public static String generateSign(QinLinSignUtil.Signing signing){
        Map<String, String> paramterMap = new HashMap();
        paramterMap.put("version", signing.getVersion());
        paramterMap.put("nonce", signing.getNonce().toString());
        paramterMap.put("timestamp", signing.getTimestamp().toString());
        paramterMap.put("accessKeyId", signing.getAppid());
        if (JSONUtil.isJsonArray(signing.getBody())) {
            paramterMap.put("array", signing.getBody());
        } else {
            JSONObject jsonObj = JSON.parseObject(signing.getBody(), new Feature[]{Feature.OrderedField});
            Iterator var3 = jsonObj.entrySet().iterator();

            while(var3.hasNext()) {
                Object map = var3.next();
                String key = ((Map.Entry)map).getKey().toString();
                if (null != ((Map.Entry)map).getValue()) {
                    String value = ((Map.Entry)map).getValue().toString();
                    paramterMap.put(key, value);
                }
            }
        }

        Set<String> keySet = paramterMap.keySet();
        String[] keyArray = (String[])keySet.toArray(new String[keySet.size()]);
        Arrays.sort(keyArray);
        StringBuilder sb = new StringBuilder();
        String[] var12 = keyArray;
        int var13 = keyArray.length;

        for(int var7 = 0; var7 < var13; ++var7) {
            String k = var12[var7];
            if (((String)paramterMap.get(k)).trim().length() > 0) {
                sb.append(k).append("=").append(((String)paramterMap.get(k)).trim()).append("&");
            }
        }

        sb.append("key=").append(signing.getSecret());
        String signStr = md5(sb.toString()).toUpperCase();
        System.out.println("stringSignTemp   " + sb.toString());
        System.out.println("sign   " + signStr);
        return signStr;
    }

    /**
     * 构建stringSignTemp
     * @param params
     * @return
     */
    public static String createLinkString(Map<String, String> params){
        List<String> keys = new ArrayList(params.keySet());
        Collections.sort(keys);
        StringBuilder content = new StringBuilder();
        for(int i = 0; i < keys.size(); ++i) {
            String key = keys.get(i);
            String value = params.get(key);
            if (i == keys.size() - 1) {
                content.append(key).append("=").append(value);
            } else {
                content.append(key).append("=").append(value).append("&");
            }
        }
        return content.toString();
    }
}
