package cn.anicert.secure.access.core;

import cn.anicert.secure.access.bean.SAResponseVO;
import cn.anicert.secure.access.constants.InterfaceConstant;
import cn.anicert.secure.access.utils.ConfigUtil;
import cn.anicert.secure.access.utils.HttpUtil;
import cn.anicert.secure.access.utils.JSONUtil;
import cn.anicert.secure.access.utils.SignUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.StaticLog;

public class SecureAccess {

    public static void init(String... configPath) {
        ConfigUtil.initConfig(configPath);
    }

    public static String unified_auth(String bizSeq, String reqStr) {
        String url = ConfigUtil.getConfig().getAccessUrl() + InterfaceConstant.UNIFIED_AUTH_URL;
        String responseStr = sendReq(bizSeq, url, reqStr);
        if (StrUtil.isNotBlank(responseStr) && verifySign(bizSeq, responseStr)) {
            return dealResult(bizSeq, responseStr);
        }
        return "";
    }

    public static String pkcs7Sign(String bizSeq, String reqStr) {
        String url = ConfigUtil.getConfig().getAccessUrl() + InterfaceConstant.P7_SIGN_URL;
        String responseStr = sendReq(bizSeq, url, reqStr);
        if (StrUtil.isNotBlank(responseStr) && verifySign(bizSeq, responseStr)) {
            return dealResult(bizSeq, responseStr);
        }
        return "";
    }
    

    private static String sendReq(String bizSeq, String url, String reqStr) {
        try {
            StaticLog.info("[安全接入平台],bizSeq--[{}]--请求开始", bizSeq);
            StaticLog.info("[安全接入平台],bizSeq--[{}]--请求数据--[{}]", bizSeq, reqStr);
            String responseStr = HttpUtil.getInstance().postRequest(url, reqStr);
            if (StrUtil.isNotBlank(responseStr)) {
                StaticLog.info("[安全接入平台],bizSeq--[{}]--请求结果--[{}]", bizSeq, responseStr);
                return responseStr;
            } else {
                StaticLog.error("[安全接入平台],bizSeq--[{}]--请求返回结果为空", bizSeq);
            }
        } catch (Exception e) {
            StaticLog.error(e, "[安全接入平台],bizSeq--[{}]--请求失败", bizSeq);
        }
        return "";
    }

    private static boolean verifySign(String bizSeq, String result) {
        try {
            SAResponseVO responseVO = JSONUtil.json2Object(result, SAResponseVO.class);
            if (StrUtil.isNotBlank(responseVO.getResponseSign())) {
                String signStr = JSONUtil.toJson(responseVO.getResponsePackage());
                boolean verifyResult = SignUtil.verifySign(signStr, responseVO.getResponseSign());
                if (!verifyResult) {
                    StaticLog.error("[安全接入平台],bizSeq--[{}]--验签不匹配", bizSeq);
                }
                return verifyResult;
            } else {
                StaticLog.error("[安全接入平台],bizSeq--[{}]--验签报文体为空", bizSeq);
                return false;
            }
        } catch (Exception e) {
            StaticLog.error(e, "[安全接入平台],bizSeq--[{}]--验签异常", bizSeq);
            return false;
        }
    }

    private static String dealResult(String bizSeq, String result) {
        try {
            SAResponseVO responseVO = JSONUtil.json2Object(result, SAResponseVO.class);
            return JSONUtil.toJson(responseVO.getResponsePackage());
        } catch (Exception e) {
            StaticLog.error(e, "[安全接入平台],bizSeq--[{}]--处理返回报文异常", bizSeq);
            return "";
        }
    }

}
