package com.design.zenspace.tools

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.design.zenspace.R
import com.design.zenspace.entity.Feature
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.request.RequestOptions
import com.design.zenspace.databinding.ItemLikeWallBinding

class LikeWallAdapter: RecyclerView.Adapter<LikeWallAdapter.FeatureViewHolder>() {
    private lateinit var context: Context
    private var featureList:List<Feature> = emptyList()
    private lateinit var listener: SecFavListener
    class FeatureViewHolder(val binding: ItemLikeWallBinding): RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FeatureViewHolder {
        context=parent.context
        val binding= ItemLikeWallBinding.inflate(LayoutInflater.from(context),parent,false)
        return FeatureViewHolder(binding)
    }
    fun setList(list: List<Feature>){
        this.featureList=list
    }
    override fun getItemCount(): Int {
        return featureList.size
    }
    override fun onBindViewHolder(holder: FeatureViewHolder, position: Int) {
        val item= featureList[position]
        val previewUrl200 = item.previewUrl200
        val previewUrl400 = item.previewUrl400

//        holder.binding.text.text=item.description
        Glide.with(context).load(previewUrl400).placeholder(R.mipmap.bg_placeholder)
            .error(R.mipmap.bg_placeholder).thumbnail(
                Glide.with(context)
                    .asDrawable()
                    .load(previewUrl200)
                    .apply(
                        RequestOptions()
                            .format(DecodeFormat.PREFER_RGB_565)
                    )
                    .placeholder(R.mipmap.bg_placeholder)
                    .centerCrop()
            ).into(holder.binding.img)
        holder.binding.img.setOnClickListener {
            listener.inItemClick(item)
        }
    }
    fun setListener(listener: SecFavListener){
        this.listener=listener
    }
}