package com.bonus.core.allocation.canteen.dto;

import cn.hutool.core.collection.CollUtil;
import com.bonus.core.allocation.canteen.enums.AllocMinDeliveryFeeOnOffEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.Iterator;
import java.util.List;

@ApiModel("配送设置Model")
public class AllocCanteenDeliveryModel {
   @ApiModelProperty("是否启用配送配置")
   private Integer ifEnableDelivery = 2;
   @ApiModelProperty("起送费开关")
   private Integer minDeliveryFeeOnOff;
   @ApiModelProperty("整体起送费")
   private Integer wholeMinDeliveryFee;
   private List<MealtimeMinDeliveryFeeDTO> mealtimeMinDeliveryFeeList;
   @ApiModelProperty("配送方式(多个数据之间 使用,分隔)")
   private String deliveries;
   @ApiModelProperty("配送列表")
   private List<AllocCanteenDeliveryDTO> deliveryList;

   public Integer getIfEnableDelivery() {
      return ifEnableDelivery;
   }

   public void setIfEnableDelivery(Integer ifEnableDelivery) {
      this.ifEnableDelivery = ifEnableDelivery;
   }

   public Integer getMinDeliveryFeeOnOff() {
      return minDeliveryFeeOnOff;
   }

   public void setMinDeliveryFeeOnOff(Integer minDeliveryFeeOnOff) {
      this.minDeliveryFeeOnOff = minDeliveryFeeOnOff;
   }

   public Integer getWholeMinDeliveryFee() {
      return wholeMinDeliveryFee;
   }

   public void setWholeMinDeliveryFee(Integer wholeMinDeliveryFee) {
      this.wholeMinDeliveryFee = wholeMinDeliveryFee;
   }

   public List<MealtimeMinDeliveryFeeDTO> getMealtimeMinDeliveryFeeList() {
      return mealtimeMinDeliveryFeeList;
   }

   public void setMealtimeMinDeliveryFeeList(List<MealtimeMinDeliveryFeeDTO> mealtimeMinDeliveryFeeList) {
      this.mealtimeMinDeliveryFeeList = mealtimeMinDeliveryFeeList;
   }

   public String getDeliveries() {
      return deliveries;
   }

   public void setDeliveries(String deliveries) {
      this.deliveries = deliveries;
   }

   public List<AllocCanteenDeliveryDTO> getDeliveryList() {
      return deliveryList;
   }

   public void setDeliveryList(List<AllocCanteenDeliveryDTO> deliveryList) {
      this.deliveryList = deliveryList;
   }

   public static class MealtimeMinDeliveryFeeDTO {
      @ApiModelProperty("餐次类型")
      Integer mealtimeType;
      @ApiModelProperty("起送费")
      private Integer minDeliveryFee;

      public Integer getMealtimeType() {
         return mealtimeType;
      }

      public void setMealtimeType(Integer mealtimeType) {
         this.mealtimeType = mealtimeType;
      }

      public Integer getMinDeliveryFee() {
         return minDeliveryFee;
      }

      public void setMinDeliveryFee(Integer minDeliveryFee) {
         this.minDeliveryFee = minDeliveryFee;
      }
   }

   public AllocCanteenDeliveryModel(Integer ifEnableDelivery, Integer minDeliveryFeeOnOff, Integer wholeMinDeliveryFee, List<MealtimeMinDeliveryFeeDTO> mealtimeMinDeliveryFeeList, String deliveries, List<AllocCanteenDeliveryDTO> deliveryList) {
      this.minDeliveryFeeOnOff = AllocMinDeliveryFeeOnOffEnum.NONE.getKey();
      this.mealtimeMinDeliveryFeeList = CollUtil.newArrayList(new MealtimeMinDeliveryFeeDTO[0]);
      this.deliveryList = CollUtil.newArrayList(new AllocCanteenDeliveryDTO[0]);
      this.ifEnableDelivery = ifEnableDelivery;
      this.minDeliveryFeeOnOff = minDeliveryFeeOnOff;
      this.wholeMinDeliveryFee = wholeMinDeliveryFee;
      this.mealtimeMinDeliveryFeeList = mealtimeMinDeliveryFeeList;
      this.deliveries = deliveries;
      this.deliveryList = deliveryList;
   }

   public AllocCanteenDeliveryDTO deliveryDetailByType(Integer type) {
      return CollUtil.isEmpty(this.deliveryList) ? new AllocCanteenDeliveryDTO() : this.deliveryList.stream().filter((s) -> {
         return s.getDeliveryType().equals(type);
      }).findFirst().orElse(new AllocCanteenDeliveryDTO());
   }

   public Integer getWholeMinDeliveryFee(Integer mealtimeType) {
      if (AllocMinDeliveryFeeOnOffEnum.WHOLE.getKey().equals(this.minDeliveryFeeOnOff)) {
         return this.wholeMinDeliveryFee;
      } else {
         if (AllocMinDeliveryFeeOnOffEnum.MEALTIME.getKey().equals(this.minDeliveryFeeOnOff)) {
            Iterator var2 = this.mealtimeMinDeliveryFeeList.iterator();

            while(var2.hasNext()) {
               MealtimeMinDeliveryFeeDTO feeDTO = (MealtimeMinDeliveryFeeDTO)var2.next();
               if (feeDTO.getMealtimeType().equals(mealtimeType)) {
                  return feeDTO.getMinDeliveryFee();
               }
            }
         }

         return 0;
      }
   }
}
