package com.gdtlib.lib.adapter
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.gdtlib.lib.viewholder.BaseListViewHolder

abstract class BaseListAdapter :
  ListAdapter<BaseListItem, RecyclerView.ViewHolder>(
    BaseListItem.ITEM_CALLBACK
  ) {

  private var itemClickListener: ((Int) -> Unit)? = null
  private var loadMoreListener: (() -> Unit)? = null

  fun setItemClickListener(itemClickListener: ((Int) -> Unit)?) {
    this.itemClickListener = itemClickListener
  }

  fun setLoadMoreListener(loadMoreListener: (() -> Unit)?) {
    this.loadMoreListener = loadMoreListener
  }

  override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    if (holder is BaseListViewHolder) {
      holder.bindView(getItem(position))
      holder.setClickListener(itemClickListener)
      if (position >= itemCount - 1 - 2) {
        loadMoreListener?.invoke()
      }
    }
  }
}