package com.adealink.weparty.share.qrcode.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.commonui.recycleview.diffutil.BaseListItemData
import com.adealink.weparty.module.playmate.data.UserPlaymateCategoryData
import com.adealink.weparty.share.databinding.LayoutChooseCategoryItemBinding
import com.adealink.weparty.R as APP_R

data class CategoryItem(
    val data: UserPlaymateCategoryData,
    var selected: Boolean,
) : BaseListItemData

class CategoryItemViewBinder(
    val listener: CategoryChooseListener
) :
    ItemViewBinder<CategoryItem, BindingViewHolder<LayoutChooseCategoryItemBinding>>() {

    override fun onBindViewHolder(
        holder: BindingViewHolder<LayoutChooseCategoryItemBinding>,
        item: CategoryItem,
    ) {
        holder.binding.root.onClick {
            listener.onChoose(item.data)
        }
        holder.binding.ivCategory.setImageUrl(item.data.icon)
        holder.binding.tvCategory.text = item.data.name
        if (item.selected) {
            holder.binding.ivSelected.setImageResource(APP_R.drawable.common_check_selected_ic)
        } else {
            holder.binding.ivSelected.setImageResource(APP_R.drawable.common_check_unselected_ic)
        }
    }

    override fun onCreateViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup,
    ): BindingViewHolder<LayoutChooseCategoryItemBinding> {
        return BindingViewHolder(LayoutChooseCategoryItemBinding.inflate(inflater, parent, false))
    }

    interface CategoryChooseListener {
        fun onChoose(data: UserPlaymateCategoryData)
    }
}