package com.base.scanqrclear.luma

import android.view.LayoutInflater
import android.view.ViewGroup
import com.base.scanqrclear.R
import com.base.scanqrclear.bean.SimilarBean
import com.base.scanqrclear.databinding.ItemSimilarPhotosChildBinding
import com.base.scanqrclear.luma.AppHelps.dpToPx
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 SimilarPhotosChildAdapter(
    val data: List<SimilarBean>
) : BaseAdapter<SimilarBean, ItemSimilarPhotosChildBinding>(data) {
    override fun getViewBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup
    ): ItemSimilarPhotosChildBinding {
        return ItemSimilarPhotosChildBinding.inflate(layoutInflater, parent, false)
    }

    override fun bind(
        holder: BaseViewHolder<ItemSimilarPhotosChildBinding>,
        position: Int,
        item: SimilarBean
    ) {
        val width = holder.itemView.context.resources.displayMetrics.widthPixels
        val margin = holder.itemView.context.dpToPx(45).toInt()
        val imageWidth = (width - margin) / 4
        holder.binding.ivPhoto.layoutParams.width = imageWidth
        holder.binding.ivPhoto.layoutParams.height = (imageWidth * 0.91).toInt()

        holder.binding.ivSelect.setImageResource(if (item.isSelect) R.mipmap.ic_check_s_photo else R.mipmap.ic_check_n)
        holder.itemView.setOnClickListener {
            item.isSelect = !item.isSelect
            holder.binding.ivSelect.setImageResource(if (item.isSelect) R.mipmap.ic_check_s_photo else R.mipmap.ic_check_n)
            callback?.onClicked(it, position, item)
        }

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