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.zj365.health.R
import com.zj365.health.adapter.UricCalibrationListAdapter
import com.zj365.health.databinding.ActUricCalibrationHistoryBinding

import com.zj365.health.vm.UricAcidVm

/**
 * 尿酸记录列表
 */
@Route(path = ARouterUrl.URIC_CALIBRATION_HISTORY)
class UricCalibrationHistoryAct : BaseListAct<UricAcidVm>() {

    private val binding by lazy { ActUricCalibrationHistoryBinding.inflate(layoutInflater) }
    override fun setLayout(): View = binding.root

    var isManage = false
    var isEdit = false
    private var userId: String = ""
    val adapter by lazy { UricCalibrationListAdapter() }

    override fun initData() {
        super.initData()

        userId = intent.getStringExtra("userId").toString()
    }

    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 initAdapter() {
        binding.mRecycle.adapter = adapter
        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
            }
        }
    }

    @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
        }
    }

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

    override fun loadData() {
        mViewModel.getUricCalibrationListRequest(page, userId)
    }

    @SuppressLint("SetTextI18n")
    override fun liveObserver() {
        mViewModel.getUricCalibrationListLive.observe(this) {
            setDate(adapter, it.data.records)
            binding.title.mTvTitle.text = "校准记录(${it.data.total})"
        }
        //删除尿酸校准记录
        mViewModel.deteleUricCalibrationLive.observe(this) {
            if (it.code == 0) {
                page = 1
                loadData()
                CommonToastUtils.showSucceedToast("删除成功")
            } else {
                CommonToastUtils.showToast("删除失败：${it.msg}")
            }
        }
    }
}