package com.jeequan.jeepay.thirdparty.channel.ybpay.model;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.SM2;
import com.jeequan.jeepay.core.model.params.ybpay.YbpayIsvParams;
import lombok.Getter;
import lombok.Setter;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 * 易宝 请求头参数
 * @author crystal
 * @date 2024/4/26 14:17
 */
@Getter
@Setter
public class ReqHeader {

    public static final String SECURITY_REQ = "YOP-RSA2048-SHA256";

    public static final String AUTH_STR = "yop-auth-v3";

    public static final String CONTENT_TYPE = "application/x-www-form-urlencoded; charset=UTF-8";

    public static final String DEFAULT_VERSION = "4.0.0";

    public static final String SIGN_HEADER = "content-type;x-yop-appkey;x-yop-content-sha256;x-yop-request-id";

    /**
     * 鉴权认证串 Authorization
     */
    private String authorization;

    /**
     * 请求唯一流水号
     * x-yop-request-id
     */
    private String requestId;

    /**
     * 请求appid
     * x-yop-appkey
     */
    private String appid;

    /**
     * 请求content-type
     * Content-Type
     */
    private String contentType;

    /**
     * 语言(如：java/python/go)
     * x-yop-sdk-langs
     */
    private String language;

    /**
     * 自实现目前统一使用 4.0.0
     */
    private String version;

    /**
     * 摘要
     */
    private String keyContent;


    public ReqHeader(YbpayIsvParams isvParams) {
        this.requestId = IdUtil.fastUUID();
        this.appid = isvParams.getAppId();
        this.contentType = CONTENT_TYPE;
        this.version = DEFAULT_VERSION;
        this.keyContent = isvParams.getKeyContent();
        this.language = "java";
    }

    public Map<String, String> toHeader(YbpayIsvParams isvParams,ReqMethod.Method method) {
        Map<String, String> headers = new HashMap<>(6);
        headers.put("x-yop-request-id",this.getRequestId());
        headers.put("x-yop-appkey",this.getAppid());
        headers.put("Content-Type",this.getContentType());
        headers.put("x-yop-sdk-langs",this.getLanguage());
        headers.put("x-yop-sdk-version",this.getVersion());
        headers.put("x-yop-content-sm3",this.getKeyContent());
        String authString = AUTH_STR + "/" + this.getAppid() + "/" + DateUtil.format(new DateTime(), "yyyy-MM-dd'T'HH:mm:ss'Z'") + "/1800";
        String Authorization = SECURITY_REQ + " " + authString + "/" + SIGN_HEADER + "/" + genSign(isvParams,authString,method);
        headers.put("Authorization",Authorization);
        return headers;
    }

    /**
     *
     * @param isvParams
     * @param authString
     * @param method
     * @return
     */
    private String genSign(YbpayIsvParams isvParams,String authString,ReqMethod.Method method) {
        SM2 sm2 = SmUtil.sm2(isvParams.getPrivateKey(), isvParams.getPublicKey());
        String signContent = authString + "\n" + method.getReqType() + "\n" + method.getMethod() + "\n" ;
        return Base64.getEncoder().encodeToString(sm2.sign(signContent.getBytes(StandardCharsets.UTF_8)));
    }
}
