mirrored_repositories/evil_word/lib/presentation/home_page/state
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:evil_word/domain/entities/joke_entity.dart';
import 'package:evil_word/domain/repository/joke_repository.dart';
import 'package:meta/meta.dart';

part 'joke_event.dart';
part 'joke_state.dart';

class JokeBloc extends Bloc<JokeEvent, JokeState> {
  JokeBloc({required this.jokeRepository}) : super(JokeInitialState()) {
    on<JokeEvent>((event, emit) async {
      await _onLoadJokeEvent(event, emit);
    });
  }
  final JokeRepository jokeRepository;

  Future<JokeEntity> _getJoke() async => await jokeRepository.fetchJoke();

  Future<void> _onLoadJokeEvent(
      JokeEvent event, Emitter<JokeState> emit) async {
    if (event is LoadJokesEvent) {
      emit(JokeLoadState());
      try {
        final joke = await _getJoke();
        emit(JokeLoadedState(joke: joke));
      } catch (e) {
        emit(JokeLoadedWithErrorState(message: e.toString()));
      }
    }
  }
}
