package com.adealink.weparty.account.register.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.util.onClick
import com.adealink.weparty.account.R
import com.adealink.weparty.account.databinding.LayoutRegisterCategoryItemBinding
import com.adealink.weparty.commonui.commonSelectedDrawable
import com.adealink.weparty.commonui.ext.dpf
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.ui.category.data.CategoryListItemData

class CategoryListItemViewBinder(val listener: OnSelectListener) :
    ItemViewBinder<CategoryListItemData, BindingViewHolder<LayoutRegisterCategoryItemBinding>>() {

    override fun onBindViewHolder(
        holder: BindingViewHolder<LayoutRegisterCategoryItemBinding>,
        item: CategoryListItemData,
    ) {
        holder.binding.root.onClick {
            listener.onSelect(item, holder.layoutPosition)
        }
        holder.binding.ivImg.setImageUrl(item.data.icon)
        holder.binding.tvTab.text = item.data.name
        if (item.selected) {
            holder.binding.root.setBackgroundResource(R.drawable.account_register_category_item_selected_bg)
            holder.binding.ivImg.setBackgroundResource(R.drawable.account_register_category_selected_bg)
            holder.binding.ivItemFrame.show()
        } else {
            holder.binding.root.setBackgroundResource(R.drawable.account_register_category_item_bg)
            holder.binding.ivImg.setBackgroundResource(0)
            holder.binding.ivItemFrame.gone()
        }
    }

    override fun onCreateViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup,
    ): BindingViewHolder<LayoutRegisterCategoryItemBinding> {
        val binding = LayoutRegisterCategoryItemBinding.inflate(inflater, parent, false)
        binding.ivItemFrame.background = commonSelectedDrawable(100.dpf(), 1.dpf())
        return BindingViewHolder(binding)
    }

    interface OnSelectListener {
        fun onSelect(item: CategoryListItemData, pos: Int)
    }
}