package com.adealink.weparty.profile.relation.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
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.profile.databinding.ItemRelationshipVisitorBinding
import com.adealink.weparty.profile.relation.data.VisitorItemData


class VisitorItemViewBinder(val listener: OnVisitorItemClick) :
    ItemViewBinder<VisitorItemData, BindingViewHolder<ItemRelationshipVisitorBinding>>() {

    override fun onBindViewHolder(
        holder: BindingViewHolder<ItemRelationshipVisitorBinding>,
        item: VisitorItemData,
    ) {
        holder.binding.root.onClick {
            listener.goProfile(item.data.uid)
        }
        holder.binding.ivAvatar.setImageUrl(item.data.avatar)
        holder.binding.tvUserName.text = item.data.nickName
        holder.binding.vGender.setGender(item.data.gender)
        holder.binding.vGender.setAge(item.data.age)

        if (item.data.online) {
            holder.binding.vOnline.show()
            holder.binding.vOnline.onStart()
        } else {
            holder.binding.vOnline.gone()
            holder.binding.vOnline.onStop()
        }
    }

    override fun onViewDetachedFromWindow(holder: BindingViewHolder<ItemRelationshipVisitorBinding>) {
        super.onViewDetachedFromWindow(holder)
        holder.binding.vOnline.onStop()
    }

    override fun onViewRecycled(holder: BindingViewHolder<ItemRelationshipVisitorBinding>) {
        holder.binding.vOnline.onStop()
        super.onViewRecycled(holder)
    }

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

    interface OnVisitorItemClick {
        fun goProfile(uid: String)
    }
}