package com.bonus.canteen.core.order.web.vo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.bonus.canteen.core.account.v3.metadata.enums.AccWalletIdEnum;
import com.bonus.canteen.core.order.common.model.OrderAmountDetail;
import com.bonus.canteen.core.order.utils.LeNumUtil;
import com.bonus.canteen.core.pay.common.constants.PayChannelEnum;
import com.bonus.canteen.core.pay.common.constants.PayTypeEnum;
import com.bonus.canteen.core.pay.common.vo.TradeChannelVO;

import java.math.BigDecimal;
import java.util.List;

public class OrderAmountDetailWebVO {
   private Long amountDetailId;
   private Long orderId;
   private Long tradeChannelId;
   private Integer walletId;
   private Integer payType;
   private Integer payChannel;
   private String payTypeDesc;
   private BigDecimal orderPayAmount;
   private BigDecimal channelPayAmount;
   private BigDecimal channelRefundAmount;

   public String getPayTypeDesc() {
      String desc = PayTypeEnum.getDesc(this.payType);
      if (PayChannelEnum.ACC.getKey().equals(this.payChannel) && LeNumUtil.isValidId(this.walletId)) {
         String walletName = AccWalletIdEnum.getDesc(this.walletId);
         if (StrUtil.isNotBlank(walletName)) {
            desc = desc + "-" + walletName;
         }
      }

      return desc;
   }

   public void fillTradeChannelVO(TradeChannelVO tradeChannelVO) {
      if (tradeChannelVO != null) {
         this.channelPayAmount = tradeChannelVO.getAmount();
         this.channelRefundAmount = tradeChannelVO.getRefundAmount();
      }
   }

   public static List<OrderAmountDetailWebVO> listOf(OrderAmountDetail orderAmountDetail) {
      List<OrderAmountDetailWebVO> list = CollUtil.newArrayList(new OrderAmountDetailWebVO[0]);
      OrderAmountDetailWebVO vo;
      if (orderAmountDetail.ifContainsAccPayDetail()) {
         if (orderAmountDetail.getWalletAmount() != null && orderAmountDetail.getWalletAmount().compareTo(BigDecimal.ZERO) > 0) {
            vo = of(orderAmountDetail);
            vo.setWalletId(AccWalletIdEnum.WALLET.getKey());
            vo.setOrderPayAmount(orderAmountDetail.getWalletAmount());
            list.add(vo);
         }

         if (orderAmountDetail.getSubsidyAmount() != null && orderAmountDetail.getSubsidyAmount().compareTo(BigDecimal.ZERO) > 0) {
            vo = of(orderAmountDetail);
            vo.setWalletId(AccWalletIdEnum.SUBSIDY.getKey());
            vo.setOrderPayAmount(orderAmountDetail.getSubsidyAmount());
            list.add(vo);
         }

         if (orderAmountDetail.getRedEnvelopeAmount() != null && orderAmountDetail.getRedEnvelopeAmount().compareTo(BigDecimal.ZERO) > 0) {
            vo = of(orderAmountDetail);
            vo.setWalletId(AccWalletIdEnum.LUCK_MONEY.getKey());
            vo.setOrderPayAmount(orderAmountDetail.getRedEnvelopeAmount());
            list.add(vo);
         }
      } else {
         vo = of(orderAmountDetail);
         list.add(vo);
      }

      return list;
   }

   public static OrderAmountDetailWebVO of(OrderAmountDetail orderAmountDetail) {
      OrderAmountDetailWebVO vo = new OrderAmountDetailWebVO();
      vo.setOrderId(orderAmountDetail.getOrderId());
      vo.setAmountDetailId(orderAmountDetail.getAmountDetailId());
      vo.setTradeChannelId(orderAmountDetail.getTradeChannelId());
      vo.setPayType(orderAmountDetail.getPayType());
      vo.setPayChannel(orderAmountDetail.getPayChannel());
      vo.setOrderPayAmount(orderAmountDetail.getAmount());
      return vo;
   }

   public Long getAmountDetailId() {
      return this.amountDetailId;
   }

   public Long getOrderId() {
      return this.orderId;
   }

   public Long getTradeChannelId() {
      return this.tradeChannelId;
   }

   public Integer getWalletId() {
      return this.walletId;
   }

   public Integer getPayType() {
      return this.payType;
   }

   public Integer getPayChannel() {
      return this.payChannel;
   }

   public BigDecimal getOrderPayAmount() {
      return this.orderPayAmount;
   }

   public BigDecimal getChannelPayAmount() {
      return this.channelPayAmount;
   }

   public BigDecimal getChannelRefundAmount() {
      return this.channelRefundAmount;
   }

   public void setAmountDetailId(final Long amountDetailId) {
      this.amountDetailId = amountDetailId;
   }

   public void setOrderId(final Long orderId) {
      this.orderId = orderId;
   }

   public void setTradeChannelId(final Long tradeChannelId) {
      this.tradeChannelId = tradeChannelId;
   }

   public void setWalletId(final Integer walletId) {
      this.walletId = walletId;
   }

   public void setPayType(final Integer payType) {
      this.payType = payType;
   }

   public void setPayChannel(final Integer payChannel) {
      this.payChannel = payChannel;
   }

   public void setPayTypeDesc(final String payTypeDesc) {
      this.payTypeDesc = payTypeDesc;
   }

   public void setOrderPayAmount(final BigDecimal orderPayAmount) {
      this.orderPayAmount = orderPayAmount;
   }

   public void setChannelPayAmount(final BigDecimal channelPayAmount) {
      this.channelPayAmount = channelPayAmount;
   }

   public void setChannelRefundAmount(final BigDecimal channelRefundAmount) {
      this.channelRefundAmount = channelRefundAmount;
   }

}
