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

import com.adealink.weparty.commonui.recycleview.adapter.ExtMultiTypeAdapter
import com.adealink.weparty.im.constant.OFFICIAL_CONVERSATION_ID
import com.adealink.weparty.im.list.adapter.data.CommonSessionListItemData
import com.adealink.weparty.im.list.adapter.data.OfficialSessionListItem
import com.adealink.weparty.im.list.adapter.data.SessionListItemData
import com.tencent.qcloud.tuikit.tuiconversation.bean.ConversationInfo
import com.tencent.qcloud.tuikit.tuiconversation.interfaces.IConversationListAdapter
import com.tencent.qcloud.tuikit.tuiconversation.minimalistui.interfaces.OnConversationAdapterListener

/**
 * 参考 ConversationListAdapter, 抽象出业务逻辑
 */
class SessionListAdapter : ExtMultiTypeAdapter(), IConversationListAdapter {

    private var mIsLoading = false
    var mOnConversationAdapterListener: OnConversationAdapterListener? = null

    private var conversationList = listOf<ConversationInfo>()

    override fun onLoadingStateChanged(isLoading: Boolean) {
        this.mIsLoading = isLoading
        notifyItemChanged(itemCount - 1)
    }

    override fun onDataSourceChanged(conversationInfoList: List<ConversationInfo?>?) {
        val newList = conversationInfoList?.mapNotNull { it } ?: emptyList()

        conversationList = newList
        val itemList = mutableListOf<SessionListItemData>()
        var officialConversation: ConversationInfo? = null
        for (info in newList) {
            if (info.conversation.conversationID == OFFICIAL_CONVERSATION_ID) {
                officialConversation = info
                itemList.add(0, OfficialSessionListItem(info))
            } else {
                itemList.add(CommonSessionListItemData(info))
            }
        }
        if (officialConversation == null) {
            itemList.add(0, OfficialSessionListItem(ConversationInfo().apply {
                id = OFFICIAL_CONVERSATION_ID
            }))
        }
        items = itemList
    }

    override fun onViewNeedRefresh() {
        notifyDataSetChanged()
    }

    override fun onItemRemoved(position: Int) {
        if (conversationList.firstOrNull()?.conversationId == OFFICIAL_CONVERSATION_ID) {
            notifyItemRemoved(position)
        } else {
            notifyItemRemoved(position + 1)
        }
    }

    override fun onItemInserted(position: Int) {
        if (conversationList.firstOrNull()?.conversationId == OFFICIAL_CONVERSATION_ID) {
            notifyItemInserted(position)
        } else {
            notifyItemInserted(position + 1)
        }
    }

    override fun onItemChanged(position: Int) {
        if (conversationList.firstOrNull()?.conversationId == OFFICIAL_CONVERSATION_ID) {
            notifyItemChanged(position)
        } else {
            notifyItemChanged(position + 1)
        }
    }

    override fun onItemRangeChanged(startPosition: Int, count: Int) {
        if (conversationList.firstOrNull()?.conversationId == OFFICIAL_CONVERSATION_ID) {
            notifyItemRangeChanged(startPosition, count)
        } else {
            notifyItemRangeChanged(startPosition + 1, count)
        }
    }

    override fun onConversationChanged(conversationInfoList: List<ConversationInfo?>?) {
        mOnConversationAdapterListener?.onConversationChanged(conversationInfoList);
    }
}