package com.dlc.chemical.print

import com.dlc.chemical.serial.HexDump
import com.print.usbprint.command.Label
import com.print.usbprint.util.USBUtil
import com.rc.core.log.RcLog
import com.rc.httpcore.vo.response.ChemicalBean
import java.util.*

/**
 * info
 *
 * @author ReiChin_
 */
object UsbPrinterManager {

    const val DEVICE_VID = 1317
    const val DEVICE_PID = 42151

    private const val PAGE_WIDTH = 384 // 48*8
    private const val PAGE_HEIGHT = 320 // 40*8
    private const val ROTATE = 0
    private const val CONFIG_OFF = 0
    private const val CONFIG_ON = 1
    private const val QR_X = 10
    private const val QR_Y = 60
    private const val QR_UNIT = 5
    private const val TXT_X = 210
    private const val FONT_HEIGHT = 24
    private const val LINE_SPACING = 2
    private const val LINE_MARGIN = 10

    // 化学品名称
    private const val NAME_Y1 = QR_Y
    private const val NAME_Y2 = NAME_Y1 + FONT_HEIGHT + LINE_SPACING

    // 属性
    private const val TYPE_Y = NAME_Y2 + FONT_HEIGHT + LINE_MARGIN

    // CAS号
    private const val CAS_Y1 = TYPE_Y + FONT_HEIGHT + LINE_MARGIN
    private const val CAS_Y2 = CAS_Y1 + FONT_HEIGHT + LINE_SPACING

    // 编号
    private const val NUM_Y1 = CAS_Y2 + FONT_HEIGHT + LINE_MARGIN
    private const val NUM_Y2 = NUM_Y1 + FONT_HEIGHT + LINE_SPACING

    fun printLabel(bean: ChemicalBean) {
        val label = Label()
        label.reset()
        label.customPageStart(PAGE_WIDTH, PAGE_HEIGHT, ROTATE)
        // 绘制二维码
        label.printQRcode(bean.tagCode, 5, 2, QR_X, QR_Y, QR_UNIT, ROTATE)

        // 绘制文本
        // 化学品名称
        val name = bean.chemicalName
        if (name.length <= 7) {
            printText(label, name, TXT_X, NAME_Y1, CONFIG_ON)
        } else {
            printText(label, name.substring(0, 7), TXT_X, NAME_Y1, CONFIG_ON)
            printText(label, name.substring(7), TXT_X, NAME_Y2, CONFIG_ON)
        }
        // 属性
        printText(label, bean.classifyAttribute, TXT_X, TYPE_Y)
        // CAS号
        printText(label, "CAS号：", TXT_X, CAS_Y1)
        printText(label, bean.casNum, TXT_X, CAS_Y2)
        // 编号
        printText(label, "编号：", TXT_X, NUM_Y1)
        printText(label, bean.tagCode, TXT_X, NUM_Y2)

        label.pageEnd()
        label.defaultPrintPage()
        printCommandLog(label.command)
        USBUtil.getInstance().CommandLabel(label)
        label.clear()
    }

    private fun printCommandLog(command: Vector<Byte>) {
        RcLog.info("command:${command}")
        val vector = command.mapTo(Vector()) { HexDump.toHexString(it) }
        RcLog.info("hex command:${vector}")
    }

    private fun printText(label: Label, text: String?, x: Int, y: Int, bold: Int = CONFIG_OFF) {
        label.customPrintText(
            text ?: "",
            x,
            y,
            FONT_HEIGHT,
            bold,
            CONFIG_OFF,
            CONFIG_OFF,
            CONFIG_OFF,
            ROTATE,
            1,
            1
        )
    }

    /**
     * 打印气瓶标签
     */
    fun printAirBottleLabel(labelCode: String) {
        val label = Label()
        label.reset()
        label.customPageStart(PAGE_WIDTH, PAGE_HEIGHT, ROTATE)
        // 绘制二维码
        label.printQRcode(labelCode, 5, 2, 105, QR_Y, QR_UNIT, ROTATE)

        printText(label, labelCode, 50, 260)

        label.pageEnd()
        label.defaultPrintPage()
        printCommandLog(label.command)
        USBUtil.getInstance().CommandLabel(label)
        label.clear()
    }

}