package com.bonus.canteen.core.nutrition.common.service;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.bonus.canteen.core.customer.constants.CustBMIEnum;
import com.bonus.canteen.core.customer.constants.CustChronicEnum;
import com.bonus.canteen.core.customer.constants.PersonalSexEnum;
import com.bonus.canteen.core.menu.constant.NutritionConstants;
import com.bonus.canteen.core.nutrition.common.enums.DoctorAdviceEnum;
import com.bonus.canteen.core.nutrition.common.po.DishNutritionParam;
import com.bonus.canteen.core.nutrition.common.vo.HealthDishRecommendScoreVO;
import com.bonus.canteen.core.nutrition.common.vo.HealthInfoSimpleVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HealthDishRecommendScoreService {
   private static final Logger log = LoggerFactory.getLogger(HealthDishRecommendScoreService.class);
   @Autowired
   private HealthInfoService healthInfoService;

   public HealthDishRecommendScoreVO getDishesRecommendScore(Long custId, List<DishNutritionParam> dishNutritionParamList) {
      HealthDishRecommendScoreVO healthDishRecommendScoreVO = new HealthDishRecommendScoreVO();
      HealthInfoSimpleVo healthInfo = this.healthInfoService.simplifyHealthInfo(custId);
      if (ObjectUtils.isEmpty(healthInfo)) {
         return healthDishRecommendScoreVO.setRecommendFlag(false);
      } else {
         double fat;
         if (PersonalSexEnum.MALE.getKey().equals(healthInfo.getSex())) {
            fat = NutritionConstants.CALORIES_MALE.multiply(NutritionConstants.STANDARD_VAL1).multiply(NutritionConstants.STANDARD_VAL2).divide(NutritionConstants.STANDARD_VAL3, NutritionConstants.NEW_SCALE, RoundingMode.HALF_UP).doubleValue();
         } else {
            fat = NutritionConstants.CALORIES_FEMALE.multiply(NutritionConstants.STANDARD_VAL1).multiply(NutritionConstants.STANDARD_VAL2).divide(NutritionConstants.STANDARD_VAL3, NutritionConstants.NEW_SCALE, RoundingMode.HALF_UP).doubleValue();
         }

         Map<Long, Integer> dishRecommendMap = new HashMap();
         dishNutritionParamList.forEach((dishNutritionParam) -> {
            Integer initialScore = dishNutritionParam.getInitialScore();
            Integer doctorAdvice = healthInfo.getDoctorAdvice();
            if (ObjectUtils.isNotNull(new Object[]{doctorAdvice}) && dishNutritionParam.classifyNameNotNull() && DoctorAdviceEnum.getDesc(doctorAdvice).contains(dishNutritionParam.getClassifyName())) {
               initialScore = initialScore - NutritionConstants.SCORE_HUNDRED;
            }

            String chronicName = healthInfo.getChronicNames();
            if (StrUtil.isNotBlank(chronicName)) {
               if (this.checkPurineChronic(chronicName) && dishNutritionParam.checkPurine()) {
                  initialScore = initialScore - NutritionConstants.SCORE_SIXTY;
               }

               if (chronicName.contains(CustChronicEnum.HYPERTENSION.getDesc()) && (dishNutritionParam.checkHypertension() || dishNutritionParam.getFat() > fat)) {
                  initialScore = initialScore - NutritionConstants.SCORE_SIXTY;
               }

               if (chronicName.contains(CustChronicEnum.HYPERTHYROIDISM.getDesc()) && dishNutritionParam.getIodine() > NutritionConstants.IODINE_VAL.doubleValue()) {
                  initialScore = initialScore - NutritionConstants.SCORE_SIXTY;
               }

               if (chronicName.contains(CustChronicEnum.DIABETES.getDesc()) && dishNutritionParam.getSodium() > NutritionConstants.SODIUM_VAL.doubleValue()) {
                  initialScore = initialScore - NutritionConstants.SCORE_SIXTY;
               }
            }

            BigDecimal bmi = healthInfo.getBmi();
            if (this.checkBMI(bmi) && dishNutritionParam.getFat() > fat) {
               initialScore = initialScore - NutritionConstants.SCORE_SIXTY;
            }

            if (initialScore < NutritionConstants.SCORE_TWENTY) {
               initialScore = NutritionConstants.SCORE_TWENTY;
            }

            dishRecommendMap.put(dishNutritionParam.getDishesId(), initialScore);
         });
         return healthDishRecommendScoreVO.setRecommendFlag(true).setDishRecommendMap(dishRecommendMap);
      }
   }

   public boolean checkPurineChronic(String chronicNames) {
      return chronicNames.contains(CustChronicEnum.HYPERURICEMIA.getDesc()) || chronicNames.contains(CustChronicEnum.GOUT.getDesc());
   }

   public boolean checkBMI(BigDecimal bmi) {
      bmi = ObjectUtil.isNull(bmi) ? BigDecimal.ZERO : bmi;
      return NumberUtil.isGreater(bmi, BigDecimal.valueOf(CustBMIEnum.NORMAL.getKey()));
   }
}
