package com.bonus.core.marketing.v2.rule.meal.dto.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.bonus.constant.LeConstants;
import com.bonus.core.customer.api.CustInfoApi;
import com.bonus.core.customer.constants.CustConstant;
import com.bonus.core.customer.model.CustInfo;
import com.bonus.core.marketing.util.EnumUtil;
import com.bonus.core.marketing.v2.rule.meal.constants.RuleMealEnum;
import com.bonus.core.marketing.v2.rule.meal.handler.RuleMealHandler;
import com.bonus.core.marketing.v2.rule.meal.handler.RuleMealProvider;
import com.bonus.core.marketing.v2.rule.param.RuleRangeParam;
import com.bonus.core.marketing.v2.rule.price.dto.RulePriceDTO;
import com.bonus.core.marketing.v2.rule.price.dto.RulePriceOrderDTO;
import com.bonus.core.marketing.v2.service.MarketRuleService;
import com.bonus.core.marketing.vo.MarketRuleVO;
import com.bonus.core.order.common.constants.OrderTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class RuleMealApi {
   private static final Logger log = LoggerFactory.getLogger(RuleMealApi.class);
   @Autowired
   private RuleMealProvider ruleMealProvider;
   @Autowired
   private MarketRuleService marketRuleService;
   @Autowired
   private CustInfoApi custInfoApi;

   public void compute(RulePriceDTO rulePriceDTO) {
      log.info("【就餐规则】开始");
      CustInfo custInfo = this.custInfoApi.getCustInfoByCustId(Objects.isNull(rulePriceDTO.getCustId())
              ? CustConstant.TEMPORARY_CUST_ID : rulePriceDTO.getCustId());
      List<RulePriceOrderDTO> orders = rulePriceDTO.getOrders();
      orders.forEach((order) -> {
         if (!this.checkNotAllowRule(order, rulePriceDTO)) {
            RuleRangeParam ruleRangeParam = RuleRangeParam.ruleMealRangeParam(order, custInfo, rulePriceDTO.getVerifyFlag());
            log.info("【就餐规则】查找合适规则入参：{}", JSONUtil.toJsonStr(ruleRangeParam));
            List<MarketRuleVO> suitableRules = this.marketRuleService.getSuitableRules(ruleRangeParam);
            if (CollUtil.isEmpty(suitableRules)) {
               log.info("【就餐规则】订单：{}，未匹配到对应规则", order.getOrderId());
            } else {
               suitableRules.stream().filter((e) -> {
                  return RuleMealEnum.RULE_TIME_LIMIT.getKey().equals(e.getRuleType());
               }).forEach((e) -> {
                  this.handle(custInfo, e);
               });
               log.info("【就餐规则】结束");
            }
         }
      });
   }

   protected void handle(CustInfo custInfo, MarketRuleVO ruleInfo) {
      RuleMealHandler handler = this.ruleMealProvider.getMarketMealHandler(ruleInfo.getRuleType());
      if (Objects.isNull(handler)) {
         log.info("【就餐规则】规则类型：{}，未找到对应规则类型的实现方法", ruleInfo.getRuleType());
      } else {
         log.info("【就餐规则】规则类型：{}，规则详情：{}", ((RuleMealEnum) EnumUtil.getEnumByKey(RuleMealEnum.class, ruleInfo.getRuleType())).getValue(), JSONUtil.toJsonStr(ruleInfo));
         handler.handle(custInfo, ruleInfo);
      }
   }

   protected boolean checkNotAllowRule(RulePriceOrderDTO order, RulePriceDTO rulePriceDTO) {
      if (Objects.equals(order.getIfOnline(), LeConstants.COMMON_NO)) {
         log.info("【就餐规则】离线订单不走就餐规则");
         return true;
      } else if (!Objects.equals(order.getOrderType(), OrderTypeEnum.ANDROID.getKey()) && !Objects.equals(order.getOrderType(), OrderTypeEnum.SHOP.getKey()) && !Objects.equals(order.getOrderType(), OrderTypeEnum.BUS.getKey()) && !Objects.equals(order.getOrderType(), OrderTypeEnum.BUFFET.getKey()) && !Objects.equals(rulePriceDTO.getVerifyFlag(), LeConstants.COMMON_YES)) {
         log.info("【就餐规则】不是线下消费、商超、班车、自助餐或者核销，不允许使用就餐规则");
         return true;
      } else {
         return false;
      }
   }
}
