package com.ruoyi.bst.channel.domain.enums;

import com.ruoyi.common.pay.PayApi;
import com.ruoyi.common.pay.ali.service.AliPayService;
import com.ruoyi.common.pay.tm.TmPayService;
import com.ruoyi.common.pay.wx.service.WxPayService;
import com.ruoyi.common.utils.spring.SpringUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 渠道信息
 * @author 辉
 * 2024/3/12
 */
@Getter
@AllArgsConstructor
public enum ChannelApiType {

    WECHAT("WX", "微信支付",WxPayService.class, false),
    ALI("ALI", "支付宝", AliPayService.class, true),
    BANK("BANK", "银行卡", null, false),
    BALANCE("BALANCE", "余额支付", null, true),
    TM_WX("TM_WX", "太米微信支付", TmPayService.class, true);

    private final String type;
    private final String name;
    private final Class<? extends PayApi> payApi;
    private final Boolean isRefundSync; // 退款是否同步通知

    public static ChannelApiType parse(String type) {
        for (ChannelApiType value : ChannelApiType.values()) {
            if (Objects.equals(value.getType(), type)) {
                return value;
            }
        }
        return null;
    }

    public static List<String> asList(ChannelApiType...types) {
        return Arrays.stream(types).map(ChannelApiType::getType).collect(Collectors.toList());
    }

    /**
     * 获取支付API
     */
    public static PayApi getPayApi(String type) {
        ChannelApiType parse = parse(type);
        return getPayApi(parse);
    }


    public static PayApi getPayApi(ChannelApiType type) {
        if (type == null || type.getPayApi() == null) {
            return null;
        }
        return SpringUtils.getBean(type.getPayApi());
    }
}

