import 'package:afterwork/domain/entities/user.dart';
import 'package:afterwork/domain/usecases/get_user.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class UserBloc extends Bloc<UserEvent, UserState> {
  final GetUser getUser;

  UserBloc({required this.getUser}) : super(UserInitial());

  Stream<UserState> mapEventToState(UserEvent event) async* {
    if (event is GetUserById) {
      yield UserLoading();
      final either = await getUser(event.id);

      yield either.fold(
        (failure) => UserError(),
        (user) => UserLoaded(user: user),
      );
    }
  }
}

abstract class UserEvent {}

class GetUserById extends UserEvent {
  final String id;

  GetUserById(this.id);
}

abstract class UserState {}

class UserInitial extends UserState {}

class UserLoading extends UserState {}

class UserLoaded extends UserState {
  final User user;

  UserLoaded({required this.user});
}

class UserError extends UserState {}
