package com.jeequan.jeepay.thirdparty.model;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.model.oauth2.WxpayOauth2Params;
import com.jeequan.jeepay.core.model.params.wxpay.WxpayIsvParams;
import com.jeequan.jeepay.core.model.params.wxpay.WxpayNormalMchParams;
import com.jeequan.jeepay.core.utils.SpringBeansUtil;
import com.jeequan.jeepay.thirdparty.util.ChannelCertConfigKitBean;
import lombok.AllArgsConstructor;
import lombok.Data;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.apache.commons.lang3.StringUtils;

/**
 * wxService 包装类
 *
 * @author terrfly
 * @date 2021/6/8 17:30
 */
@Data
@AllArgsConstructor
public class WxServiceWrapper {

    /** 缓存微信API版本 **/
    private String apiVersion;

    /** 缓存 wxPayService 对象 **/
    private WxPayService wxPayService;

    /** 缓存 wxJavaService 对象 **/
    private WxMpService wxMpService;

    /** 缓存 wxJavaService 小程序 对象 **/
    private WxMaService wxMaService;


    public static WxServiceWrapper buildWxServiceWrapper(String mchId, String appId, String mchKey, String apiVersion, String apiV3Key,
                                                         String serialNo, String cert, String apiClientCert, String apiClientKey, WxpayOauth2Params wxpayOauth2Params){

        WxPayConfig wxPayConfig = new WxPayConfig();
        wxPayConfig.setMchId(mchId);
        wxPayConfig.setAppId(appId);
        wxPayConfig.setMchKey(mchKey);

        if (CS.PAY_IF_VERSION.WX_V2.equals(apiVersion)) { // 微信API  V2
            wxPayConfig.setSignType(WxPayConstants.SignType.MD5);
        }

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

        if(StringUtils.isNotBlank(apiV3Key)) {
            wxPayConfig.setApiV3Key(apiV3Key);
        }
        if(StringUtils.isNotBlank(serialNo)) {
            wxPayConfig.setCertSerialNo(serialNo);
        }
        if(StringUtils.isNotBlank(cert)){
            wxPayConfig.setKeyPath(channelCertConfigKitBean.getCertFilePath(cert));
        }
        if(StringUtils.isNotBlank(apiClientCert)){
            wxPayConfig.setPrivateCertPath(channelCertConfigKitBean.getCertFilePath(apiClientCert));
        }
        if(StringUtils.isNotBlank(apiClientKey)) {
            wxPayConfig.setPrivateKeyPath(channelCertConfigKitBean.getCertFilePath(apiClientKey));
        }

        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(wxPayConfig); //微信配置信息

        return new WxServiceWrapper(apiVersion, wxPayService, null, null).putOauthInfo(wxpayOauth2Params);
    }

    private WxServiceWrapper putOauthInfo(WxpayOauth2Params wxpayOauth2Params){

        if(wxpayOauth2Params != null){
            // 当 公众号的配置不为空时设置 wxMpService
            if(StringUtils.isNotEmpty(wxpayOauth2Params.getAppId()) && StringUtils.isNotEmpty(wxpayOauth2Params.getAppSecret())){
                WxMpDefaultConfigImpl wxMpConfigStorage = new WxMpDefaultConfigImpl();
                wxMpConfigStorage.setAppId(wxpayOauth2Params.getAppId());
                wxMpConfigStorage.setSecret(wxpayOauth2Params.getAppSecret());
                this.wxMpService = new WxMpServiceImpl();
                this.wxMpService.setWxMpConfigStorage(wxMpConfigStorage); //微信配置信息
            }

            // 当小程序的配置不为空时设置 wxMaService
            if(StringUtils.isNotEmpty(wxpayOauth2Params.getLiteAppId()) && StringUtils.isNotEmpty(wxpayOauth2Params.getLiteAppSecret())){
                WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
                config.setAppid(wxpayOauth2Params.getLiteAppId());
                config.setSecret(wxpayOauth2Params.getLiteAppSecret());
                this.wxMaService = new WxMaServiceImpl();
                this.wxMaService.setWxMaConfig(config);
            }
        }
        return this;
    }


    public static WxServiceWrapper buildWxServiceWrapper(WxpayIsvParams wxpayParams, WxpayOauth2Params wxpayOauth2Params){

        if(wxpayParams != null){
            return buildWxServiceWrapper(wxpayParams.getMchId(), wxpayParams.getAppId(),
                    wxpayParams.getKey(), wxpayParams.getApiVersion(), wxpayParams.getApiV3Key(),
                    wxpayParams.getSerialNo(), wxpayParams.getCert(), wxpayParams.getApiClientCert(), wxpayParams.getApiClientKey(),
                    wxpayOauth2Params);
        }

        return new WxServiceWrapper(null, null, null, null).putOauthInfo(wxpayOauth2Params);


    }

    public static WxServiceWrapper buildWxServiceWrapper(WxpayNormalMchParams wxpayParams, WxpayOauth2Params wxpayOauth2Params){

        if(wxpayParams != null){
            return buildWxServiceWrapper(wxpayParams.getMchId(), wxpayParams.getAppId(),
                    wxpayParams.getKey(), wxpayParams.getApiVersion(), wxpayParams.getApiV3Key(),
                    wxpayParams.getSerialNo(), wxpayParams.getCert(), wxpayParams.getApiClientCert(), wxpayParams.getApiClientKey(),
                    wxpayOauth2Params);
        }

        return new WxServiceWrapper(null, null, null, null).putOauthInfo(wxpayOauth2Params);
    }

}
