package com.aiosman.ravenow

import com.aiosman.ravenow.data.ChatService
import com.aiosman.ravenow.data.ChatServiceImpl
import com.aiosman.ravenow.entity.ChatNotification

/**
 * 保存一些关于聊天的状态
 */
object ChatState {
    val chatService: ChatService = ChatServiceImpl()
    var chatNotificationList = mutableListOf<ChatNotification>()
    suspend fun getStrategyByTargetTrtcId(targetTrtcId: String): ChatNotification? {
        // 先从缓存中查找
        if (chatNotificationList.isNotEmpty()) {
            chatNotificationList.forEach {
                if (it.targetTrtcId == targetTrtcId) {
                    return it
                }
            }
        }
        // 缓存中没有再从网络获取
        chatService.getChatNotifications(targetTrtcId)?.let {
            chatNotificationList.add(it)
            return it
        }
        // 存在未设置策略的情况
        return null
    }

    suspend fun updateChatNotification(targetUserId: Int, strategy: String): ChatNotification {
        val updatedData = chatService.updateChatNotification(targetUserId, strategy)
        chatNotificationList = chatNotificationList.filter {
            it.targetUserId != targetUserId
        }.toMutableList().apply {
            add(updatedData)
        }
        return updatedData
    }
}