package com.adealink.weparty.im.list.adapter.viewbinder

import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.dot.NumDot
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.im.R
import com.adealink.weparty.im.databinding.LayoutSessionListOfficialItemBinding
import com.adealink.weparty.im.list.adapter.data.OfficialSessionListItem
import com.adealink.weparty.im.list.adapter.data.getMessageDisplayString
import com.adealink.weparty.im.list.adapter.data.getMessageDisplayTime
import com.adealink.weparty.util.formatTimeStr
import com.tencent.qcloud.tuikit.tuiconversation.minimalistui.interfaces.OnConversationAdapterListener


class OfficialListItemViewBinder(
    val listener: OnConversationAdapterListener
) :
    ItemViewBinder<OfficialSessionListItem, BindingViewHolder<LayoutSessionListOfficialItemBinding>>() {

    override fun onBindViewHolder(
        holder: BindingViewHolder<LayoutSessionListOfficialItemBinding>,
        item: OfficialSessionListItem,
    ) {
        holder.binding.root.onClick {
            listener.onItemClick(it, item.data.type, item.data)
        }
        holder.binding.tvTitle.text = getCompatString(R.string.im_official_msg_name)
        setLastMessageAndStatus(holder, item)
    }

    private fun setLastMessageAndStatus(
        holder: BindingViewHolder<LayoutSessionListOfficialItemBinding>,
        item: OfficialSessionListItem,
    ) {
        val conversation = item.data

        holder.binding.tvDesc.text = conversation.getMessageDisplayString()
        setLastMessageTime(holder, conversation.getMessageDisplayTime())
        holder.binding.vDot.show(NumDot(conversation.unRead))
    }

    private fun setLastMessageTime(
        holder: BindingViewHolder<LayoutSessionListOfficialItemBinding>,
        timeTs: Long?
    ) {
        holder.binding.tvTime.text = timeTs?.formatTimeStr()
    }

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