package com.adealink.weparty.room.gift.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.aab.util.getCompatColor
import com.adealink.frame.util.onClick
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.room.R
import com.adealink.weparty.room.databinding.ItemRoomGiftUserBinding
import com.adealink.weparty.room.gift.data.RoomGiftUserItemData
import com.adealink.weparty.R as APP_R

class RoomGiftUserItemViewBinder(
    private val listener: (RoomGiftUserItemData) -> Unit
) :
    ItemViewBinder<RoomGiftUserItemData, BindingViewHolder<ItemRoomGiftUserBinding>>() {

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

    override fun onBindViewHolder(
        holder: BindingViewHolder<ItemRoomGiftUserBinding>,
        item: RoomGiftUserItemData
    ) {
        holder.binding.ivAvatar.setImageUrl(item.userInfo?.avatar)
        if (item.tag.isNullOrEmpty()) {
            holder.binding.tvTag.gone()
        } else {
            holder.binding.tvTag.show()
            holder.binding.tvTag.text = item.tag
        }

        if (item.isSelected) {
            holder.binding.vSelected.show()
            holder.binding.tvTag.setBackgroundResource(R.drawable.room_gift_user_tag_bg_selected)
            holder.binding.tvTag.setTextColor(getCompatColor(APP_R.color.white))
        } else {
            holder.binding.vSelected.gone()
            holder.binding.tvTag.setBackgroundResource(R.drawable.room_gift_user_tag_bg)
            holder.binding.tvTag.setTextColor(getCompatColor(APP_R.color.color_FF2C2945))
        }

        holder.binding.root.onClick {
            listener.invoke(item)
        }
    }
}