package com.bonus.canteen.core.notice.print.v2.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.bonus.canteen.core.device.manage.vo.DeviceInfoVO;
import com.bonus.canteen.core.notice.print.v2.constant.PrintTemplateExtSettingTypeEnum;
import com.bonus.canteen.core.notice.print.v2.vo.NoticePrintTemplateSettingVO;
import com.bonus.common.houqin.constant.LeCodeUseSceneEnum;
import com.bonus.common.houqin.i18n.I18n;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

public class NoticePrintTransformUtil {
   private static final Logger log = LoggerFactory.getLogger(NoticePrintTransformUtil.class);

   private NoticePrintTransformUtil() {
   }

   public static String transformLocalDateTime(LocalDateTime localDateTime) {
      return LocalDateTimeUtil.format(localDateTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
   }

   public static String transformLocalDate(LocalDate localDate) {
      return LocalDateTimeUtil.format(localDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
   }

   public static String transformAmountYuan(BigDecimal amount) {
      if (ObjectUtil.isNull(amount)) {
         return null;
      } else {
         BigDecimal oneHundred = BigDecimal.valueOf(100L);
         return NumberUtil.div(amount, oneHundred, 2).toString();
      }
   }

   public static String transformAmountYuan(Integer amount) {
      if (ObjectUtil.isNull(amount)) {
         return null;
      } else {
         BigDecimal oneHundred = BigDecimal.valueOf(100L);
         return NumberUtil.div(amount, oneHundred, 2).toString();
      }
   }

   public static String getPrintParamValue(NoticePrintTemplateSettingVO setting, Map<String, String> paramMap) {
      return ObjectUtil.isEmpty(setting.getColumnValue()) ? null : (String)paramMap.get(setting.getColumnValue());
   }

   public static String getOrderQRCode(String sourceType, String paramValue) {
      String stime = String.valueOf(System.currentTimeMillis() / 1000L);
      return "xnzn{\"s\":" + sourceType + ",\"y\":" + LeCodeUseSceneEnum.PAIRING.key() + ",\"p\":\"" + paramValue + "\",\"t\":" + stime + "}";
   }

   public static String getSubstring(String str, int f, int t) {
      if (f > str.length()) {
         return null;
      } else {
         return t > str.length() ? str.substring(f, str.length()) : str.substring(f, t);
      }
   }

   public static String flushLeft(char c, long l, String string) {
      String regEx = "[\\u4e00-\\u9fa5]";
      String term = string.replaceAll(regEx, "aa");
      StringBuilder temp = new StringBuilder();
      if ((long)term.length() <= l) {
         for(int i = 0; (long)i < l - (long)term.length(); ++i) {
            temp.append(c);
         }
      }

      String str = string + String.valueOf(temp);
      return str;
   }

   public static String flushRight(char c, long l, String string) {
      String regEx = "[\\u4e00-\\u9fa5]";
      String term = string.replaceAll(regEx, "aa");
      StringBuilder temp = new StringBuilder();
      if ((long)term.length() <= l) {
         for(int i = 0; (long)i < l - (long)term.length(); ++i) {
            temp.append(c);
         }
      }

      String var10000 = String.valueOf(temp);
      String str = var10000 + string;
      return str;
   }

   public static boolean isEn(String str) {
      boolean b = false;

      try {
         b = str.getBytes("GBK").length == str.length();
      } catch (UnsupportedEncodingException var3) {
         log.info(var3.getMessage(), var3);
      }

      return b;
   }

   public static List<String> getStrList(String inputString, int length) {
      int size = inputString.length() / length;
      if (inputString.length() % length != 0) {
         ++size;
      }

      return getStrList(inputString, length, size);
   }

   private static List<String> getStrList(String inputString, int length, int size) {
      List<String> list = CollUtil.newArrayList(new String[0]);

      for(int index = 0; index < size; ++index) {
         String childStr = getSubstring(inputString, index * length, (index + 1) * length);
         list.add(childStr);
      }

      return list;
   }

   public static String titleAddSpace(String str) {
      int k = 0;
      int b = 10;

      try {
         k = str.getBytes("GBK").length;
      } catch (UnsupportedEncodingException var4) {
         log.info(var4.getMessage(), var4);
      }

      for(int i = 0; i < b - k; ++i) {
         str = str + " ";
      }

      return str;
   }

   public static String getStringByEnter(int length, String string, boolean boldFlag) throws Exception {
      for(int i = 1; i <= string.length(); ++i) {
         if (string.substring(0, i).getBytes("GBK").length > length) {
            String var10000;
            if (boldFlag) {
               var10000 = string.substring(0, i - 1);
               return "<L>" + var10000 + "</L><BR>" + getStringByEnter(length, string.substring(i - 1), boldFlag);
            }

            var10000 = string.substring(0, i - 1);
            return var10000 + "<BR>" + getStringByEnter(length, string.substring(i - 1), boldFlag);
         }
      }

      if (boldFlag) {
         return "<L>" + string + "</L>";
      } else {
         return string;
      }
   }

   public static String getSettingParamValue(NoticePrintTemplateSettingVO setting, Map<String, String> paramMap) {
      if (ObjectUtil.equal(PrintTemplateExtSettingTypeEnum.FIXED.getKey(), setting.getSettingType())) {
         return setting.getSettingValue();
      } else {
         String paramValue = getPrintParamValue(setting, paramMap);
         if (ObjectUtil.isEmpty(paramValue)) {
            return null;
         } else {
            return ObjectUtil.isEmpty(setting.getColumnName()) ? paramValue : I18n.getMessage(setting.getColumnName(), new Object[]{paramValue});
         }
      }
   }

//   public static LabelCommand.FONTMUL getFontMul(Integer value) {
//      LabelCommand.FONTMUL[] var1 = FONTMUL.values();
//      int var2 = var1.length;
//
//      for(int var3 = 0; var3 < var2; ++var3) {
//         LabelCommand.FONTMUL fontMul = var1[var3];
//         if (ObjectUtil.equal(fontMul.getValue(), value)) {
//            return fontMul;
//         }
//      }
//
//      return FONTMUL.MUL_1;
//   }

   public static String getNutritionValue(String i18n, String nutrition, String unit) {
      String var10000 = I18n.getMessage(i18n, new Object[0]);
      return var10000 + flushRight(' ', 7L, nutrition + unit);
   }

   public static Integer getPrintTimes(DeviceInfoVO deviceInfoVO, boolean autoFlag) {
      Object timeObj;
      if (autoFlag) {
         timeObj = deviceInfoVO.getDeviceMetadata().get("autoPrintNumber");
      } else {
         timeObj = deviceInfoVO.getDeviceMetadata().get("handPrintNumber");
      }

      return ObjectUtil.isNull(timeObj) ? 1 : Integer.valueOf(timeObj.toString());
   }
}
