package com.gh.gamecenter.category2

import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.databinding.RecyclerCategorySelectedTagBinding

class SelectedTagsAdapter(val click: (CategoryV2ViewModel.SelectedTags) -> Unit) :
    ListAdapter<CategoryV2ViewModel.SelectedTags, SelectedTagsAdapter.TagsViewHolder>(
        createDiffUtil()
    ) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TagsViewHolder {
        return TagsViewHolder(parent.toBinding())
    }

    override fun onBindViewHolder(holder: TagsViewHolder, position: Int) {
        val item = getItem(position)
        holder.binding.root.setText(item.category.name ?: "")
        holder.itemView.setOnClickListener {
            click(item)
        }
    }

    companion object {

        private fun createDiffUtil() = object : DiffUtil.ItemCallback<CategoryV2ViewModel.SelectedTags>() {
            override fun areItemsTheSame(
                oldItem: CategoryV2ViewModel.SelectedTags,
                newItem: CategoryV2ViewModel.SelectedTags
            ): Boolean {
                return oldItem.category.id == newItem.category.id
            }

            override fun areContentsTheSame(
                oldItem: CategoryV2ViewModel.SelectedTags,
                newItem: CategoryV2ViewModel.SelectedTags
            ): Boolean {
                return oldItem == newItem
            }

        }
    }

    class TagsViewHolder(val binding: RecyclerCategorySelectedTagBinding) : RecyclerView.ViewHolder(binding.root)
}