package com.zj365.health.act

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ConvertUtils
import com.xty.base.act.BaseListAct
import com.xty.common.TimeSelect
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.event.BodyEvent
import com.xty.common.recycleUtils.RecycleItem
import com.xty.common.util.CommonToastUtils
import com.zj365.health.R
import com.zj365.health.adapter.BodyInfoHistory
import com.zj365.health.databinding.ActBodyInfoHistoryBinding
import com.zj365.health.vm.BodyManagerVm
import com.xty.network.model.BodyInfo
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 *  身体管理历史数据
 */
@Route(path = ARouterUrl.BODY_MANAGER_ACT_HISTORY)
class BodyInfoHistoryAct : BaseListAct<BodyManagerVm>() {

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

    private val timeSelect by lazy {
        TimeSelect(this) {
            val toString = binding.tvAll.text.toString()
            toString.let {
                val split = it.split("-")
                year = split[0]
                month = split[1]
            }
            page = 1
            loadData()
        }.apply {
            showYear = true
            showMonth = true
            showDay = false
            showHour = false
            showMin = false
            showSec = false
        }
    }

    private val mAdapter by lazy { BodyInfoHistory() }
    var userId: String? = null
    var year: String = ""
    var month: String = ""

    override fun initView() {
        userId = intent.getStringExtra("userId")
        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener { finish() }
        binding.title.mTvTitle.text = "历史数据"

        binding.title.mTvRight.apply {
            text = "退出管理"
            setTextColor(resources.getColor(R.color.col_02c))
            setOnClickListener {
                toManage(false)
            }
        }

        binding.tvAll.setOnClickListener {
            timeSelect.selectTimeWithYearMonth(it, "yyyy-MM")
        }

        binding.tvUploadDate.setOnClickListener {
            mViewModel.orderSort = 1
            binding.ivUpload.setImageResource(R.mipmap.paixu_2)
            binding.ivTestDate.setImageResource(R.mipmap.paixu)
            page = 1
            loadData()
        }
        binding.tvTestDate.setOnClickListener {
            mViewModel.orderSort = 2
            binding.ivUpload.setImageResource(R.mipmap.paixu)
            binding.ivTestDate.setImageResource(R.mipmap.paixu_2)
            page = 1
            loadData()
        }
        //管理

        binding.mTvManage.setOnClickListener {
            toManage(true)
        }

        binding.tvChange.setOnClickListener {
            if (mAdapter.selIds.size == 1) {
                var iterator = mAdapter.selIds.values.iterator()
                var position = iterator.next()
                var bean = mAdapter.data[position].apply { isChange = true }
                bundle.clear()
                bundle.putSerializable("bean", bean)
                RouteManager.goAct(ARouterUrl.BODY_INFO_SHOW_OR_ADD, bundle)
                toManage(false)
            } else {
                CommonToastUtils.showToast("只能选中一个进行修改")
            }
        }
        binding.mDelete.setOnClickListener {
            if (mAdapter.selIds.isEmpty()) {
                CommonToastUtils.showToast("请选中需要删除的条目")
                return@setOnClickListener
            } else {
                deleteDialog.show()
            }

        }
        binding.mAddData.setOnClickListener {
            bundle.clear()
            bundle.putString("userId", userId)
            RouteManager.goAct(ARouterUrl.BODY_INFO_SHOW_OR_ADD, bundle)

        }
        super.initView()

    }

    override fun deleteData() {
        var ids = mAdapter.selIds.keys.toIntArray().joinToString(",")
        mViewModel.deleteBodyInfo(ids)
    }

    fun toManage(isManager: Boolean) {
        mAdapter.isSelect = isManager
        mAdapter.notifyDataSetChanged()
        if (isManager) {
            binding.mTvManage.visibility = View.GONE
            binding.mDelete.visibility = View.VISIBLE
            binding.mAddData.visibility = View.GONE
            binding.tvChange.visibility = View.VISIBLE
            binding.title.mTvRight.visibility = View.VISIBLE

        } else {
            binding.mTvManage.visibility = View.VISIBLE
            binding.mDelete.visibility = View.GONE
            binding.mAddData.visibility = View.VISIBLE
            binding.tvChange.visibility = View.GONE
            binding.title.mTvRight.visibility = View.GONE
        }
    }

    override fun initAdapter() {
        setRecycleRefresh(binding.mRecycle, binding.mRefresh)
        binding.mRecycle.adapter = mAdapter
        binding.mRecycle.layoutManager = LinearLayoutManager(this)
        var tvItem = RecycleItem(this, 0, 1).apply {
            setSpace(1, 1)
        }
        binding.mRecycle.addItemDecoration(RecycleItem(this, ConvertUtils.dp2px(18f)))
        mAdapter.addChildClickViewIds(R.id.mDetail)
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            var bean = adapter.getItem(position) as BodyInfo
            bundle.clear()
            bundle.putSerializable("bean", bean)
            RouteManager.goAct(ARouterUrl.BODY_INFO_SHOW_OR_ADD, bundle)
        }

        mAdapter.setOnItemClickListener { adapter, view, position ->
            if (mAdapter.isSelect) {
                var bean = adapter.getItem(position) as BodyInfo
                bean.isSelect = !bean.isSelect
                adapter.notifyItemChanged(position)


            }
        }
    }

    override fun loadData() {
        mViewModel.bodyInfoList(page, year, month, userId)
    }

    override fun liveObserver() {


        mViewModel.bodyListLive.observe(this) {

            binding.tvNumAll.text = "共${it.data.total}条"
            setDate(mAdapter, it.data.records)
        }
        mViewModel.deleteLive.observe(this) {
            CommonToastUtils.showSucceedToast("删除成功")
            if (mAdapter.selIds.size == mAdapter.data.size) {
                page = 1
                loadData()
                toManage(false)
                return@observe
            }
            var iterator = mAdapter.selIds.iterator()
            while (iterator.hasNext()) {
                var next = iterator.next()
                mAdapter.removeAt(next.value)
            }
            mAdapter.notifyDataSetChanged()
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun bodyInfo(bean: BodyEvent) {
        page = 1
        loadData()
    }
}