package com.zxhy.simplefilesmanager.simplegrid

import android.content.Context
import android.net.Uri
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bumptech.glide.Glide
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.ItemJoIVJoSelectBinding
import com.zxhy.simplefilesmanager.simpleadpter.JoCommonJoAdapter
import com.zxhy.simplefilesmanager.simpledata.JoFileJo
import com.zxhy.simplefilesmanager.simplejoke.DipPxEx
import com.zxhy.simplefilesmanager.simplejoke.JoXmljoEx.jojoinflate

class SimpleGridAdapter(
    private val isVideo: Boolean = false,
    private val context: Context,
    private val selectClick: (count: Int) -> Unit,
    private val itemClick: (path: String) -> Unit,
) : JoCommonJoAdapter<SimpleGridAdapter.HH, JoFileJo>() {

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

    class HH(view: View) : ViewHolder(view) {
        val binding = ItemJoIVJoSelectBinding.bind(view)
    }

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

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

    }

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

        val context = holder.binding.root.context
        val data = joFileList[position]
        if (payloads.isEmpty()) {
            if (isVideo) holder.binding.ivCamera.visibility = View.VISIBLE
            Glide.with(context)
                .load(data.path)
                .apply(options)
                .into(holder.binding.iv)
            holder.binding.ivSelector.isSelected = data.isSelected
            holder.binding.ivSelector.setOnClickListener {
                data.isSelected = !data.isSelected
                holder.binding.ivSelector.isSelected = data.isSelected
                notifyItemChanged(position, "sumdk")
                selectClick.invoke(getSelectData().size)
            }
            holder.binding.root.setOnClickListener {
                itemClick.invoke(data.path)
            }

        } else {
            holder.binding.ivSelector.isSelected = data.isSelected
            super.onBindViewHolder(holder, position, payloads)
        }

    }
}