package com.ux.video.file.filerecovery.base

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView


abstract class NewBaseAdapter<K>(
    protected val mContext: Context
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    protected val data: MutableList<K> = mutableListOf()

    fun addData(items: List<K>?) {
        items?.let {
            val start = data.size
            data.addAll(it)
            notifyItemRangeInserted(start, it.size)
        }
    }

    fun setData(items: List<K>?) {
        data.clear()
        items?.let { data.addAll(it) }
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int = data.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return onCreateBinding(parent, viewType)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        onBind(holder, data[position], getItemViewType(position))
    }

    /**
     * 子类实现：根据 viewType 创建对应的 ViewHolder
     */
    protected abstract fun onCreateBinding(
        parent: ViewGroup,
        viewType: Int
    ): RecyclerView.ViewHolder

    /**
     * 子类实现：绑定数据
     */
    protected abstract fun onBind(holder: RecyclerView.ViewHolder, item: K, viewType: Int)
}
