package com.sketch.ar.artdrawing.like

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.graphics.scale
import androidx.core.net.toUri
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
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 {

                    val uri =  if(CommonUtils.isInternalCacheFile(mContext,path)){
                        Log.d("ltt","=========1111===   ${path}")
                        path
                    }else{
                        Log.d("ltt","=========3222===   ${path}")
                        "file:///android_asset/$path".toUri()
                    }

                    mainImage.let {
                        Glide.with(mContext)
                            .asBitmap()
                            .load(uri)
                            .override(Target.SIZE_ORIGINAL)
                            .listener(object : RequestListener<Bitmap> {
                                override fun onLoadFailed(
                                    e: GlideException?,
                                    model: Any?,
                                    target: Target<Bitmap>,
                                    isFirstResource: Boolean
                                ): Boolean {
                                    var finalBitmap =   BitmapFactory.decodeFile(path).scale(600, 600)

                                    it.setImageBitmap(finalBitmap)

                                    return false
                                }

                                override fun onResourceReady(
                                    resource: Bitmap,
                                    model: Any,
                                    target: Target<Bitmap>?,
                                    dataSource: DataSource,
                                    isFirstResource: Boolean
                                ): Boolean {
                                    return false
                                }

                            })
                            .into(it)
                    }

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

        }


    }


}