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

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

//是否有@我的消息
  Map<int, int> isHaveAlterme = {};

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

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

  getHavaAltertime(int sessionId) {
    return isHaveAlterme[sessionId] ?? null;
  }

  setAlterMe(int sessionId, int time) {
    isHaveAlterme[sessionId] = time;
  }

  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);
    isHaveAlterme[sessionId] = null;
    if (count > 0) {
      unreadCountMap[sessionId] = 0;

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