package com.qmth.paper.library.common;

import com.qmth.boot.tools.signature.SignatureType;
import com.qmth.paper.library.common.util.Base64Util;
import com.qmth.paper.library.common.util.ShaUtils;
import org.apache.commons.lang3.StringUtils;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

public class SignatureEntityTest {

    private static final String PATTERN = "{0} {1}{2}{3}";

    public static final String FIELD_JOINER = ":";

    public static final String PARAM_JOINER = "&";

    private static Map<String, SignatureType> typeMap = new HashMap<>();

    static {
        for (SignatureType type : SignatureType.values()) {
            typeMap.put(type.getName(), type);
        }
    }

    private SignatureType type;

    private String method;

    private String uri;

    private long timestamp;

    private String ciphertext;

    private String invoker;

    private String secret;

    private SignatureEntityTest() {
    }

    public SignatureType getType() {
        return type;
    }

    private void setType(SignatureType type) {
        this.type = type;
    }

    public String getMethod() {
        return method;
    }

    private void setMethod(String method) {
        this.method = method;
    }

    public String getUri() {
        return uri;
    }

    private void setUri(String uri) {
        this.uri = uri;
    }

    public long getTimestamp() {
        return timestamp;
    }

    private void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    private String getCiphertext() {
        return ciphertext;
    }

    private void setCiphertext(String ciphertext) {
        this.ciphertext = ciphertext;
    }

    public String getInvoker() {
        return invoker;
    }

    private void setInvoker(String invoker) {
        this.invoker = invoker;
    }

    public String getSecret() {
        return secret;
    }

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

    public static String encrypt(String... values) {
        return Base64Util.encode(ShaUtils.sha1(StringUtils.join(values, PARAM_JOINER)));
    }

//    /**
//     * 基于解析好的签名对象，使用传入的保密信息进行签名内容验证
//     *
//     * @param secret
//     * @return
//     */
//    public boolean validate(String secret) {
//        if (method != null && uri != null && timestamp >= 0 && secret != null && ciphertext != null) {
//            return encrypt(method, uri, String.valueOf(timestamp), secret).equals(ciphertext);
//        }
//        return false;
//    }

    /**
     * 基于解析好的签名对象，使用传入的保密信息进行签名内容验证(测试用)
     *
     * @param secret
     * @return
     */
    public boolean validate(String secret) {
        if (secret != null && ciphertext != null) {
            return encrypt(secret).equals(ciphertext);
        }
        return false;
    }

    /**
     * 根据标准参数构造最终的签名字符串
     *
     * @param type
     * @param method
     * @param uri
     * @param timestamp
     * @param invoker
     * @param secret
     * @return
     */
    public static String build(SignatureType type, String method, String uri, long timestamp, String invoker, String secret) {
        if (type == null || method == null || uri == null || timestamp <= 0 || invoker == null || secret == null) {
            return "";
        }
        return MessageFormat.format(PATTERN, type.getName(), invoker, FIELD_JOINER,
                encrypt(method.toLowerCase(), uri, String.valueOf(timestamp), secret));
    }

    /**
     * 根据标准参数构造最终的签名字符串(测试用)
     *
     * @param type
     * @param invoker
     * @param secret
     * @return
     */
    public static String build(SignatureType type, String invoker, String secret) {
        if (type == null || invoker == null || secret == null) {
            return "";
        }
        return MessageFormat.format(PATTERN, type.getName(), invoker, FIELD_JOINER,
                encrypt(secret));
    }

    /**
     * 根据当前接口的的基本信息、header中的时间戳与签名字符串，尝试解析并构造签名数据对象
     *
     * @param method
     * @param uri
     * @param timestamp
     * @param signature
     * @return
     */
    public static SignatureEntityTest parse(String method, String uri, long timestamp, String signature) {
        if (method == null || uri == null || timestamp <= 0 || signature == null) {
            return null;
        }
        String[] values = StringUtils.split(signature);
        if (values != null && values.length == 2) {
            SignatureType type = typeMap.get(values[0]);
            if (type != null) {
                String[] array = StringUtils.split(values[1], FIELD_JOINER);
                if (array != null && array.length == 2) {
                    SignatureEntityTest info = new SignatureEntityTest();
                    info.setType(type);
                    info.setMethod(method.toLowerCase());
                    info.setUri(uri);
                    info.setTimestamp(timestamp);
                    info.setInvoker(array[0]);
                    info.setCiphertext(array[1]);
                    return info;
                }
            }
        }
        return null;
    }

    /**
     * 根据当前接口的的基本信息、header中的时间戳与签名字符串，尝试解析并构造签名数据对象(测试用)
     *
     * @param signature
     * @return
     */
    public static SignatureEntityTest parse(String signature) {
        if (signature == null) {
            return null;
        }
        String[] values = StringUtils.split(signature);
        if (values != null && values.length == 2) {
            SignatureType type = typeMap.get(values[0]);
            if (type != null) {
                String[] array = StringUtils.split(values[1], FIELD_JOINER);
                if (array != null && array.length == 2) {
                    SignatureEntityTest info = new SignatureEntityTest();
                    info.setType(type);
//                    info.setMethod(method.toLowerCase());
//                    info.setUri(uri);
//                    info.setTimestamp(timestamp);
                    info.setInvoker(array[0]);
                    info.setCiphertext(array[1]);
                    return info;
                }
            }
        }
        return null;
    }
}
