package rowedto.jockeyedto.sheetedto.meadowedto.naturaledto.idealedto

import android.annotation.SuppressLint
import android.text.format.Formatter
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.base.datarecovery.R
import com.base.datarecovery.databinding.TruckedtoBinding
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Ambitionedto.inflate
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Cunningedto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Dishedto

class Bulletedto(
    val itemClick: (data: Cunningedto) -> Unit,
    val selectClick: (selectList: List<Cunningedto>) -> Unit
) : Adapter<Bulletedto.UUU>() {
    private val TAG = "BigFileAdapter"

    private val files = arrayListOf<Cunningedto>()

    class UUU(view: View) : ViewHolder(view) {

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UUU {
        return UUU(R.layout.truckedto.inflate(parent))
    }

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

    override fun onBindViewHolder(holder: UUU, position: Int) {

    }

    override fun onBindViewHolder(holder: UUU, position: Int, payloads: MutableList<Any>) {

        val binding = TruckedtoBinding.bind(holder.itemView)
        val data = files[position]
        val content = holder.itemView.context


        if (payloads.isEmpty()) {

            binding.glassesedto.text = data.path.substring(data.path.lastIndexOf("/") + 1)
            binding.bulletinedto.text = Formatter.formatFileSize(content, data.size)
            binding.root.setOnClickListener {
                itemClick.invoke(data)
            }
            binding.relationshipedto.isSelected = data.isSelect
            binding.affectedto.setOnClickListener {
                data.isSelect = !data.isSelect
                binding.relationshipedto.isSelected = data.isSelect
                notifyItemChanged(position, "dasds")
                val selectList = files.filter { it.isSelect }
                selectClick.invoke(selectList)
            }
        } else {
            binding.relationshipedto.isSelected = data.isSelect
            super.onBindViewHolder(holder, position, payloads)
        }

    }

    @SuppressLint("NotifyDataSetChanged")
    fun setData(bigFileList: List<Cunningedto>) {
        bigFileList.forEach {
            Dishedto.logDebug(TAG, "$it")
        }
        files.clear()
        files.addAll(bigFileList)
        files.sortByDescending { it.size }
        notifyDataSetChanged()
    }

    fun getSelectData(): List<Cunningedto> {
        return files.filter { it.isSelect }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun removeData(deleteList: List<Cunningedto>) {
        files.removeAll(deleteList.toSet())
        notifyDataSetChanged()
    }

    fun isEmpty(): Boolean {
        return files.isEmpty()
    }
}