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

import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.base.AppBase
import com.adealink.frame.data.json.froJsonErrorNull
import com.adealink.frame.dot.NumDot
import com.adealink.frame.log.Log
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.databinding.LayoutSessionListItemBinding
import com.adealink.weparty.im.list.adapter.data.CommonSessionListItemData
import com.adealink.weparty.util.formatTimeStr
import com.tencent.qcloud.tuikit.tuiconversation.bean.DraftInfo
import com.tencent.qcloud.tuikit.tuiconversation.minimalistui.interfaces.OnConversationAdapterListener
import com.tencent.qcloud.tuikit.tuiconversation.presenter.ConversationPresenter


class SessionListItemViewBinder(
    val listener: OnConversationAdapterListener
) :
    ItemViewBinder<CommonSessionListItemData, BindingViewHolder<LayoutSessionListItemBinding>>() {

    override fun onBindViewHolder(
        holder: BindingViewHolder<LayoutSessionListItemBinding>,
        item: CommonSessionListItemData,
    ) {
        Log.d("zhangfei", "onBindViewHolder, position:${holder.layoutPosition}, item:${item}")
        holder.binding.root.onClick {
            listener.onItemClick(it, item.data.type, item.data)
        }
        holder.binding.ivAvatar.onClick {

        }
        holder.binding.tvTitle.text = item.data.title
        setLastMessageAndStatus(holder, item)

        // TODO: 长按测试用
        if (!AppBase.isRelease) {
            holder.binding.root.setOnLongClickListener {
                listener.onItemLongClick(it, item.data)
                true
            }
        }
    }

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

        val draftInfo: DraftInfo? = conversation.draft
        if (draftInfo != null) {
            //草稿
            val draftText =
                (froJsonErrorNull<HashMap<*, *>>(draftInfo.draftText)?.get("content") as? String)
                    ?: draftInfo.draftText

            holder.binding.tvDesc.text = draftText
            setLastMessageTime(holder, draftInfo.draftTime)
        } else {
            //上一条消息
            val lasTUIMessageBean = conversation.lastTUIMessageBean
            if (lasTUIMessageBean != null) {
                val displayString = ConversationPresenter.getMessageDisplayString(lasTUIMessageBean)
                holder.binding.tvDesc.text = displayString
            }
            if (conversation.lastMessage != null) {
                setLastMessageTime(holder, conversation.lastMessageTime * 1000)
            }
        }

        holder.binding.vDot.show(NumDot(conversation.unRead))
    }

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

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