package com.bonus.canteen.core.common.thirdutils.jiabo;

import cn.hutool.core.util.ObjectUtil;
import com.bonus.canteen.core.notice.print.v2.constant.PrinterDeviceTypeEnum;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.houqin.constant.LeConstants;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Map;
import java.util.Vector;

public class JbPrinterKeepLive {
   private static final Logger log = LoggerFactory.getLogger(JbPrinterKeepLive.class);
   private static final Map<String, Socket> PRINTER_SOCKET_MAP = Maps.newHashMap();
   private static final Map<String, Socket> PRINTER_STATUS_SOCKET_MAP = Maps.newHashMap();
   private static volatile JbPrinterKeepLive jbPrinter = null;
   private static final int PRINT_PORT = 9100;
   private static final int STATUS_PORT = 4000;
   private static final int PRINT_TIMEOUT = 10000;

   private JbPrinterKeepLive() {
   }

   public static JbPrinterKeepLive getPrinter() {
      if (jbPrinter == null) {
         Class var0 = JbPrinterKeepLive.class;
         synchronized(JbPrinterKeepLive.class) {
            if (jbPrinter == null) {
               jbPrinter = new JbPrinterKeepLive();
            }
         }
      }

      return jbPrinter;
   }

   public void executePrint(Vector<Byte> data, String ip, int printerType) {
      Socket socket = this.getSocket(ip, 9100, PRINTER_SOCKET_MAP);

      try {
         OutputStream outputStream = socket.getOutputStream();
         InputStream inputStream = socket.getInputStream();
         byte[] bytes;
         byte[] blockResult;
         if (ObjectUtil.equal(printerType, PrinterDeviceTypeEnum.TICKET_PRINTER.getKey())) {
            bytes = new byte[]{29, 114, 1};
            blockResult = bytes;
            int var9 = bytes.length;

            for(int var10 = 0; var10 < var9; ++var10) {
               byte command = blockResult[var10];
               data.add(command);
            }
         }

         bytes = new byte[data.size()];

         for(int i = 0; i < data.size(); ++i) {
            bytes[i] = (Byte)data.get(i);
         }

         outputStream.write(bytes);
         outputStream.flush();
         if (ObjectUtil.equal(printerType, PrinterDeviceTypeEnum.TICKET_PRINTER.getKey())) {
            blockResult = new byte[4096];
            inputStream.read(blockResult);
         }

      } catch (IOException var13) {
         try {
            socket.close();
         } catch (Exception var12) {
         }

         log.error("【佳博打印机】打印失败：{}", var13.getMessage(), var13);
         throw new ServiceException("【佳博打印机】连接失败");
      }
   }

   private Socket getSocket(String ip, Integer port, Map<String, Socket> socketMap) {
      Socket socket = (Socket)socketMap.get(ip);
      Class var5;
      if (socket == null) {
         var5 = JbPrinterKeepLive.class;
         synchronized(JbPrinterKeepLive.class) {
            if (socket == null) {
               socket = this.createAndConnectSocket(ip, port, socketMap);
            }
         }
      } else if (!socket.isConnected() || socket.isClosed()) {
         var5 = JbPrinterKeepLive.class;
         synchronized(JbPrinterKeepLive.class) {
            if (!socket.isConnected() || socket.isClosed()) {
               try {
                  socket.close();
               } catch (IOException var8) {
               }

               socket = this.createAndConnectSocket(ip, port, socketMap);
            }
         }
      }

      return socket;
   }

   private Socket createAndConnectSocket(String ip, Integer port, Map<String, Socket> socketMap) {
      Socket socket = new Socket();

      try {
         socket.connect(new InetSocketAddress(ip, port), 10000);
         socket.setSoTimeout(10000);
         socketMap.put(ip, socket);
         return socket;
      } catch (IOException var6) {
         log.error("【佳博打印机】连接失败：{}", var6.getMessage(), var6);
         throw new ServiceException("【佳博打印机】连接失败");
      }
   }

   public String queryOnlineStatus(String ip) {
      String status = String.valueOf(LeConstants.COMMON_NO);
      Socket socket = this.getSocket(ip, 4000, PRINTER_STATUS_SOCKET_MAP);

      try {
         InputStream inputStream = socket.getInputStream();
         OutputStream outputStream = socket.getOutputStream();
         byte[] bytes = new byte[]{27, 118};
         outputStream.write(bytes);
         outputStream.flush();
         byte[] result = new byte[4];
         int readLength = inputStream.read(result);
         if (ObjectUtil.notEqual(4, readLength)) {
            throw new ServiceException("【佳博打印机】打印机状态异常");
         } else {
            int[][] resultNestedArray = new int[4][8];

            for(int i = 0; i < result.length; ++i) {
               int val = result[i];

               for(int j = 7; j >= 0; --j) {
                  resultNestedArray[i][j] = (val & 128) == 0 ? 0 : 1;
                  val <<= 1;
               }
            }

            if (ObjectUtil.notEqual(0, resultNestedArray[0][3])) {
               throw new ServiceException("【佳博打印机】打印机状态异常");
            } else {
               return resultNestedArray[0][3] == 0 ? String.valueOf(LeConstants.COMMON_YES) : String.valueOf(LeConstants.COMMON_NO);
            }
         }
      } catch (IOException var14) {
         try {
            socket.close();
         } catch (Exception var13) {
         }

         log.error("【佳博打印机】查询状态失败：{}", var14.getMessage(), var14);
         return status;
      }
   }
}
