package com.zxhy.simplefilesmanager.simplelist

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.zxhy.simplefilesmanager.R
import com.zxhy.simplefilesmanager.databinding.ItemJoListFileJoBinding
import com.zxhy.simplefilesmanager.simpleadpter.JoCommonJoAdapter
import com.zxhy.simplefilesmanager.simpledata.JoListFileJo
import com.zxhy.simplefilesmanager.simpledata.JoListFileJo.Companion.sizeF
import com.zxhy.simplefilesmanager.simplejoke.DipPxEx
import com.zxhy.simplefilesmanager.simplejoke.JoXmljoEx.jojoinflate

class SimpleListAdapter(
    val context: Context,
    private val selectClick: (select: Boolean, count: Int) -> Unit
) : JoCommonJoAdapter<SimpleListAdapter.MultiCC, JoListFileJo>() {

    private val options =
        RequestOptions()
            .transform(CenterCrop(), RoundedCorners(DipPxEx.dp2px(context, R.dimen.dp_8)))

    class MultiCC(view: View) : ViewHolder(view) {
        val binding = ItemJoListFileJoBinding.bind(view)
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MultiCC {
        return MultiCC(R.layout.item_jo_list_file_jo.jojoinflate(parent))
    }

    override fun onBindViewHolder(holder: MultiCC, position: Int) {}

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: MultiCC, position: Int, payloads: MutableList<Any>) {

        val data = joFileList[position]
        val context = holder.binding.root.context

        if (payloads.isEmpty()) {
            bindIcon(data, holder.binding.iv)
            holder.binding.tvName.text = data.fName
            val file = data.toFile()
            if (file.isFile) {
                holder.binding.tvInfo.text = data.timeE + "  ${file.sizeF(context)}"
            } else {
                val count = file.list()?.size ?: 0
                holder.binding.tvInfo.text = data.timeE + "  ${count}items"
            }
            holder.binding.ivSelector.setOnClickListener {
                data.isSelected = !data.isSelected
                holder.binding.ivSelector.isSelected = data.isSelected
                notifyItemChanged(position, "sdada")
                selectClick.invoke(data.isSelected, getSelectData().size)
            }
        } else {
            holder.binding.root.setOnClickListener {
                holder.binding.ivSelector.isSelected = data.isSelected
            }
            super.onBindViewHolder(holder, position, payloads)
        }

    }


}