package com.zj365.health.act.binddevice

import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.xty.base.act.BaseListAct
import com.xty.common.TimeSelect
import com.xty.common.event.TZhongDataManageEvent
import com.xty.common.util.CommonToastUtils
import com.zj365.health.R
import com.zj365.health.adapter.binddevice.BindDeviceHistoryAdapter
import com.zj365.health.databinding.ActBindDeviceHistoryBinding
import com.zj365.health.vm.HistoryDeviceVm
import com.xty.network.model.BindDevHistoryBean
import org.greenrobot.eventbus.EventBus
import java.lang.StringBuilder

open class BaseDeviceHistoryAct<V : HistoryDeviceVm> : BaseListAct<V>() {
    val binding by lazy { ActBindDeviceHistoryBinding.inflate(layoutInflater) }
    var mAdapter: BindDeviceHistoryAdapter? = null
    var date: String = ""
    val timeSelect by lazy {
        TimeSelect(this) {
            date = binding.tvSelectTime.text.toString()
            loadData()
        }
    }

    var isManage: Boolean = true
    var mDeleteData = mutableListOf<BindDevHistoryBean>()

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
//        binding.title.mTvTitle.text="历史记录"
        binding.title.mIvBack.setOnClickListener { finish() }
        timeSelect.startDate.set(1922, 0, 1)
        binding.tvSelectTime.setOnClickListener {
            timeSelect.selectTime(it)
        }

        mAdapter?.apply {
            setOnItemClickListener { adapter, view, position ->
                if (!isManage) {
                    val bodyData = adapter.data[position] as BindDevHistoryBean
                    bodyData.isSelect = !bodyData.isSelect
                    notifyItemChanged(position)
                    if (bodyData.isSelect) {
                        mDeleteData.add(bodyData)
                    } else {
                        mDeleteData.remove(bodyData)
                    }
                }
            }
        }

        binding.mTvManage.setOnClickListener {

            isManage = !isManage
            if (isManage) { //退出管理
                binding.mTvManage.text = "管理"
                binding.mTvManage.setBackgroundResource(R.drawable.selector_change)
                binding.mTvManage.setTextColor(ContextCompat.getColor(this@BaseDeviceHistoryAct,R.color.white))
                mAdapter?.isSelect = false
                binding.mDelete.visibility = View.GONE

                if (mDeleteData.isNotEmpty()) {
                    mDeleteData.clear()
                }
            } else {  //管理
                binding.mTvManage.text = "退出管理"
                binding.mTvManage.setBackgroundResource(R.drawable.shape_gray_click)
                binding.mTvManage.setTextColor(ContextCompat.getColor(this@BaseDeviceHistoryAct,R.color.col_02c))

                mAdapter?.isSelect = true
                binding.mDelete.visibility = View.VISIBLE

            }
            mAdapter?.notifyDataSetChanged()
        }

        binding.mDelete.setOnClickListener {
            if (mDeleteData.isEmpty()) {
                CommonToastUtils.showLoserToast("请勾选数据")
                return@setOnClickListener
            }
            deleteDialog.show()
        }
    }

    override fun deleteData() {
        val sb = StringBuilder()
        mDeleteData.forEach {
            sb.append("${it.id},")
        }
        mViewModel.deleteDevicesDatas(sb.toString())
    }

    override fun liveObserver() {
        mViewModel.bindOk.observe(this) {
            mAdapter?.let { adapter ->
                it.data?.let {
                    setDate(adapter, it.records)

                    if (adapter.data.isEmpty()){
                        binding.llBottom.visibility = View.GONE
                    }else{
                        binding.llBottom.visibility = View.VISIBLE
                    }
                }
            }
        }

        mViewModel.deleteSuccess.observe(this) {
            CommonToastUtils.showSucceedToast("删除成功")
            EventBus.getDefault().post(TZhongDataManageEvent(""))

            mAdapter?.apply {
                data.removeAll(mDeleteData)
                notifyDataSetChanged()
                mDeleteData.clear()
                if (data.isEmpty()) {
                    binding.llBottom.visibility = View.GONE
                    binding.mTvManage.text = "管理"
                    binding.mTvManage.setBackgroundResource(R.drawable.selector_change)
                    binding.mTvManage.setTextColor(ContextCompat.getColor(this@BaseDeviceHistoryAct,R.color.white))

                    isSelect = false
                    isManage = true
                    binding.mDelete.visibility = View.GONE
                    loadData()
                }else{
                    binding.llBottom.visibility = View.VISIBLE
                }
            }
        }
    }

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

    override fun loadData() {

    }
}