package com.adealink.weparty.module.im.dot

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.adealink.frame.dot.Dot
import com.adealink.frame.dot.NumDot
import com.adealink.frame.util.AppUtil
import com.tencent.qcloud.tuicore.TUIConstants

val messageDot by lazy {
    Dot(
        arrayListOf(
            messageSessionDot,
        ),
        "MessageDot"
    )
}

val messageSessionDot by lazy { MessageSessionDot() }

class MessageSessionDot(children: ArrayList<Dot> = arrayListOf()) :
    Dot(children, "MessageSessionDot") {

    init {
        val unreadCountReceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context?, intent: Intent) {
                val unreadCount = intent.getLongExtra(TUIConstants.UNREAD_COUNT_EXTRA, 0)
                show(NumDot(unreadCount.toInt()))
            }
        }
        val unreadCountFilter = IntentFilter()
        unreadCountFilter.addAction(TUIConstants.CONVERSATION_UNREAD_COUNT_ACTION)
        LocalBroadcastManager.getInstance(AppUtil.appContext)
            .registerReceiver(unreadCountReceiver, unreadCountFilter)
    }

}