package com.bonus.canteen.core.marketing.v2.constants;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.bonus.canteen.core.marketing.util.EnumUtil;
import com.bonus.canteen.core.marketing.util.ICommonEnum;
import com.bonus.canteen.core.marketing.v2.dto.MarketRuleEditDTO;
import com.bonus.canteen.core.marketing.v2.model.MarketRuleRange;
import com.bonus.canteen.core.marketing.v2.rule.param.RuleRangeParam;
import com.bonus.canteen.core.marketing.v2.vo.MarketRuleDetailVO;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public enum MarketModuleTypeEnum implements ICommonEnum {
   RULE_MEAL(1, "就餐规则") {
      @Override
      public boolean exec(RuleRangeParam param, MarketRuleDetailVO detail) {
         if (!MarketEffectTimeEnum.operate(param, detail)) {
            return false;
         } else if (!MarketEffectPeriodEnum.operate(detail, param.getTradeDate())) {
            return false;
         } else {
            return MarketRangeTypeEnum.RANGE_DEVICE.exec(param, detail);
         }
      }

      @Override
      public List<MarketRuleRange> formatRangeDevice(MarketRuleEditDTO editDTO) {
         return formatMealAndPayRange(editDTO);
      }
   },
   RULE_PRICE(2, "计价规则") {
      @Override
      public boolean exec(RuleRangeParam param, MarketRuleDetailVO detail) {
         if (!MarketEffectTimeEnum.operate(param, detail)) {
            return false;
         } else if (!MarketEffectPeriodEnum.operate(detail, param.getTradeDate())) {
            return false;
         } else if (!MarketRangeTypeEnum.RANGE_DEVICE.exec(param, detail)) {
            return false;
         } else if (!MarketRangeTypeEnum.RANGE_MEALTIME.exec(param, detail)) {
            return false;
         } else if (!MarketRangeTypeEnum.RANGE_ORGANIZATION.exec(param, detail)) {
            return false;
         } else if (!MarketRangeTypeEnum.RANGE_PERSON.exec(param, detail)) {
            return false;
         } else {
            return MarketRangeTypeEnum.RANGE_AGE.exec(param, detail);
         }
      }

      @Override
      public List<MarketRuleRange> formatRangeDevice(MarketRuleEditDTO editDTO) {
         return formatMealAndPayRange(editDTO);
      }
   },
   RULE_PAY(3, "扣款规则") {
      @Override
      public boolean exec(RuleRangeParam param, MarketRuleDetailVO detail) {
         if (!MarketEffectTimeEnum.operate(param, detail)) {
            return false;
         } else if (!MarketEffectPeriodEnum.operate(detail, LocalDate.now())) {
            return false;
         } else if (!MarketRangeTypeEnum.RANGE_DEVICE.exec(param, detail)) {
            return false;
         } else if (!MarketRangeTypeEnum.RANGE_ORGANIZATION.exec(param, detail)) {
            return false;
         } else if (!MarketRangeTypeEnum.RANGE_PERSON.exec(param, detail)) {
            return false;
         } else {
            return MarketRangeTypeEnum.RANGE_AGE.exec(param, detail);
         }
      }

      @Override
      public List<MarketRuleRange> formatRangeDevice(MarketRuleEditDTO editDTO) {
         return formatMealAndPayRange(editDTO);
      }
   },
   RULE_RECHARGE(4, "充值规则") {
      @Override
      public boolean exec(RuleRangeParam param, MarketRuleDetailVO detail) {
         if (!MarketEffectTimeEnum.operate(param, detail)) {
            return false;
         } else if (!MarketEffectPeriodEnum.operate(detail, param.getTradeDate())) {
            return false;
         } else if (!MarketRangeTypeEnum.RANGE_PAY_CHANNEL.exec(param, detail)) {
            return false;
         } else if (!MarketRangeTypeEnum.RANGE_ORGANIZATION.exec(param, detail)) {
            return false;
         } else if (!MarketRangeTypeEnum.RANGE_PERSON.exec(param, detail)) {
            return false;
         } else {
            return MarketRangeTypeEnum.RANGE_AGE.exec(param, detail);
         }
      }

      @Override
      public List<MarketRuleRange> formatRangeDevice(MarketRuleEditDTO editDTO) {
         return new ArrayList();
      }
   };

   private final Integer key;
   private final String value;

   public static String getMetadataFileKey(Integer moduleType) {
      MarketModuleTypeEnum type = (MarketModuleTypeEnum) EnumUtil.getEnumByKey(MarketModuleTypeEnum.class, moduleType);
      if (Objects.isNull(type)) {
         return null;
      } else {
         String metadataFileKey = null;
         switch (type.ordinal()) {
            case 0:
               metadataFileKey = "ifUseRuleMeal";
               break;
            case 1:
               metadataFileKey = "ifUseRulePrice";
               break;
            case 2:
               metadataFileKey = "ifUseRulePay";
               break;
            case 3:
               metadataFileKey = "ifUseRuleRecharge";
         }

         return metadataFileKey;
      }
   }

   public abstract boolean exec(RuleRangeParam param, MarketRuleDetailVO detail);

   public abstract List<MarketRuleRange> formatRangeDevice(MarketRuleEditDTO editDTO);

   public static boolean operate(RuleRangeParam param, MarketRuleDetailVO detail) {
      MarketModuleTypeEnum moduleEnum = (MarketModuleTypeEnum)EnumUtil.getEnumByKey(MarketModuleTypeEnum.class, param.getModuleType());
      return !Objects.isNull(moduleEnum) && moduleEnum.exec(param, detail);
   }

   public static List<MarketRuleRange> formatMealAndPayRange(MarketRuleEditDTO editDTO) {
      List<MarketRuleRange> rangeList = new ArrayList();
      if (CollUtil.isEmpty(editDTO.getDeviceList())) {
         return rangeList;
      } else {
         editDTO.getDeviceList().forEach((e) -> {
            if (CharSequenceUtil.contains(e, "&")) {
               List<String> split = CharSequenceUtil.split(e, "&");
               if (!CharSequenceUtil.contains(e, MarketDeviceTreeEnum.MOBILE.getDesc()) && !CharSequenceUtil.contains(e, MarketDeviceTreeEnum.BUFFET.getDesc())) {
                  rangeList.add(MarketRuleRange.builder().ruleId(editDTO.getRuleId()).rangeType(MarketRangeTypeEnum.RANGE_DEVICE.getKey()).canteenId(CharSequenceUtil.isNotBlank((CharSequence)split.get(0)) ? Long.parseLong((String)split.get(0)) : null).stallId(CharSequenceUtil.isNotBlank((CharSequence)split.get(1)) ? Long.parseLong((String)split.get(1)) : null).deviceSn((String)split.get(2)).customSn(e).build());
               } else {
                  rangeList.add(MarketRuleRange.builder().ruleId(editDTO.getRuleId()).rangeType(MarketRangeTypeEnum.RANGE_DEVICE.getKey()).canteenId(CharSequenceUtil.isNotBlank((CharSequence)split.get(1)) ? Long.parseLong((String)split.get(1)) : null).stallId(CharSequenceUtil.isNotBlank((CharSequence)split.get(2)) ? Long.parseLong((String)split.get(2)) : null).deviceSn((String)split.get(0)).customSn(e).build());
               }
            } else {
               rangeList.add(MarketRuleRange.builder().ruleId(editDTO.getRuleId()).rangeType(MarketRangeTypeEnum.RANGE_DEVICE.getKey()).customSn(e).build());
            }

         });
         return rangeList;
      }
   }

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

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

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

   // $FF: synthetic method
   private static MarketModuleTypeEnum[] $values() {
      return new MarketModuleTypeEnum[]{RULE_MEAL, RULE_PRICE, RULE_PAY, RULE_RECHARGE};
   }
}
