package ai.guiji.duix.test.ui.adapter

import ai.guiji.duix.test.databinding.ItemAvatarCardBinding
import ai.guiji.duix.test.model.AvatarModel
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView

class AvatarAdapter(
    private val list: List<AvatarModel>,
    private val callback: Callback
) : RecyclerView.Adapter<AvatarAdapter.ViewHolder>() {

    class ViewHolder(val binding: ItemAvatarCardBinding) :
        RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemAvatarCardBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        )
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val model = list[position]
        holder.binding.ivAvatar.setImageResource(model.avatarRes)
        holder.binding.tvName.text = model.name
        holder.binding.cardAvatar.setOnClickListener {
            callback.onSelect(model)
        }
    }

    override fun getItemCount(): Int = list.size

    interface Callback {
        fun onSelect(model: AvatarModel)
    }
}
