package com.zxhy.solarmasterace.adapter

import android.annotation.SuppressLint
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.zxhy.solarmasterace.data.FileBean

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

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

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

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


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

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

    override fun getSelectData(): List<FileBean> {
        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()
    }


}