package com.adealink.weparty.room.invite.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.aab.util.getCompatString
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.module.profile.data.Gender
import com.adealink.weparty.room.databinding.LayoutRoomMemberListItemBinding
import com.adealink.weparty.room.invite.data.RoomMemberItemData
import com.adealink.weparty.R as APP_R

class RoomMemberViewBinder(
    val onAvatarClick: (item: RoomMemberItemData, position: Int) -> Unit,
    val onInviteClick: (item: RoomMemberItemData, position: Int) -> Unit
) : ItemViewBinder<RoomMemberItemData, BindingViewHolder<LayoutRoomMemberListItemBinding>>() {
    override fun onCreateViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup
    ): BindingViewHolder<LayoutRoomMemberListItemBinding> {
        return BindingViewHolder(LayoutRoomMemberListItemBinding.inflate(inflater, parent, false))
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(
        holder: BindingViewHolder<LayoutRoomMemberListItemBinding>,
        item: RoomMemberItemData
    ) {
        holder.binding.ivAvatar.onClick {
            onAvatarClick.invoke(item, holder.layoutPosition)
        }
        holder.binding.tvIndex.text = (holder.layoutPosition + 1).toString()
        holder.binding.ivAvatar.setImageUrl(item.data.avatar)
        holder.binding.tvName.text = item.data.nickName
        when (item.data.gender) {
            Gender.MALE.gender -> {
                holder.binding.vGender.show()
                holder.binding.vGender.setImageResource(APP_R.drawable.common_male_ic)
            }

            Gender.FEMALE.gender -> {
                holder.binding.vGender.show()
                holder.binding.vGender.setImageResource(APP_R.drawable.common_female_ic)
            }

            else -> {
                holder.binding.vGender.gone()
            }
        }

        if (item.invited) {
            holder.binding.btnInvite.isEnabled = false
            holder.binding.btnInvite.text = getCompatString(APP_R.string.common_invited)
        } else {
            holder.binding.btnInvite.isEnabled = true
            holder.binding.btnInvite.text = getCompatString(APP_R.string.common_invite)
            holder.binding.btnInvite.onClick {
                onInviteClick.invoke(item, holder.layoutPosition)
            }
        }
    }

}