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.databinding.ItemMoreAndSearchBinding
import com.design.wallpapergallery.listener.MainSecListener
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.request.RequestOptions

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

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