import '../../../infrastructure/utility_widgets/loadable_hydrated_bloc/loadable_hydrated_bloc.dart';
import '../../../infrastructure/utility_widgets/loadable_hydrated_bloc/loadable_hydrated_bloc_event.dart';
import '../repository/marianum_message_repository.dart';
import 'marianum_message_event.dart';
import 'marianum_message_state.dart';

class MarianumMessageBloc
    extends
        LoadableHydratedBloc<
          MarianumMessageEvent,
          MarianumMessageState,
          MarianumMessageRepository
        > {
  @override
  Future<void> gatherData() async {
    var messages = await repo.getMessages();
    add(DataGathered((state) => state.copyWith(messageList: messages)));
  }

  @override
  MarianumMessageRepository repository() => MarianumMessageRepository();

  @override
  MarianumMessageState fromNothing() => const MarianumMessageState(
    messageList: MarianumMessageList(base: '', messages: []),
  );

  @override
  MarianumMessageState fromStorage(Map<String, dynamic> json) =>
      MarianumMessageState.fromJson(json);
  @override
  Map<String, dynamic>? toStorage(MarianumMessageState state) => state.toJson();
}
