package kr.co.vividnext.sodalive.settings.event

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import coil.load
import kr.co.vividnext.sodalive.databinding.ItemEventBinding

class EventAdapter(
    private val onClick: (EventItem) -> Unit
) : RecyclerView.Adapter<EventAdapter.ViewHolder>() {

    val items = mutableListOf<EventItem>()

    inner class ViewHolder(
        private val binding: ItemEventBinding,
        private val onClick: (EventItem) -> Unit
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(event: EventItem) {
            binding.ivEvent.load(event.thumbnailImageUrl)
            binding.root.setOnClickListener { onClick(event) }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            ItemEventBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            ),
            onClick
        )
    }

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

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