package com.drawing.drawsketch.collection

import android.content.Context
import android.content.Intent
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.drawing.drawsketch.BaseAdapter
import com.drawing.drawsketch.Utils
import com.drawing.drawsketch.databinding.CollectionAdapterBinding
import com.drawing.drawsketch.dbmodel.ResSvg
import com.drawing.drawsketch.preview.PreviewActivity

class CollectionAdapter(context: Context, var clickAction:( path:String)->Unit):BaseAdapter<ResSvg,CollectionAdapterBinding>(context) {
    override fun getViewBinding(parent: ViewGroup?): CollectionAdapterBinding {

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

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

            beanUpload.path?.let{ path->


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

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

        }


    }


}