package com.qmai.zqtoolkit.base

import android.util.Log
import com.blankj.utilcode.util.ConvertUtils
import com.qmai.zqtoolkit.base.led.LEDBaseAction
import com.qmai.zqtoolkit.config.*
import com.zqdisplaysdk.ZQDisplaySDK
import com.zqprintersdk.PrinterConst

/**
 *  author : tongzi
 *  date : 2022/2/10 14:18
 *  description :中崎LED
 */


object ZqLEDKit : LEDBaseAction {
    private const val TAG: String = "ZqLEDKit"

    private val zqeb by lazy {
        ZQDisplaySDK()
    }

    private var isLEDSuccess = false
    override fun connect(port: String?, buad: String?) {
        isLEDSuccess = false
        val msg: String = when (zqeb.DSP_Connect(
            String.format("%s:%s", port ?: "ttyS3", buad ?: "9600")
        )) {
            PrinterConst.ErrorCode.SUCCESS -> {
                isLEDSuccess = true
                "打开成功"
            }
            PrinterConst.ErrorCode.INVALIDPARAM -> {
                "打印机参数无效"
            }
            PrinterConst.ErrorCode.PORTERROR -> {
                "端口错误"
            }
            PrinterConst.ErrorCode.NOPERMISSION -> {
                "没有该端口的访问权限"
            }
            else -> "未知错误"
        }
        //不管连接是否成功，先重置
        sendLEDWeight("0")
        sendLEDTotalPrice("0")
        Log.v(TAG, msg)
    }

    override fun sendData(data: ByteArray) {
        if (isLEDSuccess()) {
            var res = zqeb.DSP_SendData(data, data.size)
            Log.v(TAG, "-----resCode: $res-----")
        }
    }

    override fun sendLEDWeight(weight: String) {
        sendData(
            ConvertUtils.hexString2Bytes(ZQ_WEIGHT_START)
                .plus(ConvertUtils.string2Bytes(weight))
                .plus(ConvertUtils.hexString2Bytes(ZQ_END))
        )
    }

    override fun sendLEDUnitPrice(price: String) {
        sendData(
            ConvertUtils.hexString2Bytes(ZQ_UNIT_PRICE_START)
                .plus(ConvertUtils.string2Bytes(price))
                .plus(ConvertUtils.hexString2Bytes(ZQ_END))
        )
    }

    override fun sendLEDTotalPrice(price: String) {
        sendData(
            ConvertUtils.hexString2Bytes(ZQ_TOTAL_PRICE_START)
                .plus(ConvertUtils.string2Bytes(price))
                .plus(ConvertUtils.hexString2Bytes(ZQ_END))
        )
    }

    override fun sendLEDLight(zero: Boolean, stable: Boolean, tare: Boolean) {
        var light = ""
        light = if (zero)
            "1"
        else "0"
        light += if (stable)
            "1"
        else "0"
        light += if (tare)
            "1"
        else "0"
        light += "00000"
        sendData(
            ConvertUtils.hexString2Bytes(ZQ_LIGHT_START)
                .plus(ConvertUtils.bits2Bytes(light))
                .plus(ConvertUtils.hexString2Bytes(ZQ_LIGHT_MID))
                .plus(ConvertUtils.hexString2Bytes(ZQ_END))
        )
    }

    override fun clearLED() {
        sendData(ConvertUtils.hexString2Bytes(ZQ_CLEAR))
    }

    override fun isLEDSuccess(): Boolean {
        return isLEDSuccess
    }
}

