package com.grkj.iscs.view.activity.test

import com.grkj.iscs.BusinessManager
import com.grkj.iscs.view.base.BaseMvpActivity
import com.grkj.iscs.databinding.ActivityModbusBinding
import com.grkj.iscs.extentions.removeLeadingZeros
import com.grkj.iscs.extentions.toHexStrings
import com.grkj.iscs.view.iview.IModbusView
import com.grkj.iscs.modbus.ModBusController
import com.grkj.iscs.view.presenter.ModBusPresenter
import com.grkj.iscs.util.ToastUtils
import com.grkj.iscs.util.log.LogUtil

class ModbusActivity : BaseMvpActivity<IModbusView, ModBusPresenter, ActivityModbusBinding>() {

    override val viewBinding: ActivityModbusBinding
        get() = ActivityModbusBinding.inflate(layoutInflater)

    override fun initView() {
        BusinessManager.connectDock()

        BusinessManager.registerStatusListener(this) {

        }

        mBinding?.init?.setOnClickListener {
            ModBusController.initDevicesStatus()
        }

        mBinding?.keyBuckleStatus?.setOnClickListener {
            BusinessManager.readKeyBuckleStatus()
        }

        mBinding?.lockBuckleStatus?.setOnClickListener {
            BusinessManager.readLockBuckleStatus()
        }

        mBinding?.deviceType?.setOnClickListener {
            ModBusController.updateDeviceType()
        }

        mBinding?.openLockBuckle?.setOnClickListener {
            if (mBinding?.slaveIdx?.text.toString().isEmpty()) {
                ToastUtils.tip("请输入设备编号")
                return@setOnClickListener
            }
            if (mBinding?.lockIdx?.text.toString().isEmpty()) {
                ToastUtils.tip("请输入卡扣编号")
                return@setOnClickListener
            }
            ModBusController.controlLockBuckle(true, mBinding?.slaveIdx?.text.toString().toByte(), mBinding?.lockIdx?.text.toString().toInt()) { res ->
                LogUtil.i("开锁卡扣 : ${res.toHexStrings()}")
            }
        }

        mBinding?.closeLockBuckle?.setOnClickListener {
            if (mBinding?.slaveIdx?.text.toString().isEmpty()) {
                ToastUtils.tip("请输入设备编号")
                return@setOnClickListener
            }
            if (mBinding?.lockIdx?.text.toString().isEmpty()) {
                ToastUtils.tip("请输入卡扣编号")
                return@setOnClickListener
            }
            ModBusController.controlLockBuckle(false, mBinding?.slaveIdx?.text.toString().toByte(), mBinding?.lockIdx?.text.toString().toInt()) { res ->
                LogUtil.i("关锁卡扣 : ${res.toHexStrings()}")
            }
        }

        mBinding?.lockRfid?.setOnClickListener {
            if (mBinding?.slaveIdx?.text.toString().isEmpty()) {
                ToastUtils.tip("请输入设备编号")
                return@setOnClickListener
            }
            if (mBinding?.lockIdx?.text.toString().isEmpty()) {
                ToastUtils.tip("请输入卡扣编号")
                return@setOnClickListener
            }
            ModBusController.readLockRfid(mBinding?.slaveIdx?.text.toString().toByte(), mBinding?.lockIdx?.text.toString().toInt()) { res ->
                if (res.size < 11) {
                    LogUtil.e("Lock rfid error")
                    return@readLockRfid
                }
                val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
                LogUtil.i("锁具RFID : $rfid")
                ModBusController.updateLockRfid(mBinding?.slaveIdx?.text.toString().toByte(), mBinding?.lockIdx?.text.toString().toInt(), rfid)
            }
        }

        mBinding?.keyRfid?.setOnClickListener {
            // TODO 暂时写死，调试用
            ModBusController.readKeyRfid(1, 1) { left, res ->
                if (res.size < 11) {
                    LogUtil.e("Key rfid error")
                    return@readKeyRfid
                }
                val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
                LogUtil.i("钥匙RFID : $rfid")
                ModBusController.updateKeyRfid(1, left, rfid)
            }
        }

        mBinding?.openLeftKeyBuckle?.setOnClickListener {
            LogUtil.i("____________________________________")
            if (mBinding?.slaveIdx?.text.toString().isEmpty()) {
                ToastUtils.tip("请输入设备编号")
                return@setOnClickListener
            }
            ModBusController.controlKeyBuckle(isOpen = true, true, mBinding?.slaveIdx?.text.toString().toByte()) {
                LogUtil.i("开左钥匙卡扣 : ${it.toHexStrings()}")
                LogUtil.i("____________________________________")
            }
        }

        mBinding?.closeLeftKeyBuckle?.setOnClickListener {
            LogUtil.i("____________________________________")
            if (mBinding?.slaveIdx?.text.toString().isEmpty()) {
                ToastUtils.tip("请输入设备编号")
                return@setOnClickListener
            }
            ModBusController.controlKeyBuckle(isOpen = false, true, mBinding?.slaveIdx?.text.toString().toByte()) {
                LogUtil.i("关左钥匙卡扣 : ${it.toHexStrings()}")
                LogUtil.i("____________________________________")
            }
        }

        mBinding?.openRightKeyBuckle?.setOnClickListener {
            LogUtil.i("____________________________________")
            if (mBinding?.slaveIdx?.text.toString().isEmpty()) {
                ToastUtils.tip("请输入设备编号")
                return@setOnClickListener
            }
            ModBusController.controlKeyBuckle(isOpen = true, false, mBinding?.slaveIdx?.text.toString().toByte()) {
                LogUtil.i("开右钥匙卡扣 : ${it.toHexStrings()}")
                LogUtil.i("____________________________________")
            }
        }

        mBinding?.closeRightKeyBuckle?.setOnClickListener {
            LogUtil.i("____________________________________")
            if (mBinding?.slaveIdx?.text.toString().isEmpty()) {
                ToastUtils.tip("请输入设备编号")
                return@setOnClickListener
            }
            ModBusController.controlKeyBuckle(isOpen = false, false, mBinding?.slaveIdx?.text.toString().toByte()) {
                LogUtil.i("关右钥匙卡扣 : ${it.toHexStrings()}")
                LogUtil.i("____________________________________")
            }
        }

        mBinding?.closeAllLockBuckles?.setOnClickListener {
            ModBusController.controlAllLockBuckles(false)
        }

        mBinding?.openAllLockBuckles?.setOnClickListener {
            ModBusController.controlAllLockBuckles(true)
        }

        mBinding?.deviceList?.setOnClickListener {
            ModBusController.printDockInfo()
        }

        mBinding?.exit?.setOnClickListener { finish() }
    }

    override fun initPresenter(): ModBusPresenter {
        return ModBusPresenter()
    }

    override fun onDestroy() {
        super.onDestroy()
        BusinessManager.unregisterListener(this)
        BusinessManager.disconnectDock()
    }
}