package aicare.net.cn.sdk.ailinksdkdemoandroid.modules.ble_0020_hu_infrared_temp

import aicare.net.cn.sdk.ailinksdkdemoandroid.R
import aicare.net.cn.sdk.ailinksdkdemoandroid.base.BleAppNewBaseActivity
import aicare.net.cn.sdk.ailinksdkdemoandroid.modules.ble_0020_hu_infrared_temp.viewmodel.Ble0020HpTempViewModel
import aicare.net.cn.sdk.ailinksdkdemoandroid.utils.TimeUtils
import android.os.Message
import android.view.View
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.EditText
import android.widget.ListView
import androidx.lifecycle.ViewModelProvider
import cn.net.aicare.modulelibrary.module.ble_0020_hu_infrared_temp.HpInfraredTempBleConfig

/**
 * 华普红外测温枪
 * @author xing
 * @date 2025/03/27
 * @constructor 创建[Ble0020HpInfraredTempActivity]
 */
class Ble0020HpInfraredTempActivity : BleAppNewBaseActivity(), View.OnClickListener {

    private lateinit var btnPause: Button
    private lateinit var btnContinue: Button
    private lateinit var btnClear: Button
    private lateinit var btnWakeup: Button
    private lateinit var btnTest: Button
    private lateinit var btnStopTest: Button
    private lateinit var btnReset: Button
    private lateinit var btnEmissivity: Button
    private lateinit var btnAutoShutdown: Button
    private lateinit var btnHighOrLowTemp: Button
    private lateinit var btnGetDeviceStatus: Button
    private lateinit var btnGetDeviceParameter: Button
    private lateinit var etEmissivity: EditText
    private lateinit var etAutoShutdown: EditText
    private lateinit var etHighTemp: EditText
    private lateinit var etLowTemp: EditText
    private var mViewModel: Ble0020HpTempViewModel? = null
    private var mAddress: String = ""
    private val mList: ArrayList<String> = arrayListOf()
    private var listAdapter: ArrayAdapter<String>? = null
    private var mRefresh = true

    override fun uiHandlerMessage(msg: Message?) {

    }

    override fun getLayoutId(): Int {
        return R.layout.activity_ble_0020_hp_infrared_temp
    }

    override fun initView() {
        btnPause=findViewById(R.id.btnPause)
        btnContinue=findViewById(R.id.btnContinue)
        btnClear=findViewById(R.id.btnClear)
        btnWakeup=findViewById(R.id.btnWakeup)
        btnTest=findViewById(R.id.btnTest)
        btnStopTest=findViewById(R.id.btnStopTest)
        btnReset=findViewById(R.id.btnReset)
        btnEmissivity=findViewById(R.id.btnEmissivity)
        btnAutoShutdown=findViewById(R.id.btnAutoShutdown)
        btnHighOrLowTemp=findViewById(R.id.btnHighOrLowTemp)
        btnGetDeviceStatus=findViewById(R.id.btnGetDeviceStatus)
        btnGetDeviceParameter=findViewById(R.id.btnGetDeviceParameter)
        etEmissivity=findViewById(R.id.etEmissivity)
        etAutoShutdown=findViewById(R.id.etAutoShutdown)
        etHighTemp=findViewById(R.id.etHighTemp)
        etLowTemp=findViewById(R.id.etLowTemp)
    }



