package kr.co.vividnext.sodalive.explorer.profile

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.CircleCropTransformation
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.databinding.ItemUserProfileSimilarCreatorBinding

class UserProfileSimilarCreatorAdapter(
    private val onClickItem: (SimilarCreatorResponse) -> Unit
) : RecyclerView.Adapter<UserProfileSimilarCreatorAdapter.ViewHolder>() {

    val items = mutableListOf<SimilarCreatorResponse>()

    inner class ViewHolder(
        private val binding: ItemUserProfileSimilarCreatorBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: SimilarCreatorResponse) {
            binding.ivProfile.load(item.profileImage) {
                crossfade(true)
                placeholder(R.drawable.ic_place_holder)
                transformations(CircleCropTransformation())
            }

            binding.tvNickname.text = item.nickname
            binding.tvTags.text = item.tags.joinToString(" ") { "#$it" }
            binding.root.setOnClickListener { onClickItem(item) }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder(
        ItemUserProfileSimilarCreatorBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
    )

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(items[position])
    }

    override fun getItemCount() = items.count()
}
