package com.adealink.weparty.module.room.chat.livemessage

import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.adealink.weparty.commonui.recycleview.adapter.DefaultDiffCallback
import com.adealink.weparty.commonui.recycleview.adapter.MultiTypeListAdapter

class LiveMessageAdapter<T : ILiveMessage>(
    private val liveMessageRecyclerHelper: LiveMessageRecyclerHelper<T>,
    diffCallback: DiffUtil.ItemCallback<T> = DefaultDiffCallback()
) : MultiTypeListAdapter<T>(diffCallback) {

    override fun onCurrentListChanged(previousList: List<T>, currentList: List<T>) {
        val newCount = currentList.size - previousList.size
        if (newCount >= 0) {
            //新增消息
            liveMessageRecyclerHelper.notifyAndScrollToBottom(newCount, previousList.isNotEmpty())
        } else {
            //消息达到上限后,裁剪到最早收到的消息,此时停留到上一次看的地方
            liveMessageRecyclerHelper.scrollToCurrentReadPosition()
        }
    }

    override fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder) {
        super.onViewAttachedToWindow(holder)
        liveMessageRecyclerHelper.onAdapterItemViewAttached(holder.layoutPosition)
    }
}