package com.bonus.core.marketing.v2.rule.price.dto;

import cn.hutool.core.collection.CollUtil;
import com.bonus.core.marketing.util.EnumUtil;
import com.bonus.core.marketing.v2.rule.price.constants.RulePriceLimitEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
import java.util.Objects;

@ApiModel("限制类型")
public class RulePriceLimitDTO {
   @ApiModelProperty("开始时间")
   private LocalDate beginDate;
   @ApiModelProperty("结束时间")
   private LocalDate endDate;
   @ApiModelProperty("餐次列表")
   private List<Integer> mealtimeTypeList;

   public RulePriceLimitDTO(RulePriceResultOrderDTO order, Integer limitType, List<Integer> mealtimeTypeList) {
      RulePriceLimitEnum limitEnum = (RulePriceLimitEnum) EnumUtil.getEnumByKey(RulePriceLimitEnum.class, limitType);
      if (Objects.isNull(limitEnum)) {
         this.beginDate = order.getOrderDate();
         this.endDate = order.getOrderDate();
         this.mealtimeTypeList = mealtimeTypeList;
      } else {
         switch (limitEnum) {
            case LIMIT_DAY:
               this.beginDate = order.getOrderDate();
               this.endDate = order.getOrderDate();
               this.mealtimeTypeList = mealtimeTypeList;
               break;
            case LIMIT_MONTH:
               this.beginDate = order.getOrderDate().with(TemporalAdjusters.firstDayOfMonth());
               this.endDate = order.getOrderDate().with(TemporalAdjusters.lastDayOfMonth());
               this.mealtimeTypeList = mealtimeTypeList;
               break;
            default:
               this.beginDate = order.getOrderDate();
               this.endDate = order.getOrderDate();
               this.mealtimeTypeList = Objects.nonNull(order.getMealtimeType()) ? CollUtil.newArrayList(new Integer[]{order.getMealtimeType()}) : null;
         }

      }
   }

   public LocalDate getBeginDate() {
      return this.beginDate;
   }

   public LocalDate getEndDate() {
      return this.endDate;
   }

   public List<Integer> getMealtimeTypeList() {
      return this.mealtimeTypeList;
   }

   public RulePriceLimitDTO setBeginDate(final LocalDate beginDate) {
      this.beginDate = beginDate;
      return this;
   }

   public RulePriceLimitDTO setEndDate(final LocalDate endDate) {
      this.endDate = endDate;
      return this;
   }

   public RulePriceLimitDTO setMealtimeTypeList(final List<Integer> mealtimeTypeList) {
      this.mealtimeTypeList = mealtimeTypeList;
      return this;
   }

   public RulePriceLimitDTO(final LocalDate beginDate, final LocalDate endDate, final List<Integer> mealtimeTypeList) {
      this.beginDate = beginDate;
      this.endDate = endDate;
      this.mealtimeTypeList = mealtimeTypeList;
   }

   public RulePriceLimitDTO() {
   }
}
