package com.propertyManagement.pay.support.pay;

import cn.hutool.core.collection.CollUtil;
import com.propertyManagement.pay.domain.CmsPayBiz;
import com.propertyManagement.pay.support.pay.ali.CmsAliPayProvider;
import com.propertyManagement.pay.support.pay.wx.CmsWxPayProvider;
import com.siashan.unipay4j.biz.PayBiz;
import com.siashan.unipay4j.core.PayChannel;
import com.siashan.unipay4j.core.pay.*;
import com.siashan.unipay4j.core.transfer.ITransfer;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

/**
 * 支付服务商
 *
 * @author siashan
 * @date 2023/10/5
 * @since v1.2.3
 **/
@Slf4j
public class CmsUnipayProvider {

    private static Map<String, PayBiz> bizMap;

    public static void init(List<CmsPayBiz> payBizList, List<PayChannelDto> channelDtoList) {
        bizMap = CmsPayUtil.transBizMap(payBizList);
        if (CollUtil.isNotEmpty(channelDtoList)) {
            for (PayChannelDto channelDto : channelDtoList) {
                switch (channelDto.getChannel()) {
                    case WX:
                        CmsWxPayProvider.init(channelDto);
                        break;
                    case ALI:
                        CmsAliPayProvider.init(channelDto);
                        break;
                    default:
                        break;
                }
            }
        }
    }


    /**
     * 获取支付对象
     *
     * @param channelCode 支付渠道code
     * @param mchCode     商户号code
     * @param appCode     应用code
     * @param bizCode     业务代码
     * @return 支付类
     */
    public static IPayment getPayment(Long orderId, String channelCode, String mchCode, String appCode, String bizCode) {
        return getPayProvider(channelCode).getPayment(orderId, bizMap, mchCode, appCode, bizCode);
    }

    /**
     * 获取回调对象
     *
     * @return 回调类
     */
    public static INotify getNotify(String channelCode, String mchCode, String appCode) {
        return getPayProvider(channelCode).getNotify(mchCode, appCode);
    }


    /**
     * 获取查询对象
     *
     * @return 查询类
     */
    public static IQuery getQuery(String channelCode, String mchCode,String appCode, String bizCode) {
        return getPayProvider(channelCode).getQuery(bizMap, mchCode, appCode, bizCode);
    }


    /**
     * 获取转账对象
     *
     * @return 转账类
     */
    public ITransfer getTransfer(String channelCode, String mchCode,String appCode) {
        return getPayProvider(channelCode).getTransfer(mchCode, appCode);
    }

    /**
     * 获取退款对象
     *
     * @return 退款类
     */
    public static IRefund getRefund(Long orderId, String channelCode, String mchCode, String appCode, String bizCode) {
        return getPayProvider(channelCode).getRefund(orderId, bizMap, mchCode, appCode, bizCode);
    }

    /**
     * 获取关单对象
     *
     * @return 关单类
     */
    public IClose getClose(String channelCode, String mchCode,String appCode, String bizCode) {
        return getPayProvider(channelCode).getClose(bizMap, mchCode, appCode, bizCode);
    }



    private static ICmsUnipayProvider getPayProvider(String channelCode){
        PayChannel channel = PayChannel.valueOf(channelCode);
        ICmsUnipayProvider provider = null;
        switch (channel) {
            case WX:
                provider = new CmsWxPayProvider();
                break;
            case ALI:
                break;
            default:
                break;
        }
        return provider;
    }
}
