package com.zj365.health.act

import android.annotation.SuppressLint
import android.view.View
import android.widget.ImageView
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.xty.common.util.CommonToastUtils
import com.zj365.health.R
import com.zj365.health.adapter.bloodfat.BloodLipidAdjustAdapter
import com.zj365.health.databinding.ActBloodFatHistoryBinding
import com.zj365.health.vm.BloodLipidAdjustRecordsVm


/**
 * 血脂校准记录
 */
@Route(path = ARouterUrl.BL_ADJUST_RECORD)
class BloodLipidAdjustRecordsAct : BaseListAct<BloodLipidAdjustRecordsVm>() {

    val binding by lazy { ActBloodFatHistoryBinding.inflate(layoutInflater) }
    override fun setLayout() = binding.root

    var isManage = false
    var isEdit = false
    val adapter by lazy { BloodLipidAdjustAdapter() }

    override fun initAdapter() {
        binding.mRecycle.adapter = adapter
        binding.mRecycle.addItemDecoration(RecycleItem(this, 50))
        binding.mRecycle.layoutManager = LinearLayoutManager(this)
        setRecycleRefresh(binding.mRecycle, binding.mRefresh, true)

        adapter.setOnItemClickListener { _, view, position ->
            val bloodFatBean = adapter.data[position]
            if (isManage) {
                val findViewById = view.findViewById<ImageView>(R.id.ivSelected)
                findViewById.isSelected = !findViewById.isSelected
                bloodFatBean.isSelected = findViewById.isSelected
            }
        }
    }

    override fun loadData() {
        mViewModel.getBloodLipidRecord(page)
    }

    override fun deleteData() {
        super.deleteData()
        mViewModel.deleteBloodFatDataRequest(adapter.getSelectData())
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener { finish() }
        binding.title.mTvTitle.text = "校准记录"

        binding.mTvManage.setOnClickListener {
            toManage(!isManage)
        }
        //退出管理
        binding.mTvExitManage.setOnClickListener {
            toManage(false)
        }
        //删除
        binding.mDelete.setOnClickListener {
            val selectData = adapter.getSelectData()
            if (selectData.isEmpty()) {
                CommonToastUtils.showToast("未勾选数据")
                return@setOnClickListener
            }
            deleteDialog.show()
        }
    }

    override fun liveObserver() {
        mViewModel.bloodLipidAdjustRecord.observe(this) {
            setDate(adapter, it.data.records)
            binding.title.mTvTitle.text = "校准记录(${it.data.total})"
        }

        mViewModel.deleteBloodFatData.observe(this) {
            if (it.code == 0) {
                page = 1
                loadData()
                CommonToastUtils.showSucceedToast("删除成功")
            } else {
                CommonToastUtils.showToast("删除失败：${it.msg}")
            }
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun toManage(isManager: Boolean) {
        isManage = isManager
        adapter.isSelect = isManager
        adapter.notifyDataSetChanged()
        if (isManager) {
            binding.mTvManage.visibility = View.GONE
            binding.mDelete.visibility = View.VISIBLE
            binding.mTvExitManage.visibility = View.VISIBLE
            isEdit = true
        } else {
            binding.mTvManage.visibility = View.VISIBLE
            binding.mDelete.visibility = View.GONE
            binding.mTvExitManage.visibility = View.GONE
            adapter.reset()
            isEdit = false
        }
    }
}