package com.example.defenseapplication.utils

object MessageStatusManager {
    private var unreadCount = 0
    private val listeners = mutableListOf<UnreadCountChangeListener>()

    fun getUnreadCount(): Int {
        return unreadCount
    }

    fun setUnreadCount(count: Int) {
        unreadCount = if (count > 99) 99 else count
        notifyListeners()
    }

    fun incrementUnreadCount() {
        unreadCount++
        if (unreadCount > 99) unreadCount = 99
        notifyListeners()
    }

    fun clearUnreadCount() {
        unreadCount = 0
        notifyListeners()
    }

    fun addListener(listener: UnreadCountChangeListener) {
        if (!listeners.contains(listener)) {
            listeners.add(listener)
        }
    }

    fun removeListener(listener: UnreadCountChangeListener) {
        listeners.remove(listener)
    }

    private fun notifyListeners() {
        listeners.forEach { it.onUnreadCountChanged(unreadCount) }
    }

    interface UnreadCountChangeListener {
        fun onUnreadCountChanged(count: Int)
    }
}