package kr.co.vividnext.sodalive.explorer

import android.annotation.SuppressLint
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.ItemExplorerSectionBinding

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

    private val items = mutableListOf<GetExplorerSectionCreatorResponse>()

    inner class ViewHolder(
        private val binding: ItemExplorerSectionBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: GetExplorerSectionCreatorResponse) {
            binding.tvNickname.text = item.nickname
            binding.tvTags.text = item.tags

            binding.ivProfile.load(item.profileImageUrl) {
                transformations(CircleCropTransformation())
                placeholder(R.drawable.ic_logo)
                crossfade(true)
            }

            binding.root.setOnClickListener { onClickItem(item.id) }
        }
    }

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

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

    override fun getItemCount() = items.size

    @SuppressLint("NotifyDataSetChanged")
    fun addItems(items: List<GetExplorerSectionCreatorResponse>) {
        this.items.addAll(items)
        notifyDataSetChanged()
    }
}
