package com.paperapp.lenscape.tools

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.paperapp.lenscape.R
import com.paperapp.lenscape.element.Element
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.request.RequestOptions
import com.paperapp.lenscape.databinding.ItemLikeWallBinding

class LikeWallpapersAdapter: RecyclerView.Adapter<LikeWallpapersAdapter.ElementViewHolder>() {
    private lateinit var context: Context
    private var elementList:List<Element> = emptyList()
    private lateinit var listener: SetLoveListener
    class ElementViewHolder(val binding: ItemLikeWallBinding): RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ElementViewHolder {
        context=parent.context
        val binding= ItemLikeWallBinding.inflate(LayoutInflater.from(context),parent,false)
        return ElementViewHolder(binding)
    }
    fun setList(list: List<Element>){
        this.elementList=list
    }
    override fun getItemCount(): Int {
        return elementList.size
    }
    override fun onBindViewHolder(holder: ElementViewHolder, position: Int) {
        val item= elementList[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: SetLoveListener){
        this.listener=listener
    }
}