package com.grkj.iscs.view.presenter

import com.grkj.iscs.BusinessManager
import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_KEY
import com.grkj.iscs.util.Executor
import com.grkj.iscs.view.base.BasePresenter
import com.grkj.iscs.view.iview.IHomeView

class HomePresenter : BasePresenter<IHomeView>() {

    fun registerStatusListener() {
        BusinessManager.registerStatusListener(this) { dockBean ->
            when (dockBean.type) {
                DOCK_TYPE_KEY -> {
                    dockBean.getKeyList().forEach { keyBean ->
                        if (keyBean.isExist) {
                            Executor.repeatOnMain({
                                keyBean.mac?.let {
                                    BusinessManager.registerConnectListener(it) { isDone, bleBean ->
                                        if (isDone && bleBean != null) {
                                            Executor.delayOnMain(300) {
                                                BusinessManager.getCurrentStatus(4, BusinessManager.getBleDeviceByMac(keyBean.mac)?.bleDevice!!)
                                            }
                                        }
                                    }
                                }
                                return@repeatOnMain keyBean.mac == null
                            }, 1000, true)
                        }
                    }
                }
            }
        }
    }

    fun unregisterListener() {
        BusinessManager.unregisterListener(this)
    }
}