package rallyedto.dumpedto.editionedto

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.PlagiarizeedtoBinding
import rallyedto.farewelledto.Strategicedto.inflate
import rallyedto.policeedto.huntedto.Outletedto
import rallyedto.talledto.blewedto.Musicaledto

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

    private val files = arrayListOf<Outletedto>()

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

    }

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


        if (payloads.isEmpty()) {

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

    }

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

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

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

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