package com.bonus.canteen.core.device.metadata.api;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.bonus.canteen.core.allocation.api.AllocMealtimeApi;
import com.bonus.canteen.core.allocation.api.AllocMetadataApi;
import com.bonus.canteen.core.common.enums.MetadataModelTypeEnum;
import com.bonus.canteen.core.device.android.constant.DeviceMqMessage;
import com.bonus.canteen.core.device.mettadata.model.DeviceMetadataModel;
import com.bonus.canteen.core.device.mq.MacMessage;
import com.bonus.canteen.core.menu.api.MenuDishesApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.*;

@Component
public class DeviceMetadataApi {
   @Autowired
   @Lazy
   private AllocMetadataApi allocMetadataApi;
//   @Autowired
//   @Lazy
//   private AllocMealtimeApi allocMealtimeApi;
//   @Autowired
//   @Lazy
//   private CPJXxlJobApi cpjXxlJobApi;
//   @Autowired
//   @Lazy
//   private MenuDishesApi menuDishesApi;

//   public DeviceMetadataModel queryBuffetMetadataForWeb() {
//      DeviceMetadataModel deviceMetadataModel = (DeviceMetadataModel)this.allocMetadataApi.queryMetadataModel(MetadataModelTypeEnum.DEVICE_WEIGHT, new DeviceMetadataModel());
//      if (StrUtil.isNotBlank(deviceMetadataModel.getQuotaAmountConfig())) {
//         List<QuotaAmountMealTimeForWebVO> quotaMealtimeVOList = JSONObject.parseArray(deviceMetadataModel.getQuotaAmountConfig(), QuotaAmountMealTimeForWebVO.class);
//         List<Long> dishesIdList = new ArrayList();
//         Iterator var4 = quotaMealtimeVOList.iterator();
//
//         while(var4.hasNext()) {
//            QuotaAmountMealTimeForWebVO quotaAmountMealTimeForWebVO = (QuotaAmountMealTimeForWebVO)var4.next();
//            if (StrUtil.isNotBlank(quotaAmountMealTimeForWebVO.getDishesId())) {
//               dishesIdList.add(Long.valueOf(quotaAmountMealTimeForWebVO.getDishesId()));
//            }
//         }
//
//         List<MenuBaseDishesIdDTO> baseDishesIdDTOList = this.menuDishesApi.listBaseDishedId(dishesIdList, (List)null);
//         Map<String, String> dishesIdMap = new HashMap();
//         Iterator var6 = baseDishesIdDTOList.iterator();
//
//         while(var6.hasNext()) {
//            MenuBaseDishesIdDTO menuBaseDishesIdDTO = (MenuBaseDishesIdDTO)var6.next();
//            dishesIdMap.put(String.valueOf(menuBaseDishesIdDTO.getDishesId()), String.valueOf(menuBaseDishesIdDTO.getBaseDishesId()));
//         }
//
//         quotaMealtimeVOList.forEach((vo) -> {
//            vo.setDishesId((String)dishesIdMap.get(vo.getDishesId()));
//         });
//         deviceMetadataModel.setQuotaAmountConfig(JSONObject.toJSONString(quotaMealtimeVOList));
//      }
//
//      return deviceMetadataModel;
//   }

   public DeviceMetadataModel queryBuffetMetadata() {
      return (DeviceMetadataModel)this.allocMetadataApi.queryMetadataModel(MetadataModelTypeEnum.DEVICE, new DeviceMetadataModel());
   }

//   public DeviceMetadataModel queryBuffetMetadataOrNull() {
//      return (DeviceMetadataModel)this.allocMetadataApi.queryMetadataModelOrNull(MetadataModelTypeEnum.DEVICE, new DeviceMetadataModel());
//   }
//
//   public void saveBuffetMetadata(DeviceMetadataModel deviceMetadataModel) {
//      if (StrUtil.isNotBlank(deviceMetadataModel.getQuotaAmountConfig())) {
//         List<QuotaAmountMealTimeForWebVO> quotaMealtimeVOList = JSONObject.parseArray(deviceMetadataModel.getQuotaAmountConfig(), QuotaAmountMealTimeForWebVO.class);
//         List<Long> baseDishesIdList = new ArrayList();
//         Iterator var4 = quotaMealtimeVOList.iterator();
//
//         while(var4.hasNext()) {
//            QuotaAmountMealTimeForWebVO quotaAmountMealTimeForWebVO = (QuotaAmountMealTimeForWebVO)var4.next();
//            if (StrUtil.isNotBlank(quotaAmountMealTimeForWebVO.getDishesId())) {
//               baseDishesIdList.add(Long.valueOf(quotaAmountMealTimeForWebVO.getDishesId()));
//            }
//         }
//
//         List<MenuBaseDishesIdDTO> dishesIdDTOList = this.menuDishesApi.listBaseDishedId((List)null, baseDishesIdList);
//         Map<String, String> baseDishesIdMap = new HashMap();
//         Iterator var6 = dishesIdDTOList.iterator();
//
//         while(var6.hasNext()) {
//            MenuBaseDishesIdDTO menuBaseDishesIdDTO = (MenuBaseDishesIdDTO)var6.next();
//            baseDishesIdMap.put(String.valueOf(menuBaseDishesIdDTO.getBaseDishesId()), String.valueOf(menuBaseDishesIdDTO.getDishesId()));
//         }
//
//         quotaMealtimeVOList.forEach((vo) -> {
//            vo.setDishesId((String)baseDishesIdMap.get(vo.getDishesId()));
//         });
//         deviceMetadataModel.setQuotaAmountConfig(JSONObject.toJSONString(quotaMealtimeVOList));
//      }
//
//      this.allocMetadataApi.saveMetadataModel(MetadataModelTypeEnum.DEVICE_WEIGHT, deviceMetadataModel);
//      MacMessage.updateDeviceBasicSettingTeantV2(deviceMetadataModel);
//      DeviceMqMessage.deviceUpdateBuffetMerchantV4(deviceMetadataModel);
//   }
//
//   public ProductionMetadataModel queryProductionMetadata() {
//      return (ProductionMetadataModel)this.allocMetadataApi.queryMetadataModel(MetadataModelTypeEnum.DEVICE, new ProductionMetadataModel());
//   }
//
//   public void saveProductionMetadata(ProductionMetadataModel metadataModel) {
//      this.allocMetadataApi.saveMetadataModel(MetadataModelTypeEnum.DEVICE, metadataModel);
//      List<LocalTime> timeList = new ArrayList();
//      Integer grantOutType = Integer.valueOf(metadataModel.getProductAutoGrantOutType());
//      if (ProductionAutoGrantTypeEnum.BEFORE_DINNER.getKey().equals(grantOutType)) {
//         timeList = this.allocMealtimeApi.getStartMealtimeList(-10);
//      } else if (ProductionAutoGrantTypeEnum.AFTER_DINNER.getKey().equals(grantOutType)) {
//         timeList = this.allocMealtimeApi.getEndMealtimeList();
//      } else if (ProductionAutoGrantTypeEnum.CUSTOM.getKey().equals(grantOutType)) {
//         List<String> timeStrList = StrUtil.split(metadataModel.getProductCustomGrantOutTimes(), ',');
//         Iterator var5 = timeStrList.iterator();
//
//         while(var5.hasNext()) {
//            String timeStr = (String)var5.next();
//            ((List)timeList).add(LocalTime.parse(timeStr));
//         }
//      }
//
//      this.cpjXxlJobApi.save((List)timeList);
//   }
}
