package com.zxhy.simplefilesmanager.simplefile

import android.content.Context
import android.net.Uri
import android.view.RoundedCorner
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.load.resource.bitmap.TransformationUtils.centerCrop
import com.bumptech.glide.request.RequestOptions
import com.zxhy.simplefilesmanager.R
import com.zxhy.simplefilesmanager.databinding.ItemJoImageJoRecentBinding
import com.zxhy.simplefilesmanager.simpleadpter.JoCommonJoAdapter
import com.zxhy.simplefilesmanager.simpledata.JoFileJo
import com.zxhy.simplefilesmanager.simpledata.JoJoRecentImage
import com.zxhy.simplefilesmanager.simplejoke.DipPxEx.dp2px
import com.zxhy.simplefilesmanager.simplejoke.JoXmljoEx.jojoinflate

class SimpleRecentAdapter(
    val context: Context,
    private val itemClick: (uri: Uri) -> Unit
) : JoCommonJoAdapter<SimpleRecentAdapter.AA, JoJoRecentImage>() {
    private val options =
        RequestOptions()
            .transform(CenterCrop(), RoundedCorners(dp2px(context, R.dimen.dp_8)))

    class AA(view: View) : ViewHolder(view) {
        val binding = ItemJoImageJoRecentBinding.bind(view)
    }

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

    override fun onBindViewHolder(holder: AA, position: Int) {
        val context = holder.binding.root.context
        val data = joFileList[position]
        Glide.with(context)
            .load(data.uri)
            .apply(options)
            .into(holder.binding.iv)
        holder.binding.root.setOnClickListener {
            itemClick.invoke(data.uri)
        }
    }
}