package com.adealink.weparty.room.applymic

import com.adealink.frame.mvvm.livedata.ExtMutableLiveData
import com.adealink.frame.storage.cache.TimeoutLruCache

object ApplyMicStore {
    val onRecordChangedLD = ExtMutableLiveData<Unit>()

    private val applyMicCache by lazy {
        //<房间ID, 麦位序号>
        TimeoutLruCache<String, MutableSet<Int>>(
            200,
            10 * 60 * 1000
        ) //最多缓存200个，缓存时长10分钟
    }

    private fun getApplyMicList(roomId: String): MutableSet<Int> {
        var micIndexList = applyMicCache.get(roomId)
        if (micIndexList == null) {
            micIndexList = mutableSetOf()
            applyMicCache.put(roomId, micIndexList)
        }
        return micIndexList
    }

    fun addApplyRecord(roomId: String, micIndex: Int) {
        getApplyMicList(roomId).add(micIndex)
        onRecordChangedLD.postValue(Unit)
    }

    fun hasApply(roomId: String): Boolean {
        return getApplyMicList(roomId).isNotEmpty()
    }

    fun removeRecord(roomId: String?) {
        roomId ?: return
        applyMicCache.remove(roomId)
        onRecordChangedLD.postValue(Unit)
    }

    fun removeRecord(roomId: String, micIndex: Int) {
        getApplyMicList(roomId).remove(micIndex)
        onRecordChangedLD.postValue(Unit)
    }


    fun clearRecord() {
        applyMicCache.evictAll()
        onRecordChangedLD.postValue(Unit)
    }


}