package com.bonus.canteen.core.notice.print.v2.business.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.bonus.canteen.core.common.thirdutils.jiabo.JbPrinterKeepLive;
import com.bonus.canteen.core.notice.print.v2.constant.PrintColumnTypeEnum;
import com.bonus.canteen.core.notice.print.v2.constant.PrinterDeviceTypeEnum;
import com.bonus.canteen.core.notice.print.v2.po.PrintResponsePO;
import com.bonus.canteen.core.notice.print.v2.utils.NoticePrintTransformUtil;
import com.bonus.canteen.core.notice.print.v2.vo.NoticePrintTemplateSettingVO;
import com.bonus.canteen.core.order.api.vo.NoticePrintDetailVO;
import com.bonus.canteen.core.order.api.vo.NoticePrintNutritionVO;
import com.bonus.common.houqin.constant.LeConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

@Service
public class NoticePrinterJiaBoLabelBusiness extends NoticePrinterAbstractBusiness {
   private static final Logger log = LoggerFactory.getLogger(NoticePrinterJiaBoLabelBusiness.class);

   public Integer getPrintStatus(String sn, String ip) {
      if (ObjectUtil.isEmpty(ip)) {
         return LeConstants.COMMON_NO;
      } else {
         try {
            String status = JbPrinterKeepLive.getPrinter().queryOnlineStatus(ip);
            return ObjectUtil.isEmpty(status) ? LeConstants.COMMON_NO : LeConstants.COMMON_YES;
         } catch (Exception var4) {
            log.info("【佳博标签打印机查询状态】错误：{}", var4.getMessage(), var4);
            return LeConstants.COMMON_NO;
         }
      }
   }

//   protected PrintResponsePO executePrint(Map<String, String> paramMap, List<NoticePrintTemplateSettingVO> settingList, String sn, String ip, Long recordId) {
//      String size = (String)paramMap.get("printSize");
//      if (ObjectUtil.isEmpty(size)) {
//         size = "40,30";
//      }
//
//      String[] sizeArr = size.split(",");
//      int paperWidth = (new BigDecimal(sizeArr[0])).intValue();
//      int paperHeight = (new BigDecimal(sizeArr[1])).intValue();
//      LabelCommand tsc = new LabelCommand();
//      tsc.addSize(paperWidth, paperHeight);
//      tsc.addGap(2);
//      tsc.addDirection(DIRECTION.FORWARD, MIRROR.NORMAL);
//      tsc.addQueryPrinterStatus(RESPONSE_MODE.ON);
//      tsc.addCls();
//      settingList.forEach((setting) -> {
//         PrintColumnTypeEnum printColumnTypeEnum = PrintColumnTypeEnum.getPrintColumnTypeEnum(setting.getColumnType());
//         if (ObjectUtil.isNull(printColumnTypeEnum)) {
//            printColumnTypeEnum = PrintColumnTypeEnum.TEXT;
//         }
//
//         String paramValue = NoticePrintTransformUtil.getSettingParamValue(setting, paramMap);
//         if (!ObjectUtil.isEmpty(paramValue)) {
//            if (this.noticePrintBurialPointBusiness.checkSpecialPrintColumn(setting)) {
//               this.noticePrintBurialPointBusiness.didJiaboLabelSpecialPrintColumn(setting, paramValue, tsc);
//            } else {
//               switch (printColumnTypeEnum) {
//                  case TEXT:
//                     this.addText(tsc, setting, paramValue);
//                     break;
//                  case TEXT_ARRAY:
//                     this.addArray(tsc, NumberUtil.mul(new BigDecimal(sizeArr[1]), BigDecimal.valueOf(8L)).intValue(), setting, paramValue);
//                     break;
//                  case QR:
//                     String qr = NoticePrintTransformUtil.getOrderQRCode((String)paramMap.get("sourceType"), paramValue);
//                     this.addQr(tsc, setting, qr);
//               }
//
//            }
//         }
//      });
//      tsc.addPrint(1, 1);
//      Vector<Byte> bytes = (Vector)ObjectUtil.clone(tsc.getCommand());
//
//      try {
//         JbPrinterKeepLive.getPrinter().executePrint(bytes, ip, PrinterDeviceTypeEnum.LABEL_PRINTER.getKey());
//         return PrintResponsePO.getSuccessPO((String)null);
//      } catch (Exception var13) {
//         log.info("佳博打印机打印失败：ip:{},msg:{}", ip, var13.getMessage());
//         return PrintResponsePO.getFailPO((String)null);
//      }
//   }
//
//   private void addText(LabelCommand tsc, NoticePrintTemplateSettingVO setting, String paramValue) {
//      tsc.addText(setting.getSettingX(), setting.getSettingY(), FONTTYPE.SIMPLIFIED_CHINESE, ROTATION.ROTATION_0, NoticePrintTransformUtil.getFontMul(setting.getSettingW()), NoticePrintTransformUtil.getFontMul(setting.getSettingH()), paramValue);
//   }
//
//   private void addArray(LabelCommand tsc, Integer paperHeight, NoticePrintTemplateSettingVO setting, String paramValue) {
//      Integer x = setting.getSettingX();
//      Integer y = setting.getSettingY();
//      LabelCommand.FONTMUL wFontMul = NoticePrintTransformUtil.getFontMul(setting.getSettingW());
//      LabelCommand.FONTMUL hFontMul = NoticePrintTransformUtil.getFontMul(setting.getSettingH());
//      if (ObjectUtil.equal("canteenDishesList", setting.getColumnValue())) {
//         this.addDishesList(tsc, x, y, wFontMul, hFontMul, paperHeight, paramValue);
//      } else if (ObjectUtil.equal("nutritionList", setting.getColumnValue())) {
//         this.addNutritionList(tsc, x, y, wFontMul, hFontMul, paramValue);
//      }
//
//   }
//
//   private void addDishesList(LabelCommand tsc, Integer x, Integer y, LabelCommand.FONTMUL wFontMul, LabelCommand.FONTMUL hFontMul, Integer paperHeight, String paramValue) {
//      List<NoticePrintDetailVO> ordDetailList = JSON.parseArray(paramValue, NoticePrintDetailVO.class);
//
//      for(Iterator var9 = ordDetailList.iterator(); var9.hasNext(); y = y + 24) {
//         NoticePrintDetailVO printOrdDetail = (NoticePrintDetailVO)var9.next();
//         if (y + 24 > paperHeight) {
//            break;
//         }
//
//         String goodsName = printOrdDetail.getGoodsDishesName().length() > 5 ? printOrdDetail.getGoodsDishesName().substring(0, 5) + "..." : printOrdDetail.getGoodsDishesName();
//         tsc.addText(x, y, FONTTYPE.SIMPLIFIED_CHINESE, ROTATION.ROTATION_0, wFontMul, hFontMul, goodsName);
//         tsc.addText(x + 132, y, FONTTYPE.SIMPLIFIED_CHINESE, ROTATION.ROTATION_0, wFontMul, hFontMul, "*" + printOrdDetail.getQuantity());
//      }
//
//   }
//
//   private void addNutritionList(LabelCommand tsc, Integer x, Integer y, LabelCommand.FONTMUL wFontMul, LabelCommand.FONTMUL hFontMul, String paramValue) {
//      NoticePrintNutritionVO nutritionVO = (NoticePrintNutritionVO)JSON.parseObject(paramValue, NoticePrintNutritionVO.class);
//      Double calories = ObjectUtil.isNotNull(nutritionVO.getCalories()) ? nutritionVO.getCalories() : 0.0;
//      Double protein = ObjectUtil.isNotNull(nutritionVO.getProtein()) ? nutritionVO.getProtein() : 0.0;
//      Double fat = ObjectUtil.isNotNull(nutritionVO.getFat()) ? nutritionVO.getFat() : 0.0;
//      Double carbohydrate = ObjectUtil.isNotNull(nutritionVO.getCarbohydrate()) ? nutritionVO.getCarbohydrate() : 0.0;
//      Double dietaryFiber = ObjectUtil.isNotNull(nutritionVO.getDietaryFiber()) ? nutritionVO.getDietaryFiber() : 0.0;
//      Double cholesterol = ObjectUtil.isNotNull(nutritionVO.getCholesterol()) ? nutritionVO.getCholesterol() : 0.0;
//      tsc.addText(x, y, FONTTYPE.SIMPLIFIED_CHINESE, ROTATION.ROTATION_0, wFontMul, hFontMul, NoticePrintTransformUtil.getNutritionValue("notice_print_nutrition_calories", String.valueOf(calories), "kj"));
//      tsc.addText(x + 144, y, FONTTYPE.SIMPLIFIED_CHINESE, ROTATION.ROTATION_0, wFontMul, hFontMul, NoticePrintTransformUtil.getNutritionValue("notice_print_nutrition_protein", String.valueOf(protein), "g"));
//      y = y + 24;
//      tsc.addText(x, y, FONTTYPE.SIMPLIFIED_CHINESE, ROTATION.ROTATION_0, wFontMul, hFontMul, NoticePrintTransformUtil.getNutritionValue("notice_print_nutrition_fat", String.valueOf(fat), "g"));
//      tsc.addText(x + 144, y, FONTTYPE.SIMPLIFIED_CHINESE, ROTATION.ROTATION_0, wFontMul, hFontMul, NoticePrintTransformUtil.getNutritionValue("notice_print_nutrition_carbohydrate", String.valueOf(carbohydrate), "g"));
//      y = y + 24;
//      tsc.addText(x, y, FONTTYPE.SIMPLIFIED_CHINESE, ROTATION.ROTATION_0, wFontMul, hFontMul, NoticePrintTransformUtil.getNutritionValue("notice_print_nutrition_dietary_fiber", String.valueOf(dietaryFiber), "g"));
//      tsc.addText(x + 144, y, FONTTYPE.SIMPLIFIED_CHINESE, ROTATION.ROTATION_0, wFontMul, hFontMul, NoticePrintTransformUtil.getNutritionValue("notice_print_nutrition_cholesterol", String.valueOf(cholesterol), "g"));
//   }
//
//   private void addQr(LabelCommand tsc, NoticePrintTemplateSettingVO setting, String qr) {
//      tsc.addQRCode(setting.getSettingX(), setting.getSettingY(), EEC.LEVEL_L, 5, ROTATION.ROTATION_0, qr);
//   }
}
