package com.bonus.core.pay.common.service;


import com.bonus.core.allocation.alloc.model.AllocPayModel;
import com.bonus.core.order.utils.LeNumUtil;
import com.bonus.core.pay.channel.cmb.dto.QrCodeApplyDTO;
import com.bonus.core.pay.common.constants.PayChannelEnum;
import com.bonus.core.pay.common.constants.PayChannelSelectEnum;
import com.bonus.core.pay.common.constants.PayStateEnum;
import com.bonus.core.pay.common.dto.*;
import com.bonus.core.pay.common.vo.*;

public interface PayService {

    default boolean payChannelSelect(PayChannelSelectEnum selectType, PayChannelSelectDTO payChannelSelectDTO, AllocPayModel canteenPayModel, AllocPayModel merchantPayModel, PayChannelSelectService payChannelSelectService, PayChannelSelectResultVO resultVO) {
        try {
            PayChannelSelectOldVO oldVO = payChannelSelectService.payChannelSelect(selectType, canteenPayModel, payChannelSelectDTO, resultVO);
            if (LeNumUtil.isValidId(oldVO.getPayType()) && LeNumUtil.isValidId(oldVO.getPayChannel())) {
                if (PayChannelEnum.MIXTURE.getKey().equals(oldVO.getPayChannel())) {
                    PayChannelSplitDTO splitParam = PayChannelSplitDTO.of(oldVO.getPayType(), oldVO.getPayChannel(), oldVO.getCouponId(), payChannelSelectDTO.getCanteenId());
                    payChannelSelectService.splitChannelDetails(splitParam).forEach((s) -> {
                        resultVO.addPayChannelSplit(s, false);
                    });
                } else {
                    resultVO.addPayChannelSplit(oldVO.getPayType(), oldVO.getPayChannel());
                }
            }

            if (oldVO.getCustPayVO() != null && resultVO.getCustPayVO() == null) {
                resultVO.setCustPayVO(oldVO.getCustPayVO());
            }
        } catch (Exception var9) {
            resultVO.setErrorInfo(var9);
        }

        return true;
    }

    default ChannelPayVO channelPayTrail(ChannelPayDTO channelPayDTO) {
        ChannelPayVO channelPayVO = new ChannelPayVO();
        channelPayVO.setCode(PayStateEnum.PAY_SUCC.getKey());
        channelPayVO.setMsg("试算成功");
        channelPayVO.setRealAmount(channelPayDTO.getAmount());
        return channelPayVO;
    }

    default SupportFastAsyncPayVO supportFastAsyncPay(ChannelPayDTO channelPayDTO) {
        return SupportFastAsyncPayVO.no();
    }

    default MixPayResultVO mixPayResultHandler(MixPayResultDTO mixPayResultDTO, TradeChannelVO tradeChannelVO) {
        return MixPayResultVO.newDefaultInstance();
    }

    default ChannelPayVO channelPay(ChannelPayDTO channelPayDTO) {
        if (PayChannelSelectEnum.isMobile(channelPayDTO.getScenario())) {
            MobilePayDTO mobilePayDTO = channelPayDTO.convertToMobilePayDTO();
            MobilePayVO mobilePayVO = this.mobilePay(mobilePayDTO);
            return ChannelPayVO.of(mobilePayVO);
        } else {
            AndroidPayDTO androidPayDTO;
            if (PayChannelSelectEnum.isDeviceOnline(channelPayDTO.getScenario())) {
                androidPayDTO = channelPayDTO.convertToAndroidPayDTO();
                AndroidPayVO unifyPayVO = this.androidPay(androidPayDTO);
                return ChannelPayVO.of(unifyPayVO);
            } else if (!PayChannelSelectEnum.isDeviceOffline(channelPayDTO.getScenario()) && !PayChannelSelectEnum.isExceptionPay(channelPayDTO.getScenario())) {
                if (PayChannelSelectEnum.isDeviceQrCode(channelPayDTO.getScenario())) {
                    QrCodeApplyDTO qrCodeApplyDTO = channelPayDTO.convertToQrCodeApplyDTO();
                    QrCodeApplyVO unifyPayVO = this.qrCodeApply(qrCodeApplyDTO);
                    return ChannelPayVO.of(unifyPayVO);
                } else {
                    ChannelPayVO unifyPayVO = new ChannelPayVO();
                    unifyPayVO.setCode(PayStateEnum.PAY_FAIL.getKey());
                    unifyPayVO.setMsg("当前渠道暂不支持支付");
                    return unifyPayVO;
                }
            } else {
                androidPayDTO = channelPayDTO.convertToAndroidPayDTO();
                OfflinePayVO unifyPayVO = this.offlinePay(androidPayDTO);
                return ChannelPayVO.of(unifyPayVO);
            }
        }
    }

    @Deprecated
    default MobilePayVO mobilePay(MobilePayDTO mobilePayDTO) {
        MobilePayVO mobilePayVO = new MobilePayVO();
        mobilePayVO.setCode(PayStateEnum.PAY_FAIL.getKey());
        mobilePayVO.setMsg("暂不支持移动端支付");
        return mobilePayVO;
    }
    @Deprecated
    default AndroidPayVO androidPay(AndroidPayDTO androidPayDTO) {
        AndroidPayVO androidPayVO = new AndroidPayVO();
        androidPayVO.setCode(PayStateEnum.PAY_FAIL.getKey());
        androidPayVO.setMsg("暂不支持设备端支付");
        return androidPayVO;
    }

    @Deprecated
    default OfflinePayVO offlinePay(AndroidPayDTO androidPayDTO) {
        OfflinePayVO offlinePayVO = new OfflinePayVO();
        offlinePayVO.setCode(PayStateEnum.PAY_FAIL.getKey());
        offlinePayVO.setMsg("暂不支持离线支付");
        return offlinePayVO;
    }

    /** @deprecated */
    @Deprecated
    default QrCodeApplyVO qrCodeApply(QrCodeApplyDTO qrCodeApplyDTO) {
        QrCodeApplyVO qrCodeApplyVO = new QrCodeApplyVO();
        qrCodeApplyVO.setCode(PayStateEnum.PAY_FAIL.getKey());
        qrCodeApplyVO.setMsg("暂不支持收款二维码");
        return qrCodeApplyVO;
    }

    default AndroidPayVO paySelect(PaySelectDTO paySelectDTO) {
        AndroidPayVO androidPayVO = new AndroidPayVO();
        androidPayVO.setCode(PayStateEnum.PAY_INPROCESS.getKey());
        androidPayVO.setMsg("当前渠道暂不支持查询支付结果");
        return androidPayVO;
    }

    default PayRefundVO payRefund(PayRefundDTO payRefundDTO) {
        PayRefundVO payRefundVO = new PayRefundVO();
        payRefundVO.setCode(PayStateEnum.PAY_FAIL.getKey());
        payRefundVO.setMsg("当前渠道暂不支持退款功能");
        return payRefundVO;
    }
}
