package com.lukouguoji.gjc.holder

import android.view.View
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableList.OnListChangedCallback
import com.lukouguoji.gjc.R
import com.lukouguoji.gjc.databinding.ItemBoxAddBinding
import com.lukouguoji.module_base.base.BaseViewHolder
import com.lukouguoji.module_base.base.CommonAdapter
import com.lukouguoji.module_base.bean.GjcBoxAddListBean
import com.lukouguoji.module_base.bean.JianBean
import com.lukouguoji.module_base.ktx.refresh

class BoxAddListViewHolder(view: View) :
    BaseViewHolder<GjcBoxAddListBean, ItemBoxAddBinding>(view) {

    override fun onBind(item: Any?, position: Int) {
        val bean = getItemBean(item) ?: return

        binding.bean = bean

        binding.rv.adapter = CommonAdapter(
            itemView.context,
            R.layout.item_box_add_j,
            BoxAddListJViewHolder::class.java
        )

        binding.rv.refresh(bean.jianList)

        bean.jianList.addOnListChangedCallback(object :OnListChangedCallback<ObservableArrayList<JianBean>>(){
            override fun onChanged(sender: ObservableArrayList<JianBean>?) {
                binding.rv.refresh(bean.jianList)
            }

            override fun onItemRangeChanged(
                sender: ObservableArrayList<JianBean>?,
                positionStart: Int,
                itemCount: Int
            ) {
                binding.rv.refresh(bean.jianList)
            }

            override fun onItemRangeInserted(
                sender: ObservableArrayList<JianBean>?,
                positionStart: Int,
                itemCount: Int
            ) {
                binding.rv.refresh(bean.jianList)
            }

            override fun onItemRangeMoved(
                sender: ObservableArrayList<JianBean>?,
                fromPosition: Int,
                toPosition: Int,
                itemCount: Int
            ) {
                binding.rv.refresh(bean.jianList)
            }

            override fun onItemRangeRemoved(
                sender: ObservableArrayList<JianBean>?,
                positionStart: Int,
                itemCount: Int
            ) {
                binding.rv.refresh(bean.jianList)
            }
        })

        notifyItemClick(position, binding.tvBatch)
        notifyItemClick(position, binding.tvDeleteOrder)
        notifyItemClick(position, binding.tvDeleteMatter)
        notifyItemClick(position, binding.ivShow)
    }
}