package com.zxhy.fastfilemanagerpro.kokoadapter

import android.annotation.SuppressLint
import android.util.Log
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.zxhy.fastfilemanagerpro.kokodata.KoFileDatako

abstract class CommonAdapter<T : ViewHolder?>() : RecyclerView.Adapter<T>(),
    AdapterCommonDataFunction<KoFileDatako> {

    protected val fileList = arrayListOf<KoFileDatako>()
    private var firstRefresh = true

    @SuppressLint("NotifyDataSetChanged")
    override fun setData(data: List<KoFileDatako>) {
        fileList.clear()
        fileList.addAll(data)
        notifyDataSetChanged()
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun addData(data: List<KoFileDatako>) {
        fileList.addAll(data)
        notifyDataSetChanged()
    }


    @SuppressLint("NotifyDataSetChanged")
    override fun clearData() {
        if (fileList.size == 0) return
        fileList.clear()
        notifyDataSetChanged()
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun removeData(data: List<KoFileDatako>) {
        data.forEach {
            val index = fileList.indexOf(it)
            if (index != -1) {
                fileList.removeAt(index)
                notifyItemRemoved(index)
            }
        }
    }

    override fun getSelectData(): List<KoFileDatako> {
        return fileList.filter { it.isSelect }
    }

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

    @SuppressLint("NotifyDataSetChanged")
    override fun toggleSelect(select: Boolean) {
        fileList.forEach { fileBean ->
            fileBean.isSelect = select
        }
        notifyDataSetChanged()
    }


}