package com.zj365.dc.activity

import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.xty.base.act.BaseListAct
import com.xty.common.UmengEventId
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.onEventObject
import com.xty.common.recycleUtils.RecycleItem
import com.xty.network.model.MessageBean
import com.zj365.dc.R
import com.zj365.dc.adapter.WarningMessageAdapter
import com.zj365.dc.vm.HealthVm
import com.zj365.mime.databinding.ActWarningMessageBinding

/**
 * 我的预警消息
 */
@Route(path = ARouterUrl.WARNING_MSG)
class WarningMsgAct : BaseListAct<HealthVm>() {
    val binding by lazy { ActWarningMessageBinding.inflate(layoutInflater) }
    val adapter by lazy { WarningMessageAdapter() }
    var clickBean: MessageBean.SysMsg? = null
    var clickPosition = 0

    override fun setLayout() = binding.root

    override fun initData() {
        super.initData()
        isDarkBar = true
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = getString(R.string.warning_message)

        setRecycleRefresh(binding.mRecycle, binding.mRefresh)
        binding.title.mIvBack.setOnClickListener {
            finish()
        }
        onEventObject(baseContext, UmengEventId.WarningMsg, "打开预警消息列表")
    }

    override fun initAdapter() {
        binding.mRecycle.layoutManager = LinearLayoutManager(this)
        binding.mRecycle.adapter = adapter
        binding.mRecycle.addItemDecoration(RecycleItem(this, 30))
        adapter.addChildClickViewIds(R.id.mContentView)
        adapter.setOnItemChildClickListener { adapter, view, position ->
            var bean = adapter.getItem(position)
            when (view.id) {
                R.id.mContentView -> {
                        var data = bean as MessageBean.SysMsg
                        if (data.notifyStatus == 0) {
                            clickBean = data
                            mViewModel.readMsg(data.id, data.notifyId)
                            clickPosition = position
                        } else {
                            bundle.clear()
                            bundle.putSerializable("bean", data)
                            RouteManager.goAct(ARouterUrl.MESSAGE_DETAIL, bundle)
                        }
                }
            }
        }

    }

    override fun liveObserver() {
        mViewModel.readLive.observe(this) {
            (adapter.data[clickPosition]).notifyStatus = 1
            adapter.notifyItemChanged(clickPosition)
            bundle.clear()
            bundle.putSerializable("bean", clickBean)
            RouteManager.goAct(ARouterUrl.MESSAGE_DETAIL, bundle)
        }
        mViewModel.warningMsgLive.observe(this) {
            it?.let {
                it.data?.let {
                    setDate(adapter,it?.systemMessage)
                }
            }

            }
        }

    override fun loadData() {
        mViewModel.getWarningMsg()
    }
}
