package com.bonus.canteen.core.device.android.common.service;

import com.bonus.canteen.core.common.redis.RedisUtil;
import com.bonus.canteen.core.common.utils.JavaxValidateUtils;
import com.bonus.canteen.core.common.utils.TenantContextHolder;
import com.bonus.canteen.core.device.android.common.dto.MenuSendByDTO;
import com.bonus.canteen.core.device.android.common.dto.MenuSendDTO;
import com.bonus.canteen.core.device.android.constant.DeviceMqMessage;
import com.bonus.canteen.core.device.manage.dto.DeviceSearchDTO;
import com.bonus.canteen.core.device.manage.model.DeviceInfo;
import com.bonus.canteen.core.device.manage.service.DeviceInfoService;
import com.bonus.canteen.core.device.mq.MacMessage;
import com.bonus.canteen.core.menu.api.MenuRecipeApi;
import com.bonus.canteen.core.menu.dto.IssueRecipeV2DTO;
import com.bonus.canteen.core.menu.vo.IssueRecipeCompressVO;
import com.bonus.common.houqin.constant.LeConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

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

@Service
public class DeviceMenuService {
   private static final Logger log = LoggerFactory.getLogger(DeviceMenuService.class);
   @Autowired
   @Lazy
   private MenuRecipeApi menuRecipeApi;
   @Autowired
   @Lazy
   private DeviceInfoService deviceInfoService;
//   @Autowired
//   @Lazy
//   private DeviceApi deviceApi;
//   @Autowired
//   @Lazy
//   private MenuDishesApi menuDishesApi;
//   @Autowired
//   @Lazy
//   private AndroidMenuService androidMenuService;

   public void sendMenuByCanteen(Long recipeId, List<MenuSendByDTO> menuSendByDTOS) {
      List<DeviceInfo> deviceInfoList = new ArrayList<>();
      menuSendByDTOS.forEach((menuSendByDTO) -> {
         DeviceSearchDTO deviceSearchDTO = new DeviceSearchDTO();
         deviceSearchDTO.setCanteenId(menuSendByDTO.getCanteenId());
         deviceSearchDTO.setStallId(menuSendByDTO.getStallId());
         deviceSearchDTO.setMealLineId(menuSendByDTO.getMealLineId());
         deviceSearchDTO.setDeviceTypeList(menuSendByDTO.getDeviceTypeList());
         deviceInfoList.addAll(this.deviceInfoService.list(deviceSearchDTO));
      });
      this.sendMenu(new MenuSendDTO(recipeId, deviceInfoList));
      deviceInfoList.forEach((s) -> {
         DeviceMqMessage.deviceUpdateMenuConfigV4(s.getDeviceSn());
      });
   }

   public void sendMenu(MenuSendDTO menuSendDTO) {
      JavaxValidateUtils.validate(menuSendDTO);
      IssueRecipeCompressVO issueRecipeCompressVO = this.menuRecipeApi.issueRecipeV2Compress(new IssueRecipeV2DTO(menuSendDTO.getRecipeId(), LocalDate.now(), LocalDate.now().plusDays(6L)));
      menuSendDTO.getDeviceInfoList().forEach((deviceInfo) -> {
         if (LeConstants.COMMON_YES.equals(deviceInfo.getOnlineState())) {
            RedisUtil.setString("yunshitang:merchantId:recipesendtype:macNumber".replace("merchantId", TenantContextHolder.getTenantId().toString()).replace("macNumber", deviceInfo.getDeviceNum()), "3");
            MacMessage.distributeRecipe(issueRecipeCompressVO, deviceInfo.getDeviceSn());
         }

      });
   }
//
//   public IssueRecipeCompressVO queryBySn(String machineSn) {
//      Long recipeId = this.deviceApi.getFirstRecipeIdBySn(machineSn);
//      return recipeId == null ? new IssueRecipeCompressVO() : this.queryByRecipeId(recipeId);
//   }
//
//   public IssueRecipeCompressVO queryByRecipeId(Long recipeId) {
//      IssueRecipeV2DTO issueRecipeV2DTO = new IssueRecipeV2DTO(recipeId, LocalDate.now(), LocalDate.now().plusDays(6L));
//      return this.menuRecipeApi.issueRecipeV2Compress(issueRecipeV2DTO);
//   }
//
//   public List<MenuDishesTypeAllVO> getAllMenuDishesTypeList() {
//      List<MenuDishesType> allMenuDishesType = this.menuDishesApi.getAllMenuDishesType();
//      return BeanUtil.copyToList(allMenuDishesType, MenuDishesTypeAllVO.class);
//   }
//
//   public void addAndroidOrderEvaluateSelfTerminal(MenuEvaluateSelfAddDTO menuEvaluateSelfAddDTO) {
//      this.androidMenuService.addAndroidOrderEvaluateSelfTerminal(menuEvaluateSelfAddDTO);
//   }
//
//   public Page<AndroidDishesEvaluateVO> getAndroidDishesEvaluatePage(AndroidDishesEvaluateDTO content) {
//      return this.androidMenuService.getAndroidDishesEvaluatePage(content);
//   }
//
//   public List<AndroidRecipeTypeDetailVO> getProductionMachineRecipe() {
//      return this.menuRecipeApi.getProductionMachineRecipe();
//   }
//
//   public List<AndroidRecipeDetailV2VO> getAndroidRecipeDetail(Map<String, String> headers, AndroidRecipeDetailV2DTO content) {
//      return this.androidMenuService.getAndroidRecipeDetail(headers, content);
//   }
//
//   public List<AndroidGetRecipeDishesVO> androidGetRecipeDishes(Map<String, String> headers, AndroidGetRecipeDishesDTO content) {
//      return this.menuRecipeApi.androidGetRecipeDishes(content);
//   }
//
//   public AddDishesVo deviceAddDishes(DeviceAddDishesDto content) {
//      return this.menuDishesApi.deviceAddDishes(content);
//   }
//
//   public List<MenuRecipe> deviceSearchRecipe(DeviceSearchRecipeDto content) {
//      return this.menuRecipeApi.deviceSearchRecipe(content);
//   }
//
//   public PageVO<AndroidRecipeTypeDetailVO> getAllDishesByPage(PageDTO content) {
//      return this.menuRecipeApi.getAllDishesByPage(content);
//   }
//
//   public void deviceAddDishesToRecipe(String machineSn, AndroidAddRecipeDishesDTO content) {
//      Long recipeId = this.deviceApi.getFirstRecipeIdBySn(machineSn);
//      if (recipeId == null) {
//         throw new LeException(I18n.getMessage("menu_mac_no_recipe", new Object[0]));
//      } else {
//         content.setRecipeId(recipeId);
//         this.menuRecipeApi.androidAddRecipeDishes(content);
//      }
//   }
}
