package com.design.wallpapergallery.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.design.wallpapergallery.R
import com.design.wallpapergallery.data.Photo
import com.design.wallpapergallery.listener.MainSecListener
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.request.RequestOptions
import com.design.wallpapergallery.databinding.ItemAddLikeBinding

class AddLikeAdapter: RecyclerView.Adapter<AddLikeAdapter.ThreeViewHolder>() {
    private lateinit var context: Context
    private var list:List<Photo> = emptyList()
    private lateinit var listener: MainSecListener
    class ThreeViewHolder(val binding: ItemAddLikeBinding): RecyclerView.ViewHolder(binding.root)

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