package com.zj365.health.act.binddevice

import android.view.LayoutInflater
import androidx.recyclerview.widget.LinearLayoutManager
import com.xty.base.act.BaseListAct
import com.xty.common.event.BindDevEvent
import com.xty.common.event.WorkManagerEvent
import com.zj365.health.R
import com.zj365.health.adapter.binddevice.BindDeviceAdapter
import com.zj365.health.databinding.ActBindDeviceListBinding
import com.zj365.health.vm.BindDeviceListVm
import com.zj365.health.weight.UnBindDialog
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

abstract class BaseDeviceBindListAct<V: BindDeviceListVm> : BaseListAct<V>() {
    val binding  by lazy{ ActBindDeviceListBinding.inflate(layoutInflater)}
    var dialog: UnBindDialog?=null
    var mAdapter: BindDeviceAdapter?=null
    enum class DeviceType(var type:Int){
        XUEYA(1),
        XUETANG(2),
        BODYFAT(3)
    }
    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener { finish() }
        binding.tvBindDeviceImdiatly.setOnClickListener {
            goToBind()
        }
        binding.tvHistory.setOnClickListener {
            goToHistoryList()
        }
        mAdapter?.setUnbindListener { sn,mac->
            dialog =  UnBindDialog(this){
                mViewModel.unBind(sn,mac)
            }
            dialog?.show()
        }
    }

    abstract fun goToBind()

    abstract fun goToHistoryList()

    override fun liveObserver() {
        mViewModel.list.observe(this) {
            mAdapter?.let { adapter ->
                setDate(adapter, it.data)
            }
        }
        mViewModel.unbindLive.observe(this){
            loadData()
            EventBus.getDefault().post(WorkManagerEvent())  //首页更新
        }
    }

    override fun setLayout()= binding.root
    override fun initAdapter() {
        setRecycleRefresh(binding.mRecycle,binding.mRefresh)
        binding.mRecycle.layoutManager= LinearLayoutManager(this)
        binding.mRecycle.adapter=mAdapter
        mAdapter?.setEmptyView(this.getNoneView())
    }

    override fun loadData() {
    }
    override fun getNoneView() =
        LayoutInflater.from(this).inflate(R.layout.view_device_none_data, binding.mRecycle, false)
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun familyChange(event: BindDevEvent){
       loadData()
    }
}