package com.zxhy.simplefilesmanager.simpleadpter

import android.annotation.SuppressLint
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.zxhy.simplefilesmanager.simpledata.JoFileJo

@SuppressLint("NotifyDataSetChanged")
abstract class JoCommonJoAdapter<T : ViewHolder, E : JoFileJo>() : RecyclerView.Adapter<T>() {

    val joFileList = arrayListOf<E>()

    override fun getItemCount(): Int {
        return joFileList.size
    }

    fun setData(data: List<E>) {
        joFileList.clear()
        joFileList.addAll(data)
        notifyDataSetChanged()
    }

    fun addData(data: List<E>) {
        joFileList.addAll(data)
        notifyDataSetChanged()
    }

    fun clearData() {
        if (joFileList.size == 0) return
        joFileList.clear()
        notifyDataSetChanged()
    }

    fun removeData(data: List<E>) {
        data.forEach {
            val index = joFileList.indexOf(it)
            if (index != -1) {
                joFileList.removeAt(index)
                notifyItemRemoved(index)
            }
        }
    }

    fun getSelectData(): List<E> {
        return joFileList.filter { it.isSelected }
    }

    fun toggleSelect(select: Boolean) {
        joFileList.forEach { fileBean ->
            fileBean.isSelected = select
        }
        notifyDataSetChanged()
    }


}