package com.propertyManagement.pay.support.pay.ali;

import cn.hutool.core.collection.CollUtil;
import com.propertyManagement.pay.domain.CmsPayApp;
import com.propertyManagement.pay.support.pay.CmsPayMerchantDto;
import com.propertyManagement.pay.support.pay.ICmsUnipayProvider;
import com.propertyManagement.pay.support.pay.PayChannelDto;
import com.siashan.unipay4j.ali.AliPayConfig;
import com.siashan.unipay4j.ali.AliPayFactory;
import com.siashan.unipay4j.ali.conf.AliApp;
import com.siashan.unipay4j.biz.PayBiz;
import com.siashan.unipay4j.biz.PayBizUtil;
import com.siashan.unipay4j.core.PayChannel;
import com.siashan.unipay4j.core.pay.*;
import com.siashan.unipay4j.core.transfer.ITransfer;
import org.springframework.beans.BeanUtils;

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

public class CmsAliPayProvider implements ICmsUnipayProvider {


    private static Map<String, AliPayConfInfo> mchMap = new HashMap<>();


    public static void init(PayChannelDto channelDto) {
        List<CmsPayMerchantDto> merchantList = channelDto.getMerchantList();
        if (CollUtil.isNotEmpty(merchantList)) {
            for (CmsPayMerchantDto merchant : merchantList) {
                AliPayConfInfo aliPayConfInfo = new AliPayConfInfo();
                List<CmsPayApp> appList = merchant.getAppList();
                if (CollUtil.isNotEmpty(appList)){
                    Map<String, AliApp> appMap = new HashMap<>();
                    for (CmsPayApp payApp : appList){
                        AliApp app = new AliApp();
                        BeanUtils.copyProperties(payApp,app);
                        appMap.put(payApp.getAppCode(),app);
                    }
                    aliPayConfInfo.setAppMap(appMap);
                }
                mchMap.put(merchant.getMerchantCode(),aliPayConfInfo);
            }
        }
    }

    /**
     * 获取支付对象
     *
     * @param mchCode  商户code
     * @param appCode 应用code
     * @param bizCode 业务代码
     * @return 支付类
     */
    @Override
    public  IPayment getPayment(Long orderId, Map<String, PayBiz> bizMap, String mchCode, String appCode, String bizCode) {
        AliPayConfInfo wxPayConfInfo = mchMap.get(mchCode);
        Map<String, AliApp> appMap = wxPayConfInfo.getAppMap();
        AliApp aliApp = appMap.get(appCode);
        return new AliPayFactory().getPayment(aliApp.getTradeType(), initPayConfig(mchCode,aliApp,PayBizUtil.getPayNotifyUrl(bizMap,bizCode)));
    }

    @Override
    public IQuery getQuery(Map<String, PayBiz> bizMap, String mchCode, String appCode, String bizCode) {
        AliPayConfInfo wxPayConfInfo = mchMap.get(mchCode);
        Map<String, AliApp> appMap = wxPayConfInfo.getAppMap();
        AliApp aliApp = appMap.get(appCode);
        return new AliPayFactory().getQuery(initPayConfig(mchCode,aliApp,PayBizUtil.getPayNotifyUrl(bizMap,bizCode)));
    }

    @Override
    public IClose getClose(Map<String, PayBiz> bizMap, String mchCode, String appCode, String bizCode) {
//        AliPayConfInfo wxPayConfInfo = mchMap.get(mchCode);
//        Map<String, AliApp> appMap = wxPayConfInfo.getAppMap();
//        AliApp aliApp = appMap.get(appCode);
//        return new WxPayFactory().getClose(initPayConfig(wxPayConfInfo.getMchConfig(),wxApp.getAppId(), PayBizUtil.getPayNotifyUrl(bizMap,bizCode)));

        return null;
    }

    @Override
    public IRefund getRefund(Long orderId, Map<String, PayBiz> bizMap, String mchCode, String appCode, String bizCode) {
        AliPayConfInfo wxPayConfInfo = mchMap.get(mchCode);
        Map<String, AliApp> appMap = wxPayConfInfo.getAppMap();
        AliApp aliApp = appMap.get(appCode);
        return new AliPayFactory().getRefund(initPayConfig(mchCode,aliApp, PayBizUtil.getRefundNotifyUrl(bizMap,bizCode)));
    }

    @Override
    public INotify getNotify(String mchCode, String appCode) {
        AliPayConfInfo wxPayConfInfo = mchMap.get(mchCode);
        Map<String, AliApp> appMap = wxPayConfInfo.getAppMap();
        AliApp aliApp = appMap.get(appCode);
        return new AliPayFactory().getNotify(initPayConfig(mchCode,aliApp,null));
    }

    @Override
    public ITransfer getTransfer(String mchCode, String appCode) {
        AliPayConfInfo wxPayConfInfo = mchMap.get(mchCode);
        Map<String, AliApp> appMap = wxPayConfInfo.getAppMap();
        AliApp aliApp = appMap.get(appCode);
        return new AliPayFactory().getTransfer(initPayConfig(mchCode,aliApp, null));
    }


    private AliPayConfig initPayConfig(String mchCode,AliApp aliApp, String notifyUrl) {
        AliPayConfig aliPayConfig = new AliPayConfig();
        aliPayConfig.setAppId(aliApp.getAppId());
        aliPayConfig.setMerchantPrivateKey(aliApp.getMerchantPrivateKey());
        aliPayConfig.setAlipayPublicKey(aliApp.getAlipayPublicKey());
        aliPayConfig.setAppCertPath(aliApp.getAppCertPath());
        aliPayConfig.setAlipayCertPath(aliApp.getAlipayCertPath());
        aliPayConfig.setAlipayRootCertPath(aliApp.getAlipayRootCertPath());
        notifyUrl = notifyUrl + "/" + PayChannel.ALI.getCode() + "/" + mchCode + "/" + aliApp.getAppCode();
        aliPayConfig.setNotifyUrl(notifyUrl);
        return aliPayConfig;
    }


}
