package com.xingxi.unifiedpay.configuration;

import com.xingxi.unifiedpay.domain.XingxiUnifiedpayCallbackResultDTO;
import com.xingxi.unifiedpay.open.XingxiUnifiedpayCallbackServiceI;
import com.xingxi.unifiedpay.repository.XingxiUnifiedpayRepository;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
@ComponentScan(basePackages = {"com.xingxi.unifiedpay"})
@ConditionalOnProperty(prefix = "spring.application.xingxi.unifiedpay", name = "enabled", havingValue = "true")
@EnableConfigurationProperties(XingxiUnifiedpayWxPayProperties.class)
@RequiredArgsConstructor
@ConditionalOnBean(XingxiUnifiedpayRepository.class)
class XingxiUnifiedpayConfiguration {
    private final XingxiUnifiedpayWxPayProperties wxPayProperties;

    @Bean
    @ConditionalOnMissingBean(XingxiUnifiedpayCallbackServiceI.class)
    XingxiUnifiedpayCallbackServiceI biandanUnifiedpayCallbackServiceI(){
        return new XingxiUnifiedpayCallbackServiceI() {
            @Override
            public void callbackFollowUpProcessing(XingxiUnifiedpayCallbackResultDTO biandanUnifiedpayCallbackResultDTO) {
                throw new RuntimeException("业务实现[BiandanUnifiedpayCallbackServiceI]");
            }
        };
    }

    @Bean("wxPayService")
    @ConditionalOnProperty(prefix = "spring.application.xingxi.unifiedpay.wxpay", name = "enabled", havingValue = "true")
    WxPayService wxPayService() {
        final WxPayService wxPayService = new WxPayServiceImpl();
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull(this.wxPayProperties.getAppId()));
        payConfig.setMchId(StringUtils.trimToNull(this.wxPayProperties.getMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(this.wxPayProperties.getMchKey()));
        payConfig.setSubAppId(StringUtils.trimToNull(this.wxPayProperties.getSubAppId()));
        payConfig.setSubMchId(StringUtils.trimToNull(this.wxPayProperties.getSubMchId()));
        payConfig.setKeyPath(StringUtils.trimToNull(this.wxPayProperties.getKeyPath()));
        //以下是apiv3以及支付分相关
        payConfig.setServiceId(StringUtils.trimToNull(this.wxPayProperties.getServiceId()));
        payConfig.setPayScoreNotifyUrl(StringUtils.trimToNull(this.wxPayProperties.getPayScoreNotifyUrl()));
        payConfig.setPrivateKeyPath(StringUtils.trimToNull(this.wxPayProperties.getPrivateKeyPath()));
        payConfig.setPrivateCertPath(StringUtils.trimToNull(this.wxPayProperties.getPrivateCertPath()));
        payConfig.setCertSerialNo(StringUtils.trimToNull(this.wxPayProperties.getCertSerialNo()));
        payConfig.setApiV3Key(StringUtils.trimToNull(this.wxPayProperties.getApiv3Key()));
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

}
