package com.qmai.zqtoolkit.base

import android.annotation.SuppressLint
import android.content.Context
import android.util.Log
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.sunmi.electronicscaleservice.ScaleCallback
import com.sunmi.scalelibrary.ScaleManager
import com.sunmi.scalelibrary.ScaleManager.ScaleServiceConnection
import java.text.DecimalFormat

/**
 * tongzi
 * 2022/4/28
 * 商米电子秤
 */
object SunmiScaleKit : ScaleBaseKit() {
    override val TAG: String
        get() = "SunmiScaleKit"
    override val STABLE_COUNT: Int
        get() = 3
    override val CONECT_CHECK_COUNT: Int
        get() = 0
    override val SCALE_TYPE: QmScaleBrand
        get() = QmScaleBrand.SUNMI

    @SuppressLint("StaticFieldLeak")
    private var mScaleManager: ScaleManager? = null
    private val decimalFormat = DecimalFormat("0.000")

    override fun connect(cxt: Context?, scale: ScaleInfo ) {
        super.connect(cxt, scale)
        cxt?.let {
            mScaleManager = ScaleManager.getInstance(it)
            mScaleManager?.connectService(object : ScaleServiceConnection {
                override fun onServiceConnected() {
                    updateState(ConnectState(true, "打开成功"))
                }

                override fun onServiceDisconnect() {
                    updateState(ConnectState(false, "打开失败"))
                }
            })
        }
    }
    override fun readData() {
        try {
            mScaleManager?.getData(object : ScaleCallback.Stub() {
                override fun getData(i: Int, i1: Int, i2: Int) {
                    // i = 净重量 单位 克 ，i1 = 皮重量 单位 克 ，i2 = 稳定状态  1 为稳定。具体其他状态请参考商米开发者文档
                    Log.v(
                        TAG, String.format(
                            "%d,%d,%d",
                            i2, i, i1
                        )
                    )
                    updateWeight(
                        LivingWeight(
                            i2 and 1 == 1,
                            decimalFormat.format((i * 1.0f / 1000).toDouble()),
                            "kg",
                            decimalFormat.format((i1 * 1.0f / 1000).toDouble()),
                            false, //sunmi 没有读数错误的场景 ，不默认会触发readErrLimit 断开连接
                        )
                    )
                }
            })
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun stopReadData() {
        try {
            mScaleManager?.cancelGetData()
        } catch (e: Exception) {
        }
    }


    override fun disconnect() {
        super.disconnect()
        mScaleManager?.onDestroy()
        mScaleManager = null
        Log.v(TAG, "disconnect")
    }


    //置零
    override fun setZero() {
        if (isScaleSuccess()) {
            try {
                mScaleManager?.zero()
            } catch (e: Exception) {
            }
            Log.v(TAG, "setZero")
        }
    }

    //去皮
    override fun setNetWeight() {
        if (isScaleSuccess()) {
            try {
                mScaleManager?.tare()
            } catch (e: Exception) {
            }
            Log.v(TAG, "setNetWeight")
        }
    }

    override fun setNetWeightByElec(tare: Double) {
        if (isScaleSuccess()) {
            try {
                mScaleManager?.digitalTare((tare * 1000.0f).toInt())
            } catch (e: Exception) {
            }
            Log.v(TAG, "setNetWeightByElec")
        }
    }

    //清皮
    override fun clearTare() {
        if (isScaleSuccess()) {
            try {
                mScaleManager?.tare()
            } catch (e: Exception) {
            }
            Log.v(TAG, "clearTare")
        }
    }

    //读取电子秤参数
    override fun getParameter(): Array<String> {
        var parameter: Array<String> = arrayOf()
        if (isScaleSuccess()) {
            parameter[0] = mScaleManager?.firmwareVersion.toString()
            Log.v(TAG, "getParameter")
        }
        return parameter
    }


}