import 'package:chat/utils/MessageMgr.dart';

class UnreadCountProvider {
//未读消息条数管理
  Map<int, int> unreadCountMap = {};

  updateUnreadCount(int sessionId, int count) {
    if (unreadCountMap[sessionId] == null) {
      unreadCountMap[sessionId] = 0;
    }
    unreadCountMap[sessionId] += count;

    MessageMgr().emit('Update UnreadCount', sessionId);
  }

  clear() {
    unreadCountMap.clear();
  }

  int getUnreadCount(int sessionId) {
    return unreadCountMap[sessionId] ?? 0;
  }

  bool checkUnreadMsg() {
    for (var v in unreadCountMap.values) {
      if (v > 0) {
        return true;
      }
    }
    return false;
  }

  signRead(int sessionId) {
    print('消除未读标记:$sessionId');

    int count = getUnreadCount(sessionId);
    if (count > 0) {
      unreadCountMap[sessionId] = 0;

      MessageMgr().emit('Update UnreadCount', sessionId);
    }
  }
}
