import 'package:didvan/models/chat_room/chat_room.dart';
import 'package:didvan/models/enums.dart';
import 'package:didvan/providers/core.dart';
import 'package:didvan/services/network/request.dart';
import 'package:didvan/services/network/request_helper.dart';

class DirectListState extends CoreProvier {
  final List<ChatRoom> chatRooms = [];
  int _unreadCount = 0;
  int get unreadCount => _unreadCount;

  set unreadCount(int value) {
    _unreadCount = value;
    notifyListeners();
  }

  Future<void> getDirectsList() async {
    final RequestService service = RequestService(RequestHelper.directs);
    await service.httpGet();
    if (service.isSuccess) {
      _unreadCount = service.result['unread'] ?? 0;
      final List chatRoomsData = service.result['chatRooms'] ?? [];
      for (var i = 0; i < chatRoomsData.length; i++) {
        chatRooms.add(ChatRoom.fromJson(chatRoomsData[i]));
      }
      appState = AppState.idle;
      return;
    }
    appState = AppState.failed;
  }
}
