package com.swago.baseswago.im

import android.text.TextUtils

/**
 *@date 2021/10/1 11:50
 *description:
 */
class IMGroupNewMsgListener : IGroupNewMsg{

    var groupId = ""

    var isAttach = false

    private val hashMap = HashMap<Int, ((iGroupNewMsgBean: IGroupNewMsgBean) -> Unit)?>()

    fun <T : IGroupNewMsgBean> handleMsgType(
        type: Int,
        call: ((iGroupMsgModel: T) -> Unit)
    ): IMGroupNewMsgListener {
        hashMap[type] = call as ((IGroupNewMsgBean) -> Unit)
        return this
    }

    fun attach() {
        isAttach = true
        IMMessageManager.addGroupMsgListener(this)
    }

    fun detach() {
        isAttach = false
        IMMessageManager.removeGroupMsgListener(this)
    }

    override fun onNewMessage(iGroupNewMsgBean: IGroupNewMsgBean) {
        val task = hashMap[iGroupNewMsgBean.getType()]
        if (!TextUtils.isEmpty(groupId)) {
            if (groupId != iGroupNewMsgBean.getGroupId()) {
                return
            }
        }
        task?.invoke(iGroupNewMsgBean)
    }
}