package com.apps.pixwall.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.apps.pixwall.R
import com.apps.pixwall.data.Paper
import com.apps.pixwall.databinding.ItemDoubleLineBinding
import com.apps.pixwall.listener.MainInListener
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.request.RequestOptions

class DoubleLineAdapter: RecyclerView.Adapter<DoubleLineAdapter.DoubleViewHolder>() {
    private lateinit var context:Context
    private var list:List<Paper> = emptyList()
    private lateinit var listener:MainInListener
    class DoubleViewHolder(val binding:ItemDoubleLineBinding): RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DoubleViewHolder {
        context=parent.context
        val binding=ItemDoubleLineBinding.inflate(LayoutInflater.from(context),parent,false)
        return DoubleViewHolder(binding)
    }
    fun setList(list: List<Paper>){
        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: MainInListener){
        this.listener=listener
    }
}