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

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.CallSuper
import androidx.viewbinding.ViewBinding
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.im.databinding.LayoutSessionMessageCenterBaseBinding
import com.adealink.weparty.util.getSessionTimeStr
import com.tencent.qcloud.tuikit.timcommon.bean.TUIMessageBean
import com.tencent.qcloud.tuikit.timcommon.interfaces.OnItemClickListener


abstract class BaseCenterMessageViewBinder<T : TUIMessageBean, V : ViewBinding, MH : CenterMessageViewHolder<T, V>>(
    protected var onItemClickListener: OnItemClickListener?
) : ItemViewBinder<T, MH>() {

    override fun onBindViewHolder(holder: MH, item: T) {
        initView(holder, item)
        setTimeTitle(holder, item)
        holder.onBindMessage(holder.messageBinding, item)
    }

    @CallSuper
    open fun initView(
        holder: MH, msg: T
    ) {
        holder.binding.root.onClick {
            onItemClickListener?.onMessageLongClick(holder.binding.root, msg)
            true
        }
        if (msg.status == TUIMessageBean.MSG_STATUS_SEND_FAIL) {
            //消息发送失败触发长按
            holder.binding.root.onClick {
                onItemClickListener?.onMessageLongClick(holder.binding.root, msg)
                true
            }
        } else {
            holder.binding.root.onClick {
                onItemClickListener?.onMessageClick(holder.binding.root, msg)
                true
            }
        }
        holder.initView(holder.messageBinding, msg)
    }

    private fun setTimeTitle(holder: MH, msg: T) {
        val position = holder.layoutPosition
        val last: TUIMessageBean? = adapter.items.getOrNull(position - 1) as? TUIMessageBean
        val timeStr = getSessionTimeStr(msg.messageTime * 1000, (last?.messageTime ?: 0) * 1000)
        if (timeStr.isNullOrEmpty()) {
            holder.binding.tvTime.gone()
        } else {
            holder.binding.tvTime.show()
            holder.binding.tvTime.text = timeStr
        }
    }

    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): MH {
        val rootBinding = LayoutSessionMessageCenterBaseBinding.inflate(inflater, parent, false)
        return onCreateMessageHolder(rootBinding, inflater, rootBinding.messageContent)
    }

    abstract fun onCreateMessageHolder(
        rootBinder: LayoutSessionMessageCenterBaseBinding,
        inflater: LayoutInflater,
        parent: ViewGroup
    ): MH

}

abstract class CenterMessageViewHolder<T : TUIMessageBean, V : ViewBinding>(
    rootBinding: LayoutSessionMessageCenterBaseBinding,
    val messageBinding: V
) : BindingViewHolder<LayoutSessionMessageCenterBaseBinding>(rootBinding) {

    open fun initView(binding: V, msg: T) {}

    abstract fun onBindMessage(binding: V, msg: T)
}