package com.ssyf.common.security.helper;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONUtil;
import com.ssyf.common.security.domain.Certificate;
import com.ssyf.common.security.holder.CertificateSecretHandlerHolder;


/**
 * 证书工具
 * @author huwei
 * @date 2023/9/24 15:51
 */
public class CertificateHelper {

    /**
     * 获取秘钥
     * @return
     */
    public static byte[] getSecret(){
        return CertificateSecretHandlerHolder.holder().getSecret(ClientContextHelper.getClientInfo()).getBytes();
    }

    /**
     * 生成证书
     * @param certificate
     * @return
     */
    public static String generate(Certificate certificate){
        return generate(getSecret() ,certificate);
    }

    /**
     * 生成证书
     * @param secret
     * @param certificate
     * @return
     */
    public static String generate(byte[] secret , Certificate certificate){
        return SecureUtil.aes(secret).encryptBase64(JSONUtil.toJsonStr(certificate));
    }

    /**
     * 解析证书并获取证书对象
     * @param certificate
     * @return
     * @param <T>
     */
    public static <T> Certificate<T> parse(String certificate){
        return parse(getSecret() ,certificate);
    }

    /**
     * 解析证书并获取证书对象
     * @param secret
     * @param certificate
     * @param <T>
     * @return
     */
    public static <T> Certificate<T> parse(byte[] secret , String certificate){
        if(secret == null || StrUtil.isBlank(certificate)){
            return null;
        }
        try{
            String json = SecureUtil.aes(secret).decryptStr(certificate);
            Certificate<T> result = JSONUtil.toBean(json , Certificate.class);
            return result;
        }catch (Exception e){
            //解析失败，忽略错误，返回null
        }
        return null;
    }

    /**
     * 是否过期
     * @param certificate
     * @return
     */
    public static boolean isTimeout(Certificate certificate){
        return certificate == null || System.currentTimeMillis() > certificate.getEndTimeMillis();
    }

    /**
     * 校验证书
     * @param certificate
     * @return
     */
    public static boolean valid(String certificate){
        return valid(parse(getSecret(),certificate));
    }

    /**
     * 校验证书
     * @param secret
     * @param certificate
     * @return
     */
    public static boolean valid(String secret ,String certificate){
        return valid(parse(secret.getBytes() ,certificate));
    }

    /**
     * 校验证书
     * @param certificate
     * @return
     */
    public static boolean valid(Certificate certificate){
        return certificate != null && !isTimeout(certificate);
    }
    /**
     * 校验证书
     * @param secret
     * @param source
     * @param certificateStr
     * @return
     */
    public static boolean valid(String secret ,Certificate source ,String certificateStr){
        return valid(source ,parse(secret.getBytes() ,certificateStr));
    }

    /**
     * 校验证书
     * @param source
     * @param target
     * @return
     */
    public static boolean valid(Certificate source , Certificate target){
        //有效性
        if(!valid(target)){
            return false;
        }
        if(StrUtil.isNotBlank(source.getEvent()) && !source.getEvent().equals(target.getEvent())){
            return false;
        }
        if(StrUtil.isNotBlank(source.getType()) && !source.getType().equals(target.getType())){
            return false;
        }
        if(source.getContent() != null && StrUtil.isNotBlank(source.getContent().toString()) && !source.getContent().equals(target.getContent())){
            return false;
        }
        return true;
    }
}
