package com.jeequan.jeepay.thirdparty.model;

import com.alipay.api.*;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.oauth2.AlipayOauth2Params;
import com.jeequan.jeepay.core.model.params.alipay.AlipayConfig;
import com.jeequan.jeepay.core.model.params.alipay.AlipayIsvParams;
import com.jeequan.jeepay.core.model.params.alipay.AlipayNormalMchParams;
import com.jeequan.jeepay.core.utils.SpringBeansUtil;
import com.jeequan.jeepay.thirdparty.util.ChannelCertConfigKitBean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

 /*
* 支付宝Client 包装类
*
* @author terrfly
* @date 2021/6/8 17:28
*/
@Slf4j
@Data
@AllArgsConstructor
public class AlipayClientWrapper {

    /** 缓存支付宝client对象 (支付使用) **/
    private AlipayClientEntry alipayClient;

     /** 缓存支付宝client对象 (服务窗获取userId) **/
     private AlipayClientEntry alipayClientByOauth2;

     /** 缓存支付宝client对象 (小程序获取userId ) **/
     private AlipayClientEntry alipayClientByOauth2Lite;


    /*
     * 构建支付宝client 包装类
     *
     * @author terrfly
     * @date 2021/6/8 17:46
     */
    public static AlipayClientEntry buildAlipayClientWrapper(Byte useCert, Byte sandbox, String appId,
                                                               String privateKey, String alipayPublicKey, String signType,
                                                               String appCert, String alipayPublicCert, String alipayRootCert){

        //避免空值
        sandbox = sandbox == null ? CS.NO : sandbox;

        AlipayClient alipayClient = null;
        if(useCert != null && useCert == CS.YES){ //证书的方式

            ChannelCertConfigKitBean channelCertConfigKitBean = SpringBeansUtil.getBean(ChannelCertConfigKitBean.class);

            CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
            certAlipayRequest.setServerUrl(sandbox == CS.YES ? com.jeequan.jeepay.core.model.params.alipay.AlipayConfig.SANDBOX_SERVER_URL : com.jeequan.jeepay.core.model.params.alipay.AlipayConfig.PROD_SERVER_URL);
            certAlipayRequest.setAppId(appId);
            certAlipayRequest.setPrivateKey(privateKey);
            certAlipayRequest.setFormat(com.jeequan.jeepay.core.model.params.alipay.AlipayConfig.FORMAT);
            certAlipayRequest.setCharset(com.jeequan.jeepay.core.model.params.alipay.AlipayConfig.CHARSET);
            certAlipayRequest.setSignType(signType);

            certAlipayRequest.setCertPath(channelCertConfigKitBean.getCertFilePath(appCert));
            certAlipayRequest.setAlipayPublicCertPath(channelCertConfigKitBean.getCertFilePath(alipayPublicCert));
            certAlipayRequest.setRootCertPath(channelCertConfigKitBean.getCertFilePath(alipayRootCert));
            try {
                alipayClient = new DefaultAlipayClient(certAlipayRequest);
            } catch (AlipayApiException e) {
                log.error("error" ,e);
                alipayClient = null;
            }
        }else{
            alipayClient = new DefaultAlipayClient(sandbox == CS.YES ? com.jeequan.jeepay.core.model.params.alipay.AlipayConfig.SANDBOX_SERVER_URL : com.jeequan.jeepay.core.model.params.alipay.AlipayConfig.PROD_SERVER_URL
                    , appId, privateKey, com.jeequan.jeepay.core.model.params.alipay.AlipayConfig.FORMAT, AlipayConfig.CHARSET,
                    alipayPublicKey, signType);
        }

        return new AlipayClientEntry(useCert, alipayClient);
    }

