package com.qmai.zqtoolkit.base

import android.content.Context
import android.util.Log
import com.blankj.utilcode.util.GsonUtils
import com.example.scaler.AclasScaler
import com.qmai.zqtoolkit.ConnectState
import com.qmai.zqtoolkit.LivingWeight
import com.qmai.zqtoolkit.ScaleInfo
import com.qmai.zqtoolkit.base.scale.ScaleBaseKit
import com.qmai.zqtoolkit.config.QmScaleBrand
import com.qmai.zqtoolkit.utils.ScaleChecker

/**
 * 巨隆秤
 * 用顶尖的sdk 串口连接方式
 */
object GiLongScaleKit : ScaleBaseKit() {
    override val TAG: String
        get() = "GiLongScaleKit"
    override val STABLE_COUNT: Int
        get() = 4
    override val CONECT_CHECK_COUNT: Int
        get() = 0
    override val SCALE_TYPE: QmScaleBrand
        get() = QmScaleBrand.GILONG
    private var aclasScaler: AclasScaler? = null
    private var aclasScalerListener = object : AclasScaler.AclasScalerListener {
        override fun onDisConnected() {
            updateState(ConnectState(false, "打开失败"))
        }

        override fun onConnected() {
            updateState(ConnectState(true, "打开成功"))
        }

        override fun onRcvData(weght: AclasScaler.WeightInfoNew?) {
            Log.v(TAG, GsonUtils.toJson(weght))
            //  getTare()
            updateWeight(
                LivingWeight(
                    weght?.isStable ?: false, weght.toString().replace(weght?.unit ?: "", ""),
                    weght?.unit ?: "", weght?.tareWeight?.toString() ?: "", weght?.netWeight == null
                )
            )
        }

        override fun onError(p0: Int, p1: String?) {
            Log.v(TAG, "onError:$p0$p1")
        }

        override fun onUpdateProcess(p0: Int, p1: Int) {
            Log.v(TAG, "onUpdateProcess:$p0$p1")
        }

    }


    override fun connect(cxt: Context?, scale: ScaleInfo) {
        super.connect(cxt, scale)
        if (aclasScaler == null) {
            cxt?.let {
                aclasScaler = AclasScaler(AclasScaler.Type_SerialPort, it, aclasScalerListener)
            }
        }
        //val usbConnectIndex = getAclasScaleIndex()
        //val usbConnectIndex = 0
        val aclasConnect = aclasScaler?.AclasConnect("/dev/${scale.port}", (scale.buad?.toIntOrNull()?:9600),0)
        Log.v(TAG, "aclasConnect:$aclasConnect")
    }

    override fun disconnect() {
        super.disconnect()
        aclasScaler?.AclasDisconnect()
        aclasScaler = null
    }

    override fun readData() {

    }

    override fun stopReadData() {

    }

    override fun setZero() {
        if (isScaleSuccess()) {
            val aclasZero = aclasScaler?.AclasZero()
            Log.v(TAG, "setZero:$aclasZero")
        }
    }

    private fun getTare() {
        if (isScaleSuccess()) {
            val getTare = aclasScaler?.AclasReadTareValue()
            Log.v(TAG, "getTare:$getTare")
        }
    }

    override fun setNetWeight() {
        if (isScaleSuccess()) {
            val aclasTare = aclasScaler?.AclasTare()
            Log.v(TAG, "setNetWeight:$aclasTare")
        }
    }

    override fun setNetWeightByElec(tare: Double) {
        if (isScaleSuccess()) {
            val aclasSetPreTare = aclasScaler?.AclasSetPreTare((tare * 1000.0f).toInt())
            Log.v(TAG, "setNetWeightByElec:$aclasSetPreTare")
        }
    }

    //去除 除皮一个方法
    override fun clearTare() {
        if (isScaleSuccess()) {
            val clearTare = aclasScaler?.AclasTare()
            Log.v(TAG, "clearTare:$clearTare")
        }
    }

    override fun getParameter(): Array<String> {
        var parameter: Array<String> = arrayOf()
        if (isScaleSuccess()) {
            parameter[0] = aclasScaler?.AclasSDKVersion() ?: ""
            parameter[1] = aclasScaler?.AclasReadVersion() ?: ""
            parameter[2] = aclasScaler?.AclasReadId() ?: ""
        }
        return parameter
    }


    override fun isScaleSuccess(): Boolean {
        return super.isScaleSuccess() && aclasScaler?.AclasIsConnect() == true
    }

    private fun getAclasScaleIndex(): Int {
        var usbIndex = 0
        var aclasPname = ScaleChecker.getAclasPname()
        if (aclasPname.isNotEmpty()) {
            var comList = mutableListOf<String>()
            aclasScaler?.getCommList(comList)
            if (!comList.isNullOrEmpty()) {
                usbIndex = comList.indexOfFirst { name -> name.contains(aclasPname) }
                return usbIndex
            }
        }
        return usbIndex
    }
}