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.XTangHistoryAdapter
import com.zj365.health.databinding.ActHistoryXtangBinding
import com.zj365.health.vm.XTangVm

/**
 *
 * 历史血糖上传记录
 */
@Route(path = ARouterUrl.XT_HISTORY)
class XTangHistoryAct : BaseListAct<XTangVm>() {

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

    var isManage = false
    var isEdit = false
    var userId = ""

    val adapter by lazy { XTangHistoryAdapter() }

    override fun initData() {
        super.initData()
        isDarkBar = true
        intent.extras?.let {
            if (it.containsKey("id")) {
                userId = it.getString("id").toString()
            }
        }
    }

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

        //管理
        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 deleteData() {
        mViewModel.deleteBloodCalibrationRequest(adapter.getSelectData())
    }

    @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 loadData() {
        userId?.let {
            mViewModel.getXtHistoryNew(it.toInt(), page)
        }
    }


    @SuppressLint("NotifyDataSetChanged")
    override fun liveObserver() {
        mViewModel.xtSectionList.observe(this) {
            if (null == it.data) return@observe
            it.data?.let { page ->
                page?.let {
                    setDate(adapter, it.records)
                }
            }
        }
        //删除血糖校准
        mViewModel.deleteBloodCalibrationData.observe(this) {
            if (it.code == 0) {
                page = 1
                loadData()
                CommonToastUtils.showSucceedToast("删除成功")
            } else {
                CommonToastUtils.showToast("删除失败：${it.msg}")
            }
        }
    }

    override fun initAdapter() {
        setRecycleRefresh(binding.mRecycle, binding.mRefresh, true)
        binding.mRecycle.addItemDecoration(RecycleItem(this, 10))
        binding.mRecycle.layoutManager = LinearLayoutManager(this)
        binding.mRecycle.adapter = adapter
//        adapter.setOnItemClickListener { adapter, view, position ->
//            var bean = adapter.getItem(position) as XTangHistoryBean
//        }
        adapter.setOnItemClickListener { _, view, position ->
            val bloodSugarNewbean = adapter.data[position]
            if (isManage) {
                val findViewById = view.findViewById<ImageView>(R.id.ivSelected)
                findViewById.isSelected = !findViewById.isSelected
                bloodSugarNewbean.isSelected = findViewById.isSelected
            }
        }
    }
}