    public static AlipayClientWrapper buildAlipayClientWrapper(AlipayIsvParams alipayParams, AlipayOauth2Params alipayOauth2Params){

        AlipayClientEntry clientByPay = null;
        AlipayClientEntry clientByOauth2 = null;
        AlipayClientEntry clientByOauth2Lite = null;

        if(alipayParams != null){
            clientByPay = buildAlipayClientWrapper(
                    alipayParams.getUseCert(), alipayParams.getSandbox(), alipayParams.getAppId(), alipayParams.getPrivateKey(),
                    alipayParams.getAlipayPublicKey(), alipayParams.getSignType(), alipayParams.getAppPublicCert(),
                    alipayParams.getAlipayPublicCert(), alipayParams.getAlipayRootCert()
            );
        }

        if(alipayOauth2Params != null && StringUtils.isNotEmpty(alipayOauth2Params.getAppId())){
            clientByOauth2 = buildAlipayClientWrapper(
                    alipayOauth2Params.getUseCert(), alipayOauth2Params.getSandbox(), alipayOauth2Params.getAppId(), alipayOauth2Params.getPrivateKey(),
                    alipayOauth2Params.getAlipayPublicKey(), alipayOauth2Params.getSignType(), alipayOauth2Params.getAppPublicCert(),
                    alipayOauth2Params.getAlipayPublicCert(), alipayOauth2Params.getAlipayRootCert()
            );
        }

        if(alipayOauth2Params != null && alipayOauth2Params.getLiteParams() != null && StringUtils.isNotEmpty(alipayOauth2Params.getLiteParams().getAppId())){
            clientByOauth2Lite = buildAlipayClientWrapper(
                    alipayOauth2Params.getLiteParams().getUseCert(), alipayOauth2Params.getLiteParams().getSandbox(), alipayOauth2Params.getLiteParams().getAppId(), alipayOauth2Params.getLiteParams().getPrivateKey(),
                    alipayOauth2Params.getLiteParams().getAlipayPublicKey(), alipayOauth2Params.getLiteParams().getSignType(), alipayOauth2Params.getLiteParams().getAppPublicCert(),
                    alipayOauth2Params.getLiteParams().getAlipayPublicCert(), alipayOauth2Params.getLiteParams().getAlipayRootCert()
            );
        }

        return new AlipayClientWrapper(clientByPay, clientByOauth2, clientByOauth2Lite);
    }

     public static AlipayClientWrapper buildAlipayClientWrapper(AlipayNormalMchParams alipayParams, AlipayOauth2Params alipayOauth2Params){

         AlipayClientEntry clientByPay = null;
         AlipayClientEntry clientByOauth2 = null;
         AlipayClientEntry clientByOauth2Lite = null;

         if(alipayParams != null){
             clientByPay = buildAlipayClientWrapper(
                     alipayParams.getUseCert(), alipayParams.getSandbox(), alipayParams.getAppId(), alipayParams.getPrivateKey(),
                     alipayParams.getAlipayPublicKey(), alipayParams.getSignType(), alipayParams.getAppPublicCert(),
                     alipayParams.getAlipayPublicCert(), alipayParams.getAlipayRootCert()
             );
         }

         if(alipayOauth2Params != null && StringUtils.isNotEmpty(alipayOauth2Params.getAppId())){
             clientByOauth2 = buildAlipayClientWrapper(
                     alipayOauth2Params.getUseCert(), alipayOauth2Params.getSandbox(), alipayOauth2Params.getAppId(), alipayOauth2Params.getPrivateKey(),
                     alipayOauth2Params.getAlipayPublicKey(), alipayOauth2Params.getSignType(), alipayOauth2Params.getAppPublicCert(),
                     alipayOauth2Params.getAlipayPublicCert(), alipayOauth2Params.getAlipayRootCert()
             );
         }

         if(alipayOauth2Params != null && alipayOauth2Params.getLiteParams() != null && StringUtils.isNotEmpty(alipayOauth2Params.getLiteParams().getAppId())){
             clientByOauth2Lite = buildAlipayClientWrapper(
                     alipayOauth2Params.getLiteParams().getUseCert(), alipayOauth2Params.getLiteParams().getSandbox(), alipayOauth2Params.getLiteParams().getAppId(), alipayOauth2Params.getLiteParams().getPrivateKey(),
                     alipayOauth2Params.getLiteParams().getAlipayPublicKey(), alipayOauth2Params.getLiteParams().getSignType(), alipayOauth2Params.getLiteParams().getAppPublicCert(),
                     alipayOauth2Params.getLiteParams().getAlipayPublicCert(), alipayOauth2Params.getLiteParams().getAlipayRootCert()
             );
         }

         return new AlipayClientWrapper(clientByPay, clientByOauth2, clientByOauth2Lite);
     }



     /** 封装支付宝接口调用函数  --> 默认调用 pay的 **/
     public <T extends AlipayResponse> T execute(AlipayRequest<T> request){

         if(this.alipayClient == null){
             throw new BizException("获取支付宝client失败！");
         }

         return this.alipayClient.execute(request);
     }
































 }
