import '../../../../../api/demo/data/demo_talk.dart';
import '../../../../../api/demo/demo_mode.dart';
import '../../../../../api/marianumcloud/talk/create_room/create_room.dart';
import '../../../../../api/marianumcloud/talk/create_room/create_room_params.dart';
import '../../../../../api/marianumcloud/talk/room/get_room_cache.dart';
import '../../../../../api/marianumcloud/talk/room/get_room_response.dart';
import '../../../../../api/request_cache.dart';

class ChatListDataProvider {
  Future<GetRoomResponse> getRooms({
    void Function(Object)? onError,
    bool renew = false,
  }) {
    if (DemoMode.active) return Future.value(DemoTalk.rooms());
    return resolveFromCache<GetRoomResponse>(
      (onUpdate, onError) =>
          GetRoomCache(renew: renew, onUpdate: onUpdate, onError: onError),
      onError: onError,
      operationName: 'getRooms',
    );
  }

  /// Returns the token of the created (or already existing) 1:1 room, or null
  /// in demo mode where no room is actually created.
  Future<String?> createDirectRoom(String invite) async {
    if (DemoMode.active) return null;
    final response = await CreateRoom(
      CreateRoomParams(roomType: 1, invite: invite),
    ).run();
    return response.data.token;
  }
}
