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

import com.ruoyi.common.pay.PayApi;
import com.ruoyi.common.pay.ali.service.AliPayService;
import com.ruoyi.common.pay.syb.service.SybPayService;
import com.ruoyi.common.pay.tm.TmPayService;
import com.ruoyi.common.pay.wx.service.WxPayService;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.ss.account.domain.enums.AccountType;
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 ChannelEnum {

    WECHAT(1L, "微信支付", AccountType.WECHAT, WxPayService.class, false),
    ALI(2L, "支付宝", AccountType.ALIPAY, AliPayService.class, true),
    BANK(3L, "银行卡", AccountType.BANK_CARD, null, false),
    BALANCE(4L, "余额支付", null, null, true),
    TL_WX(5L, "通联微信支付", null, SybPayService.class, true),
    TM_WX(6L, "太米微信支付", null, TmPayService.class, true);

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

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

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

    /**
     * 获取支付API
     */
    public static PayApi getPayApi(Long type) {
        ChannelEnum parse = parse(type);
        if (parse == null || parse.getPayApi() == null) {
            return null;
        }
        return SpringUtils.getBean(parse.getPayApi());
    }
}
