package com.bonus.canteen.core.pay.channel.coupon.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.NumberUtil;
import com.bonus.canteen.core.allocation.alloc.model.AllocPayModel;
import com.bonus.canteen.core.common.utils.JacksonUtil;
import com.bonus.canteen.core.customer.api.CustInfoApi;
import com.bonus.canteen.core.customer.dto.CustPayDTO;
import com.bonus.canteen.core.order.utils.LeNumUtil;
import com.bonus.canteen.core.pay.channel.coupon.extension.CouponPayExtension;
import com.bonus.canteen.core.pay.common.constants.PayChannelEnum;
import com.bonus.canteen.core.pay.common.constants.PayChannelSelectEnum;
import com.bonus.canteen.core.pay.common.constants.PayStateEnum;
import com.bonus.canteen.core.pay.common.constants.PayTypeEnum;
import com.bonus.canteen.core.pay.common.dto.ChannelPayDTO;
import com.bonus.canteen.core.pay.common.dto.MixPayResultDTO;
import com.bonus.canteen.core.pay.common.service.PayChannelSelectService;
import com.bonus.canteen.core.pay.common.vo.*;
import com.bonus.common.houqin.i18n.I18n;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class CouponPayServiceImpl implements CouponPayExtension {
   private static final Logger log = LoggerFactory.getLogger(CouponPayServiceImpl.class);
   private static final String PARAM_KEY_COUPON_ID = "couponId";
//   @Autowired
//   @Lazy
//   private MktCouponApi mktCouponApi;
//   @Autowired
//   @Lazy
//   private CustInfoApi custInfoApi;
//
//   public ChannelPayVO channelPayTrail(ChannelPayDTO channelPayDTO) {
//      ChannelPayVO channelPayVO = new ChannelPayVO();
//      channelPayVO.setCode(PayStateEnum.PAY_SUCC.getKey());
//      if (channelPayDTO.getAmount().compareTo(BigDecimal.ZERO) == 0) {
//         return channelPayVO;
//      } else {
//         Long couponNum = this.parseCouponNumFromCouponId(channelPayDTO.getCouponId());
//         MktCouponPayModel coupon = this.mktCouponApi.getCouponAmountForAppPay(couponNum);
//         BigDecimal amountForAppPay = coupon.getCouponAmount().multiply(new BigDecimal("100"));
//         BigDecimal realAmount = amountForAppPay.min(channelPayDTO.getAmount());
//         channelPayVO.setRealAmount(realAmount);
//         return channelPayVO;
//      }
//   }
//
//   public SupportFastAsyncPayVO supportFastAsyncPay(ChannelPayDTO channelPayDTO) {
//      return SupportFastAsyncPayVO.yes();
//   }
//
//   public ChannelPayVO channelPay(ChannelPayDTO channelPayDTO) {
//      Long couponNum = this.parseCouponNumFromCouponId(channelPayDTO.getCouponId());
//      MktCouponPayModel coupon = this.mktCouponApi.getCouponAmountForAppPay(couponNum);
//      BigDecimal amountForAppPay = coupon.getCouponAmount().multiply(new BigDecimal("100"));
//      if (!channelPayDTO.isPayForMix() && amountForAppPay.compareTo(channelPayDTO.getAmount()) < 0) {
//         throw new LeException(I18n.getMessage("pay.coupon-amount-query-failed", new Object[0]));
//      } else {
//         BigDecimal realAmount = amountForAppPay.min(channelPayDTO.getAmount());
//         this.mktCouponApi.updateCouponAfterPay(couponNum, realAmount.multiply(new BigDecimal("0.01")));
//         ChannelPayVO channelPayVO = (ChannelPayVO)BeanUtil.copyProperties(channelPayDTO, ChannelPayVO.class, new String[0]);
//         channelPayVO.setCode(PayStateEnum.PAY_SUCC.getKey());
//         channelPayVO.setMsg(PayStateEnum.PAY_SUCC.getDesc());
//         channelPayVO.setRealAmount(realAmount);
//         ObjectNode channelParam = JacksonUtil.objectNode();
//         channelParam.put("couponId", couponNum);
//         channelPayVO.setChannelParam(channelParam);
//         return channelPayVO;
//      }
//   }
//
//   public AndroidPayVO paySelect(PaySelectDTO paySelectDTO) {
//      AndroidPayVO androidPayVO = new AndroidPayVO();
//      Long couponNum = this.parseCouponNumFromCouponId(paySelectDTO.getCouponId());
//      Boolean success = this.mktCouponApi.selectCouponPayState(couponNum);
//      if (success) {
//         androidPayVO.setCode(PayStateEnum.PAY_SUCC.getKey());
//         androidPayVO.setMsg(PayStateEnum.PAY_SUCC.getDesc());
//      } else {
//         androidPayVO.setCode(PayStateEnum.PAY_FAIL.getKey());
//         androidPayVO.setMsg(PayStateEnum.PAY_FAIL.getDesc());
//      }
//
//      androidPayVO.setRealAmount(BigDecimal.ZERO);
//      return androidPayVO;
//   }
//
//   public PayCancelVO payCancel(PayCancelDTO payCancelDTO) {
//      Long couponId = this.parseCouponNumFromCouponId(payCancelDTO.getCouponId());
//      this.mktCouponApi.cancelCouponPay(couponId);
//      PayCancelVO payCancelVO = new PayCancelVO();
//      payCancelVO.setCode(CancelResultEnum.CANCEL_SUCCESS.getKey());
//      payCancelVO.setMsg(CancelResultEnum.CANCEL_SUCCESS.getDesc());
//      return payCancelVO;
//   }
//
//   public MixPayResultVO mixPayResultHandler(MixPayResultDTO mixPayResultDTO, TradeChannelVO tradeChannelVO) {
//      MixPayResultVO resultVO = MixPayResultVO.newDefaultInstance();
//      if (PayStateEnum.isPaySuccessState(tradeChannelVO.getTradeState()) && PayStateEnum.isFailedState(mixPayResultDTO.getCode())) {
//         if (tradeChannelVO.getParam() != null && tradeChannelVO.getParam().get("couponId") != null) {
//            this.mktCouponApi.cancelCouponPay(tradeChannelVO.getParam().get("couponId").longValue());
//            tradeChannelVO.setAmount(BigDecimal.ZERO);
//            tradeChannelVO.setTradeState(PayStateEnum.PAY_CLOSE.getKey());
//            tradeChannelVO.setTradeResult(PayStateEnum.PAY_CLOSE.getDesc());
//            resultVO.setNeedUpdateTradeChannel(true);
//            return resultVO;
//         } else {
//            log.error("[混合支付补偿]餐券交易参数不存在：{}", mixPayResultDTO.getTradeChannelId());
//            return resultVO;
//         }
//      } else {
//         return resultVO;
//      }
//   }

//   public boolean payChannelSelect(PayChannelSelectEnum selectType, PayChannelSelectDTO payChannelSelectDTO, AllocPayModel canteenPayModel, AllocPayModel merchantPayModel, PayChannelSelectService payChannelSelectService, PayChannelSelectResultVO resultVO) {
//      Long couponNum = this.parseCouponNumFromCouponId(resultVO.getCouponId());
//      if (LeNumUtil.isValidId(couponNum)) {
//         MktCouponPayModel couponAmountForMacPay = this.mktCouponApi.getCouponAmountForAppPay(couponNum);
//         if (MktCouponTypeEnum.ELECTRON_COUPON.getKey().equals(couponAmountForMacPay.getCouponType()) && resultVO.getCustPayVO() == null) {
//            try {
//               resultVO.setCustPayVO(this.custInfoApi.getCustPayVO((new CustPayDTO()).setCustId(couponAmountForMacPay.getCustId())));
//            } catch (Exception var10) {
//               log.warn("电子餐券关联人员信息查询失败", var10);
//            }
//         }
//
//         resultVO.addPayChannelSplit(PayTypeEnum.COUPON.getKey(), PayChannelEnum.COUPON.getKey());
//      }
//
//      return true;
//   }

   private Long parseCouponNumFromCouponId(String couponId) {
      if (CharSequenceUtil.split(couponId, ",", true, true).size() > 1) {
         log.warn("[餐券支付]不支持多张餐券支付 {}", couponId);
         return null;
      } else {
         return NumberUtil.isNumber(couponId) ? Long.valueOf(couponId) : null;
      }
   }
}
