package com.kxs.common.pay.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.kxs.common.pay.factory.PaymentFactory;
import com.kxs.common.pay.handlers.PayEventHandlers;
import com.kxs.common.pay.service.ali.AliPayService;
import com.kxs.common.pay.service.wechat.WxPayService;
import com.kxs.common.pay.service.yue.YuePayService;
import com.kxs.user.api.feign.RemoteKxsUserService;
import com.kxs.user.api.feign.RemoteYuePayService;
import com.lly835.bestpay.config.WxPayConfig;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 支付配置
 *
 * @author 没秃顶的码农
 * @date 2024-04-24
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties({AliPayAccountConfig.class, WechatAccountConfig.class})
@AllArgsConstructor
public class PayConfiguration {

    private final WechatAccountConfig wechatAccountConfig;

    private final AliPayAccountConfig aliPayAccountConfig;

    private final RemoteYuePayService remoteYuePayService;

    @Bean
    public WxPayConfig wxPayConfig() {
        WxPayConfig wxPayConfig = new WxPayConfig();
        wxPayConfig.setAppId(wechatAccountConfig.getMpAppId());
        wxPayConfig.setMiniAppId(wechatAccountConfig.getMiniAppId());
        wxPayConfig.setMchId(wechatAccountConfig.getMchId());
        wxPayConfig.setMchKey(wechatAccountConfig.getMchKey());
        wxPayConfig.setKeyPath(wechatAccountConfig.getKeyPath());
        wxPayConfig.setNotifyUrl(wechatAccountConfig.getNotifyUrl());
        wxPayConfig.setAppAppId(wechatAccountConfig.getAppAppId());
        return wxPayConfig;
    }

    @Bean
    public AlipayClient alipayClient() throws AlipayApiException {
        AlipayConfig aliPayConfig = new AlipayConfig();
        aliPayConfig.setAppId(aliPayAccountConfig.getAppId());
        aliPayConfig.setPrivateKey(aliPayAccountConfig.getPrivateKey());
        aliPayConfig.setSignType(aliPayAccountConfig.getSignType());
        aliPayConfig.setAlipayPublicKey(aliPayAccountConfig.getAliPayPublicKey());
        return new DefaultAlipayClient(aliPayConfig);
    }


    @Bean
    @ConditionalOnMissingBean
    public YuePayService yuePayService() {

        return new YuePayService(remoteYuePayService);
    }

    @Bean
    @ConditionalOnMissingBean
    public AliPayService aliPayService(AlipayClient alipayClient) {

        return new AliPayService(alipayClient, aliPayAccountConfig);
    }

    @Bean
    @ConditionalOnMissingBean
    public WxPayService wxPayService(WxPayConfig wxPayConfig) {

        return new WxPayService(wxPayConfig, wechatAccountConfig);
    }

    @Bean
    @ConditionalOnMissingBean
    public PaymentFactory paymentFactory(AliPayService aliPayService, WxPayService wxPayService, YuePayService yuePayService) {

        return new PaymentFactory(aliPayService, wxPayService, yuePayService);
    }

    @Bean
    @ConditionalOnMissingBean
    public PayEventHandlers payEventHandlers(PaymentFactory paymentFactory) {

        return new PayEventHandlers(paymentFactory);
    }
}