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

import cn.hutool.core.collection.CollUtil;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.canteen.core.common.constant.LeRetCodeEnum;
import com.bonus.canteen.core.order.utils.LeNumUtil;

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

public class OrderDeliveryResultVO {
   private Integer code;
   private String msg;
   private BigDecimal lessAmountForDelivery;
   private List<LessAmountInfo> lessAmountInfoList = CollUtil.newArrayList(new LessAmountInfo[0]);

   public boolean ifSuccess() {
      return LeRetCodeEnum.ifSuccess(this.code);
   }

   public BigDecimal getLessAmountForDelivery() {
      return LeNumUtil.sumField(this.lessAmountInfoList, LessAmountInfo::getLessAmountForDelivery);
   }

   public void saveLessAmountInfo(Long orderId, BigDecimal lessAmountForDelivery) {
      this.setCode(LeRetCodeEnum.PAY_ORD_LESS_THAN_DELIVERY_MIN_AMOUNT.getKey());
      this.setMsg(LeRetCodeEnum.PAY_ORD_LESS_THAN_DELIVERY_MIN_AMOUNT.getDesc());
      this.lessAmountInfoList.add(LessAmountInfo.of(orderId, lessAmountForDelivery));
   }

   public static OrderDeliveryResultVO successInstance() {
      OrderDeliveryResultVO resultVO = new OrderDeliveryResultVO();
      resultVO.setCode(LeRetCodeEnum.SUCC.getKey());
      resultVO.setMsg(LeRetCodeEnum.SUCC.getDesc());
      return resultVO;
   }

   public static OrderDeliveryResultVO failedInstance(Exception e) {
      OrderDeliveryResultVO resultVO = new OrderDeliveryResultVO();
      resultVO.setCode(e instanceof ServiceException ? ((ServiceException)e).getCode() : LeRetCodeEnum.FAIL.getKey());
      resultVO.setMsg(e.getMessage());
      return resultVO;
   }

   public Integer getCode() {
      return this.code;
   }

   public String getMsg() {
      return this.msg;
   }

   public List<LessAmountInfo> getLessAmountInfoList() {
      return this.lessAmountInfoList;
   }

   public void setCode(final Integer code) {
      this.code = code;
   }

   public void setMsg(final String msg) {
      this.msg = msg;
   }

   public void setLessAmountForDelivery(final BigDecimal lessAmountForDelivery) {
      this.lessAmountForDelivery = lessAmountForDelivery;
   }

   public void setLessAmountInfoList(final List<LessAmountInfo> lessAmountInfoList) {
      this.lessAmountInfoList = lessAmountInfoList;
   }


   public static class LessAmountInfo {
      private Long orderId;
      private BigDecimal lessAmountForDelivery;

      public static LessAmountInfo of(Long orderId, BigDecimal lessAmountForDelivery) {
         LessAmountInfo lessAmountInfo = new LessAmountInfo();
         lessAmountInfo.setOrderId(orderId);
         lessAmountInfo.setLessAmountForDelivery(lessAmountForDelivery);
         return lessAmountInfo;
      }

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

      public BigDecimal getLessAmountForDelivery() {
         return this.lessAmountForDelivery;
      }

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

      public void setLessAmountForDelivery(final BigDecimal lessAmountForDelivery) {
         this.lessAmountForDelivery = lessAmountForDelivery;
      }
   }
}
