package com.quiescent.wallpaper.collection.test.tool

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.quiescent.wallpaper.collection.test.R
import com.quiescent.wallpaper.collection.test.bean.InfoList
import com.quiescent.wallpaper.collection.test.databinding.ItemHomeListBinding
import com.quiescent.wallpaper.collection.test.ui.applicationContext


class HomeAdapter(
    private val infoLists: List<InfoList>,
    private val function: (InfoList) -> Unit
) : RecyclerView.Adapter<HomeAdapter.ItemViewH>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewH {

        return ItemViewH(
            ItemHomeListBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )

    }

    override fun onBindViewHolder(holder: ItemViewH, position: Int) {
        val item = infoLists[position]

        holder.binding.viewSpace.isVisible = position % 2 <= 0

        holder.binding.linearLayout.setOnClickListener {
            function.invoke(item)
        }
        Glide.with(applicationContext).load(item.previewThumb)
            .placeholder(ContextCompat.getDrawable(applicationContext, R.mipmap.logo))
            .transform(RoundedCorners(Utils.unitCovert(10f)))
            .into(holder.binding.itemIm)

    }

    override fun getItemCount(): Int = infoLists.size

    inner class ItemViewH(var binding: ItemHomeListBinding) :
        RecyclerView.ViewHolder(binding.root) {


        override fun toString(): String {
            return super.toString()
        }
    }

}