package com.zj365.health.act

import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.xty.base.act.BaseListAct
import com.xty.common.arouter.ARouterUrl
import com.xty.common.recycleUtils.RecycleItem
import com.zj365.health.R
import com.zj365.health.adapter.BloodInfoAdapter
import com.zj365.health.databinding.ActDataListBinding
import com.zj365.health.vm.ElectronicListVm
import com.xty.network.model.BloodBean

/**
 * 血压历史数据
 * 血糖历史数据
 */
@Route(path = ARouterUrl.BLOOD_INFO)
class BloodInfoHistoryAct: BaseListAct<ElectronicListVm>() {
    val binding by lazy{ ActDataListBinding.inflate(layoutInflater)}
    val mAdapter by lazy{ BloodInfoAdapter() }
    var type=1
    var deletePosition=0

    override fun setLayout()=binding.root
    override fun initData() {
        super.initData()
        isDarkBar=true
        intent.extras?.let {
            type=it!!.getInt("type")
        }

    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener { finish() }
        when(type){
            1-> binding.title.mTvTitle.text="血压"
            2-> binding.title.mTvTitle.text="血糖"
        }


    }
    override fun initAdapter() {
        if(type==1){
            mAdapter.unit="mmHg"
        }else{
            mAdapter.unit="mml/L"
        }
        mAdapter.type=type
        binding.mRecycle.adapter=mAdapter
        binding.mRecycle.addItemDecoration(RecycleItem(this,50))
        binding.mRecycle.layoutManager= LinearLayoutManager(this)
        mAdapter.addChildClickViewIds(R.id.mContent,R.id.mDelete)
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            var bean=adapter.getItem(position) as BloodBean
            deletePosition=position
            when(view.id){
                R.id.mContent->{}
                R.id.mDelete->{
                    when(type){
                        1->mViewModel.deleteBlood(bean.id)
                        2->mViewModel.deleteClu(bean.id)
                    }
                }

            }
        }
        setRecycleRefresh(binding.mRecycle,binding.mRefresh)


    }

    override fun loadData() {
        when(type){
            1->mViewModel.getBlood(page)
            2->mViewModel.getCluList(page)
        }
    }
    override fun liveObserver() {
        mViewModel.bloodLive.observe(this){
              setDate(mAdapter,it.data.records)
        }
        mViewModel.deleteClu.observe(this){
            if(mAdapter.data.size==1){
                setDate(mAdapter, mutableListOf())
            }else{
                mAdapter.data.removeAt(deletePosition)
                mAdapter.notifyItemRemoved(deletePosition)
            }
        }
        mViewModel.deleteBlood.observe(this){
            if(mAdapter.data.size==1){
                setDate(mAdapter, mutableListOf())
            }else{
                mAdapter.data.removeAt(deletePosition)
                mAdapter.notifyItemRemoved(deletePosition)
            }
        }

    }

}