package com.bonus.core.marketing.v2.rule.recharge.constants;

import com.bonus.core.account.v3.api.dto.AccRechargeSumApiDTO;
import com.bonus.core.marketing.util.EnumUtil;
import com.bonus.core.marketing.util.ICommonEnum;
import com.bonus.core.marketing.v2.rule.recharge.dto.RuleRechargeComputeDTO;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
import java.util.Objects;

public enum RuleRechargeSumEnum implements ICommonEnum {
   LIMIT_DAY(1, "按日") {
      @Override
      public AccRechargeSumApiDTO formatParam(List<Integer> payChannelList, RuleRechargeComputeDTO computeDTO) {
         LocalDateTime startDateTime = computeDTO.getTradeDate().atStartOfDay();
         LocalDateTime endDateTime = computeDTO.getTradeDate().atTime(23, 59, 59);
         return new AccRechargeSumApiDTO(startDateTime, endDateTime, payChannelList, computeDTO.getCustId());
      }
   },
   LIMIT_MONTH(2, "按周") {
      public AccRechargeSumApiDTO formatParam(List<Integer> payChannelList, RuleRechargeComputeDTO computeDTO) {
         LocalDateTime startDateTime = computeDTO.getTradeDate().with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).atStartOfDay();
         LocalDateTime endDateTime = computeDTO.getTradeDate().with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).atTime(23, 59, 59);
         return new AccRechargeSumApiDTO(startDateTime, endDateTime, payChannelList, computeDTO.getCustId());
      }
   },
   LIMIT_MEALTIME(3, "按月") {
      public AccRechargeSumApiDTO formatParam(List<Integer> payChannelList, RuleRechargeComputeDTO computeDTO) {
         LocalDateTime startDateTime = computeDTO.getTradeDate().with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
         LocalDateTime endDateTime = computeDTO.getTradeDate().with(TemporalAdjusters.lastDayOfMonth()).atTime(23, 59, 59);
         return new AccRechargeSumApiDTO(startDateTime, endDateTime, payChannelList, computeDTO.getCustId());
      }
   };

   private final Integer key;
   private final String value;

   public abstract AccRechargeSumApiDTO formatParam(List<Integer> payChannelList, RuleRechargeComputeDTO computeDTO);

   public static AccRechargeSumApiDTO getSumParam(Integer sumType, List<Integer> payChannelList, RuleRechargeComputeDTO computeDTO) {
      return Objects.isNull(sumType) ? new AccRechargeSumApiDTO((LocalDateTime)null, (LocalDateTime)null, payChannelList, computeDTO.getCustId()) : ((RuleRechargeSumEnum) EnumUtil.getEnumByKey(RuleRechargeSumEnum.class, sumType)).formatParam(payChannelList, computeDTO);
   }

   @Override
   public Integer getKey() {
      return this.key;
   }

   @Override
   public String getValue() {
      return this.value;
   }

   private RuleRechargeSumEnum(final Integer key, final String value) {
      this.key = key;
      this.value = value;
   }

   // $FF: synthetic method
   private static RuleRechargeSumEnum[] $values() {
      return new RuleRechargeSumEnum[]{LIMIT_DAY, LIMIT_MONTH, LIMIT_MEALTIME};
   }
}
