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.feie.FeiePrint;
import com.bonus.canteen.core.common.thirdutils.feie.FeiePrintResponse;
import com.bonus.canteen.core.notice.print.v2.constant.PrintColumnGroupEnum;
import com.bonus.canteen.core.notice.print.v2.constant.PrintColumnTypeEnum;
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.common.core.exception.ServiceException;
import com.bonus.common.houqin.i18n.I18n;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.*;

@Service
public class NoticePrinterFeiEBusiness extends NoticePrinterAbstractBusiness {
   @Autowired
   @Lazy
   private FeiePrint feiePrint;

   public Integer getPrintStatus(String sn, String ip) {
      return this.feiePrint.queryPrinterStatus(sn);
   }

   protected PrintResponsePO executePrint(Map<String, String> paramMap, List<NoticePrintTemplateSettingVO> settingList, String sn, String ip, Long recordId) {
      StringBuilder sb = new StringBuilder();
      settingList.sort(Comparator.comparing(NoticePrintTemplateSettingVO::getSort));
      Iterator var7 = settingList.iterator();

      while(var7.hasNext()) {
         NoticePrintTemplateSettingVO setting = (NoticePrintTemplateSettingVO)var7.next();
         Integer settingGroup = setting.getColumnGroup();
         String paramValue = this.getParamValue(setting, paramMap);
         if (!ObjectUtil.isEmpty(paramValue)) {
            if (this.noticePrintBurialPointBusiness.checkSpecialPrintColumn(setting)) {
               this.noticePrintBurialPointBusiness.didFeieSpecialPrintColumn(setting, paramValue, sb);
            } else if (ObjectUtil.equal(settingGroup, PrintColumnGroupEnum.TITLE.getKey())) {
               sb.append("<CB>").append(paramValue).append("</CB>");
            } else {
               PrintColumnTypeEnum printColumnTypeEnum = PrintColumnTypeEnum.getPrintColumnTypeEnum(setting.getColumnType());
               if (!ObjectUtil.isNull(printColumnTypeEnum)) {
                  switch (printColumnTypeEnum) {
                     case TEXT:
                        if (this.checkBoldColumns(setting)) {
                           sb.append("<L>").append(paramValue).append("</L><BR>");
                        } else {
                           sb.append(paramValue).append("<BR>");
                        }
                        break;
                     case TEXT_ARRAY:
                        this.addArray(sb, setting, paramValue);
                        break;
                     case QR:
                        String qr = NoticePrintTransformUtil.getOrderQRCode((String)paramMap.get("sourceType"), paramValue);
                        sb.append("<QR>").append(qr).append("</QR>");
                  }
               }
            }
         }
      }

      sb.append("------------------------------<BR>");
      sb.append(I18n.getMessage("notice_print_thanks_msg", new Object[0]));
      FeiePrintResponse print = this.feiePrint.print(sn, sb.toString(), "1");
      if (ObjectUtil.isNull(print)) {
         throw new ServiceException("飞鹅打印未响应");
      } else if (print.isSuccess()) {
         return PrintResponsePO.getSuccessPO(print.getData());
      } else {
         return PrintResponsePO.getFailPO(print.getMsg());
      }
   }

   private String getParamValue(NoticePrintTemplateSettingVO setting, Map<String, String> paramMap) {
      String paramValue = NoticePrintTransformUtil.getPrintParamValue(setting, paramMap);
      if (ObjectUtil.isEmpty(paramValue)) {
         return null;
      } else {
         return ObjectUtil.isEmpty(setting.getColumnName()) ? paramValue : I18n.getMessage(setting.getColumnName(), new Object[]{paramValue});
      }
   }

   private void addArray(StringBuilder sb, NoticePrintTemplateSettingVO setting, String paramValue) {
      this.addDishesList(sb, paramValue, this.checkBoldColumns(setting));
   }

   private void addDishesList(StringBuilder sb, String dishesStr, boolean boldFlag) {
      List<NoticePrintDetailVO> ordDetailList = JSON.parseArray(dishesStr, NoticePrintDetailVO.class);
      if (!ObjectUtil.isEmpty(ordDetailList)) {
         int nameLength = 16;
         sb.append(NoticePrintTransformUtil.flushLeft(' ', (long)nameLength, I18n.getMessage("notice_print_order_detail_name", new Object[0]))).append(" ");
         sb.append(NoticePrintTransformUtil.flushLeft(' ', 5L, I18n.getMessage("notice_print_order_detail_price", new Object[0]))).append(" ");
         sb.append(NoticePrintTransformUtil.flushLeft(' ', 4L, I18n.getMessage("notice_print_order_detail_num", new Object[0]))).append(" ");
         sb.append(I18n.getMessage("notice_print_order_detail_total_amount", new Object[0])).append("<BR>------------------------------<BR>");

         StringBuilder goodsName;
         for(Iterator var6 = ordDetailList.iterator(); var6.hasNext(); sb.append(goodsName).append("<BR>")) {
            NoticePrintDetailVO ordDetail = (NoticePrintDetailVO)var6.next();
            StringBuilder otherSb = new StringBuilder();
            otherSb.append(" ");
            String qunatity = String.valueOf(ordDetail.getQuantity());
            if (boldFlag) {
               qunatity = "<L>" + qunatity + "</L>";
            }

            NumberUtil.mul(ordDetail.getPrice(), ordDetail.getQuantity());
            otherSb.append(NoticePrintTransformUtil.flushLeft(' ', 7L, (String)Objects.requireNonNull(NoticePrintTransformUtil.transformAmountYuan(ordDetail.getPrice())))).append(NoticePrintTransformUtil.flushLeft(' ', 3L, qunatity)).append(this.getDetailTotalPrice(ordDetail));
            goodsName = new StringBuilder(ordDetail.getGoodsDishesName());
            int tl = 0;

            try {
               tl = goodsName.toString().getBytes("GBK").length;
            } catch (UnsupportedEncodingException var18) {
            }

            int spaceNum = (tl / nameLength + 1) * nameLength - tl;
            if (tl < nameLength) {
               goodsName = (new StringBuilder()).append("<L>").append(goodsName).append("</L>");

               for(int k = 0; k < spaceNum; ++k) {
                  goodsName.append(" ");
               }

               goodsName.append(otherSb);
            } else if (tl == nameLength) {
               goodsName = (new StringBuilder()).append("<L>").append(goodsName).append("</L>");
               goodsName.append(otherSb);
            } else {
               List list;
               if (NoticePrintTransformUtil.isEn(goodsName.toString())) {
                  list = NoticePrintTransformUtil.getStrList(goodsName.toString(), nameLength);
               } else {
                  list = NoticePrintTransformUtil.getStrList(goodsName.toString(), nameLength / 2);
               }

               String s0 = NoticePrintTransformUtil.titleAddSpace((String)list.get(0));
               if (boldFlag) {
                  s0 = "<L>" + s0 + "</L>";
               }

               goodsName = new StringBuilder(s0 + String.valueOf(otherSb) + "<BR>");
               StringBuilder s = new StringBuilder();

               for(int k = 1; k < list.size(); ++k) {
                  s.append((String)list.get(k));
               }

               try {
                  s = new StringBuilder(NoticePrintTransformUtil.getStringByEnter(nameLength, s.toString(), boldFlag));
               } catch (Exception var17) {
               }

               goodsName.append(s);
            }
         }

         sb.append("------------------------------<BR>");
      }
   }
}
