package importantedto.deserveedto.manufactureredto.viableedto

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.BlandedtoBinding
import importantedto.concreteedto.cameedto.Vigorousedto
import importantedto.gallonedto.Tumbleedto
import importantedto.maidedto.witnessedto.nationalismedto.Gestureedto.inflate

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

    private val files = arrayListOf<Vigorousedto>()

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

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UUU {
        return UUU(R.layout.blandedto.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 = BlandedtoBinding.bind(holder.itemView)
        val data = files[position]
        val content = holder.itemView.context


        if (payloads.isEmpty()) {

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

    }

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

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

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

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