package com.base.scanqrclear.luma
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.base.scanqrclear.bean.FileBean
import com.base.scanqrclear.databinding.ItemWhatsappCleanBinding
import com.base.scanqrclear.luma.AppHelps.dpToPx
import com.base.scanqrclear.luma.MineTypeHelps.TYPE_VIDEO
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

class WhatsappCleanAdapter(
    var data: List<FileBean>,
    val type: Int = TYPE_VIDEO
) : BaseAdapter<FileBean, ItemWhatsappCleanBinding>(data.take(3)) {

    constructor(type: Int = TYPE_VIDEO): this(emptyList(), type)

    fun updateData(data: List<FileBean>) {
        this.data = data
        submitList(data.take(3))
    }

    override fun getViewBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup
    ): ItemWhatsappCleanBinding {
        return ItemWhatsappCleanBinding.inflate(layoutInflater, parent, false)
    }

    override fun bind(
        holder: BaseViewHolder<ItemWhatsappCleanBinding>,
        position: Int,
        item: FileBean
    ) {
        val width = holder.itemView.context.resources.displayMetrics.widthPixels
        val margin = holder.itemView.context.dpToPx(128).toInt()
        val imageWidth = (width - margin) / 3
        holder.binding.ivImage.layoutParams.width = imageWidth
        holder.binding.ivImage.layoutParams.height = imageWidth

        val round = holder.itemView.context.dpToPx(8).toInt()
        val options = RequestOptions().transform(CenterCrop(), RoundedCorners(round))
        Glide.with(holder.itemView.context)
            .load(item.path)
            .apply(options)
            .into(holder.binding.ivImage)

        holder.binding.tvSize.text = ""
        holder.binding.ivVideo.visibility = if (type == TYPE_VIDEO) View.VISIBLE else View.GONE
        if (data.size > 3 && position == itemCount - 1) {
            holder.binding.tvSize.text = "+${data.size - itemCount}"
        } else {
            holder.binding.ivShadow.visibility = View.GONE
        }
    }
}