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.util.CommonToastUtils
import com.xty.common.util.CommonUtils
import com.zj365.health.R
import com.zj365.health.adapter.BloodHistoryAdapter
import com.zj365.health.databinding.ActBloodCalibeationHistoryBinding
import com.zj365.health.vm.BloodCalibrationVm

/**
 * 血压校准历史记录
 */
@Route(path = ARouterUrl.BLOOD_ALIGN_HISTORY)
class BloodCalibrationHistoryAct : BaseListAct<BloodCalibrationVm>() {

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

    val mAdapter by lazy { BloodHistoryAdapter() }

    var isManage = false
    var isEdit = false

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener { finish() }
        binding.title.mTvTitle.text = getString(R.string.blood_align_history)

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

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

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

    override fun loadData() {
        CommonUtils.getMyUserInfo()?.let {
            mViewModel.calibrationHistoryNew(it.id, page)
        }
    }

    override fun liveObserver() {
        mViewModel.calibrationHistoryLiveNew.observe(this) {
            it.data?.let { page ->
                val records = page.records
                setDate(mAdapter, records)
            }
        }
        //批量删除血压校准记录
        mViewModel.deleteDbpCalibrationData.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
        mAdapter.isSelect = isManager
        mAdapter.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
            mAdapter.reset()
            isEdit = false
        }
    }
}