    override fun initListener() {
        btnPause.setOnClickListener(this)
        btnContinue.setOnClickListener(this)
        btnClear.setOnClickListener(this)
        btnWakeup.setOnClickListener(this)
        btnTest.setOnClickListener(this)
        btnStopTest.setOnClickListener(this)
        btnReset.setOnClickListener(this)
        btnEmissivity.setOnClickListener(this)
        btnAutoShutdown.setOnClickListener(this)
        btnHighOrLowTemp.setOnClickListener(this)
        btnGetDeviceStatus.setOnClickListener(this)
        btnGetDeviceParameter.setOnClickListener(this)

        mViewModel?.let {
            it.showLogLiveData.observe(this) {
                addLog(it)
            }
        }

    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btnPause -> {
                mRefresh = false
            }

            R.id.btnContinue -> {
                mRefresh = true

            }

            R.id.btnClear -> {
                clear()
            }

            R.id.btnWakeup -> {
                mViewModel?.sendButtonCmd(HpInfraredTempBleConfig.CMD_WAKEUP)
                addLog("发送唤醒")
            }

            R.id.btnTest -> {
                mViewModel?.sendButtonCmd(HpInfraredTempBleConfig.CMD_TEST)
                addLog("发送测量")
            }

            R.id.btnStopTest -> {
                mViewModel?.sendButtonCmd(HpInfraredTempBleConfig.CMD_STOP_TEST)
                addLog("发送停止测量")
            }

            R.id.btnReset -> {
                mViewModel?.sendButtonCmd(HpInfraredTempBleConfig.CMD_RESET)
                addLog("发送重置")
            }

            R.id.btnEmissivity -> {
                val emissivity = etEmissivity.text.toString()
                if (emissivity.isNotBlank()) {
                    val data = (emissivity.toFloat() * 100).toInt()
                    mViewModel?.sendEmissivity(data)
                    addLog("发送发射率:$data")
                }

            }

            R.id.btnAutoShutdown -> {
                val autoShutdown = etAutoShutdown.text.toString()
                if (autoShutdown.isNotBlank()) {
                    mViewModel?.sendAutoShutdown(autoShutdown.toInt())
                    addLog("发送自动关机:$autoShutdown")
                }
            }

            R.id.btnHighOrLowTemp -> {
                val tempHStr = etHighTemp.text.toString()
                val tempLStr = etLowTemp.text.toString()
                if (tempHStr.isNotBlank() && tempLStr.isNotBlank()) {
                    val tempH = tempHStr.toInt()
                    val tempL = tempLStr.toInt()
                    val signH = if (tempH > 0) {
                        HpInfraredTempBleConfig.PLUS
                    } else {
                        HpInfraredTempBleConfig.MINUS
                    }
                    val signL = if (tempL > 0) {
                        HpInfraredTempBleConfig.PLUS
                    } else {
                        HpInfraredTempBleConfig.MINUS
                    }
                    mViewModel?.sendWarmInfo(HpInfraredTempBleConfig.TEMP_C, signH, signL, tempH, tempL)
                    addLog("发送高低温报警:高温=${tempHStr}℃,低温=${tempLStr}℉")
                }


            }

            R.id.btnGetDeviceStatus -> {
                mViewModel?.sendGetDeviceStatus()
                addLog("发送获取设备状态")
            }

            R.id.btnGetDeviceParameter -> {
                mViewModel?.sendGetDeviceParameter()
                addLog("发送获取设备参数")
            }

            else -> {}
        }

    }

    override fun initData() {
        mContext = this
        val mac = intent.getStringExtra("mac")
        if (mac.isNullOrBlank()) {
            finish()
            return
        }
        mAddress = mac
        mViewModel = ViewModelProvider(this)[Ble0020HpTempViewModel::class.java]
        if (isServiceBind) {
            onServiceSuccess()
        }
        val listView = findViewById<ListView>(R.id.listview)
        listAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, mList)
        listView.adapter = listAdapter
    }



    private fun addLog(msg: String) {
        if (!mRefresh) {
            return
        }
        mList.add(TimeUtils.getTimeSSS() + msg)
        listAdapter!!.notifyDataSetChanged()
    }

    private fun clear() {
        mList.clear()
        listAdapter!!.notifyDataSetChanged()
    }

    private var isServiceBind = false
    override fun onServiceSuccess() {
        isServiceBind = true
        if (mAddress.isNotBlank()) {
            val bleDevice = mBleManager?.getBleDevice(mAddress)
            bleDevice?.let {
                mViewModel?.initDevice(it)
            }
        }
    }

    override fun onServiceErr() {

    }

    override fun unbindServices() {

    }








}