package co.yixiang.yshop.module.pay.strategy;

import co.yixiang.yshop.module.order.api.productorder.ProductOrderApi;
import co.yixiang.yshop.module.pay.enums.PayTypeEnum;
import co.yixiang.yshop.module.pay.strategy.type.AliPay;
import co.yixiang.yshop.module.pay.strategy.type.IntegralPay;
import co.yixiang.yshop.module.pay.strategy.type.NowMoneyPay;
import co.yixiang.yshop.module.pay.strategy.type.WeChatPay;
import co.yixiang.yshop.module.pay.strategy.weixin.service.WxPayServiceUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.pay.enums.ErrorCodeConstants.PAY_TYPE_NOT_EXISTS;

/**
 * 支付工厂实现
 */
@Component
public class PayClientFactoryImpl implements PayClientFactory {

    @Resource
    private WxPayServiceUtils wxPayServiceUtils;
    @Resource
    private ProductOrderApi productOrderApi;

    @Override
    public PayClient getClient(String payType) {
        // 支付类型
        PayTypeEnum type = PayTypeEnum.toType(payType);

        switch (type) {
            // 微信
            case WEIXIN:
            case WEIXIN_H5:
            case WEIXIN_APPLET:
            case WEIXIN_APP:
                return new WeChatPay(wxPayServiceUtils);
            case YUE:
                return new NowMoneyPay(productOrderApi);
            case INTEGRAL:
                return new IntegralPay();
        }
        throw exception(PAY_TYPE_NOT_EXISTS);
    }
}
