package com.sketch.ar.artdrawing.like

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.sketch.ar.artdrawing.dataBean.DataLocal
import com.sketch.ar.artdrawing.databinding.AdapterLikeBinding
import com.sketch.ar.artdrawing.utils.BaseAdapter
import com.sketch.ar.artdrawing.utils.CommonUtils

class LikeAdapter(context: Context, var clickAction:(path:String)->Unit):
    BaseAdapter<DataLocal, AdapterLikeBinding>(context) {
    override fun getViewBinding(parent: ViewGroup?): AdapterLikeBinding {

       return  AdapterLikeBinding.inflate(
           LayoutInflater.from(parent?.context),
           parent,
           false
       )
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val itemHolder  = holder as VHolder<AdapterLikeBinding>
        data[position].let {
            val beanUpload: DataLocal = it

            beanUpload.path?.let{ path->


                itemHolder.vb.run {
                    CommonUtils.loadImage(mContext,path){ bitmap: Bitmap? ->
                        if(bitmap == null){
                            mainImage.setImageBitmap(BitmapFactory.decodeFile(path))
                        }else{
                            mainImage.setImageBitmap(bitmap)
                        }

                    }
                    mainImage.setOnClickListener(View.OnClickListener {
                        clickAction.invoke(path)
                    })
                }
            }

        }


    }


}