package pxds.hpls.xkxo

import android.text.format.Formatter
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bumptech.glide.Glide
import com.test.basd.supercleanermax.R
import com.test.basd.supercleanermax.databinding.XqvqfrBinding
import pxds.dlpf.buoz.fico.Ipnk.inflate
import pxds.lmmd.zmwf.Egac
import pxds.lmmd.zmwf.Khko.setTrackedOnClickListener
import pxds.lmmd.zmwf.Khko.string
import pxds.vazp.FileBean

class Eqvl(
    val itemClick: (data: FileBean) -> Unit, val selectClick: (selectList: List<FileBean>) -> Unit
) : Adapter<Eqvl.UUU>() {

    private val files = arrayListOf<FileBean>()

    private val TAG = R.string.ބޔށޡށ_D.string()

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

    fun removeData(deleteList: ArrayList<FileBean>) {
        files.removeAll(deleteList.toSet())
        notifyDataSetChanged()
    }

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

    }

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

    }

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

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

        if (payloads.isEmpty()) {
            binding.mhofkmm.isVisible = data.isVideo()
            when {
                data.isImage() -> {
                    Glide.with(content).load(data.path).into(binding.klpobam)
                }

                data.isVideo() -> {
                    Glide.with(content).load(data.path).into(binding.klpobam)
                }

                data.isAudio() -> {
                    binding.klpobam.setImageResource(R.mipmap.lhsuaqcm)
                }

                data.isDoc() -> {
                    binding.klpobam.setImageResource(R.mipmap.cbzopvid)
                }

                data.isApk() -> {
                    binding.klpobam.setImageResource(R.mipmap.xwpfssvf)
                }

                data.isZip() -> {
                    binding.klpobam.setImageResource(R.mipmap.jkbrlpal)
                }

                else -> {
                    binding.klpobam.setImageResource(R.mipmap.pvtrprkz)
                }
            }
            binding.ivrxehl.text = data.name
            binding.wkrduju.text = Formatter.formatFileSize(content, data.size)
            binding.root.setTrackedOnClickListener {
                itemClick.invoke(data)
            }
            binding.khqvveq.isSelected = data.isSelect
            binding.bmgbylr.setTrackedOnClickListener {
                data.isSelect = !data.isSelect
                binding.khqvveq.isSelected = data.isSelect
                notifyItemChanged(position, R.string.ޚޣށއޞ_D.string())
                val selectList = files.filter { it.isSelect }
                selectClick.invoke(selectList)
            }
        } else {
            binding.khqvveq.isSelected = data.isSelect
            super.onBindViewHolder(holder, position, payloads)
        }

    }

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

    fun setData(bigFileList: List<FileBean>) {
        bigFileList.forEach {
            Egac.logDebug(TAG, R.string.ދޙޅޤޝ_D.string((it).toString()))
        }
        files.clear()
        files.addAll(bigFileList)
        files.sortByDescending { it.size }
        notifyDataSetChanged()
    }

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