package com.bonus.canteen.core.menu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.canteen.core.device.api.DeviceV2Api;
import com.bonus.canteen.core.menu.constant.MenuRecipeSortEnum;
import com.bonus.canteen.core.menu.dto.MenuRecipeSortVo;
import com.bonus.canteen.core.menu.entity.MenuAppRecipe;
import com.bonus.canteen.core.menu.entity.MenuRecipeBindSort;
import com.bonus.canteen.core.menu.mapper.MenuAppRecipeMapper;
import com.bonus.canteen.core.menu.mapper.MenuRecpieBindSortMapper;
import com.bonus.canteen.core.menu.service.MenuRecipeBindSortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.invoke.SerializedLambda;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class MenuRecipeBindSortServiceImpl extends ServiceImpl<MenuRecpieBindSortMapper, MenuRecipeBindSort> implements MenuRecipeBindSortService {

   @Autowired
   private DeviceV2Api deviceV2Api;
   @Autowired
   private MenuAppRecipeMapper menuAppRecipeMapper;

   public List<MenuRecipeSortVo> querySort() {
      Map<Integer, MenuRecipeSortEnum> bindTypeMap = MenuRecipeSortEnum.getKeyMap();
      List<MenuRecipeBindSort> list = this.list();
      List<MenuRecipeSortVo> res;
      if (ObjectUtil.isNotEmpty(list)) {
         res = list.stream().map((item) -> {
            MenuRecipeSortEnum menuRecipeSortEnum = bindTypeMap.get(item.getId());
            MenuRecipeSortVo menuRecipeSortVo = new MenuRecipeSortVo(menuRecipeSortEnum);
            menuRecipeSortVo.setSortNum(item.getSortNum());
            return menuRecipeSortVo;
         }).collect(Collectors.toList());
         Set<Integer> keys = bindTypeMap.keySet();
         List<Integer> haveKeys = list.stream().map(MenuRecipeBindSort::getId).collect(Collectors.toList());
         Objects.requireNonNull(keys);
         haveKeys.forEach(keys::remove);
         Iterator var6 = keys.iterator();

         while(var6.hasNext()) {
            Integer key = (Integer)var6.next();
            MenuRecipeSortEnum menuRecipeSortEnum = bindTypeMap.get(key);
            MenuRecipeSortVo menuRecipeSortVo = new MenuRecipeSortVo(menuRecipeSortEnum);
            menuRecipeSortVo.setSortNum(key);
            res.add(menuRecipeSortVo);
         }
      } else {
         res = bindTypeMap.values().stream().map((item) -> {
            MenuRecipeSortVo menuRecipeSortVo = new MenuRecipeSortVo(item);
            menuRecipeSortVo.setSortNum(item.getKey());
            return menuRecipeSortVo;
         }).collect(Collectors.toList());
      }

      Map<Integer, Integer> countMap = this.deviceV2Api.getDeviceTypeCountMap();
      return res.stream().peek((item) -> {
         if (ObjectUtil.isEmpty(item.getDeviceTypes())) {
            item.setHidden(0);
         } else {
            Iterator var2 = item.getDeviceTypes().iterator();

            while(var2.hasNext()) {
               Integer deviceType = (Integer)var2.next();
               Integer count = (Integer)countMap.get(deviceType);
               if (count != null && count > 0) {
                  item.setHidden(0);
                  break;
               }
            }

         }
      }).sorted(Comparator.comparing(MenuRecipeSortVo::getSortNum)).collect(Collectors.toList());
   }

   public Boolean saveSort(List<MenuRecipeSortVo> content) {
      this.remove(Wrappers.lambdaQuery());
      List<MenuRecipeBindSort> menuRecipeBindSorts = BeanUtil.copyToList(content, MenuRecipeBindSort.class);
      super.saveBatch(menuRecipeBindSorts);
      return true;
   }

   public void deleteByDevice(Integer deviceType) {
      Map<Integer, Integer> countMap = this.deviceV2Api.getDeviceTypeCountMap();
      Arrays.stream(MenuRecipeSortEnum.values()).filter((item) -> {
         return item.getDeviceType().contains(deviceType);
      }).findFirst().ifPresent((item) -> {
         List<Integer> deviceTypes = item.getDeviceType();
         Boolean removeFlage = true;
         Iterator var5 = deviceTypes.iterator();

         while(var5.hasNext()) {
            Integer type = (Integer)var5.next();
            Integer count = (Integer)countMap.get(type);
            if (count != null && count > 0) {
               removeFlage = false;
               break;
            }
         }

         if (removeFlage) {
            this.menuAppRecipeMapper.delete(Wrappers.lambdaQuery(MenuAppRecipe.class)
                    .eq(MenuAppRecipe::getBindType, item.getBindType()));
         }

      });
   }
}
