package com.yonge.toolset.payment.original.wx.config;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.yonge.toolset.payment.base.enums.OpenEnum;
import com.yonge.toolset.payment.base.enums.PaymentClientEnum;
import com.yonge.toolset.payment.core.service.SysConfigPaymentService;
import com.yonge.toolset.payment.original.wx.constant.WxpayConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.security.PrivateKey;


/**
 * @Author: liweifan
 * @Data: 2022/5/12 10:12
 */
@Configuration
public class WxpayConfiguration {
    @Autowired
    private SysConfigPaymentService configPaymentService;
    @Value("${spring.profiles.active}")
    private String profile;

    @Bean(name = "studentWxService")
    public WxPayService studentWxService() {
        String merchantId = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, WxpayConstant.WX_MERCHANT_ID).getParamValue();
        String apiV3Key = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, WxpayConstant.WX_API_V3_KEY).getParamValue();

        String merchantSerialNumber = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, WxpayConstant.WX_MERCHANT_SERIAL_NUMBER).getParamValue();
        String merchantPrivateKey = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, WxpayConstant.WX_MERCHANT_PRIVATE_KEY).getParamValue();

        String privateCertPath = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, WxpayConstant.WX_PRIVATE_CERT_PATH).getParamValue();
        String privateKeyPath = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, WxpayConstant.WX_PRIVATE_KEY_PATH).getParamValue();

        String appId = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, PaymentClientEnum.STUDENT, WxpayConstant.WX_APPID).getParamValue();


        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(appId);
        payConfig.setMchId(merchantId);
        payConfig.setMchKey(merchantPrivateKey);
        payConfig.setCertSerialNo(merchantSerialNumber);
        payConfig.setApiV3Key(apiV3Key);
        if("local".equals(profile)){
            payConfig.setPrivateCertPath("D:\\env\\license\\wxpay\\apiclient_cert.pem");
            payConfig.setPrivateKeyPath("D:\\env\\license\\wxpay\\apiclient_key.pem");
        }else{
            payConfig.setPrivateCertPath(privateCertPath);
            payConfig.setPrivateKeyPath(privateKeyPath);
        }

        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);


        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

    @Bean(name = "teacherWxService")
    public WxPayService teacherWxService() {
        String merchantId = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, WxpayConstant.WX_MERCHANT_ID).getParamValue();
        String apiV3Key = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, WxpayConstant.WX_API_V3_KEY).getParamValue();

        String merchantSerialNumber = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, WxpayConstant.WX_MERCHANT_SERIAL_NUMBER).getParamValue();
        String merchantPrivateKey = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, WxpayConstant.WX_MERCHANT_PRIVATE_KEY).getParamValue();

        String privateCertPath = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, WxpayConstant.WX_PRIVATE_CERT_PATH).getParamValue();
        String privateKeyPath = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, WxpayConstant.WX_PRIVATE_KEY_PATH).getParamValue();

        String appId = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, PaymentClientEnum.TEACHER, WxpayConstant.WX_APPID).getParamValue();


        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(appId);
        payConfig.setMchId(merchantId);
        payConfig.setMchKey(merchantPrivateKey);
        payConfig.setCertSerialNo(merchantSerialNumber);
        payConfig.setApiV3Key(apiV3Key);

        if("local".equals(profile)){
            payConfig.setPrivateCertPath("D:\\env\\license\\wxpay\\apiclient_cert.pem");
            payConfig.setPrivateKeyPath("D:\\env\\license\\wxpay\\apiclient_key.pem");
        }else{
            payConfig.setPrivateCertPath(privateCertPath);
            payConfig.setPrivateKeyPath(privateKeyPath);
        }
        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);


        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }
}
