import 'package:food_delivery_client/feature/auth/domain/usecases/reset_password_usecase.dart';
import 'package:food_delivery_client/feature/common/presentation/widgets/w_toastification.dart';
import 'package:food_delivery_client/food_delivery_client.dart';

part 'reset_password_event.dart';

part 'reset_password_state.dart';

part 'reset_password_bloc.freezed.dart';

@injectable
class ResetPasswordBloc extends Bloc<ResetPasswordEvent, ResetPasswordState> {
  final ResetPasswordUseCase _passwordUseCase;

  ResetPasswordBloc(this._passwordUseCase) : super(const ResetPasswordState()) {
    on<_Loaded>(_onLoaded);
  }

  Future<void> _onLoaded(
    _Loaded event,
    Emitter<ResetPasswordState> emit,
  ) async {
    emit(state.copyWith(status: RequestStatus.loading));
    final response = await _passwordUseCase.call(event.params);
    response.fold(
      (l) {
        showErrorToast(l.errorMessage);
        emit(state.copyWith(status: RequestStatus.error));
      },
      (r) {
        showSuccessToast(r.message);
        emit(state.copyWith(status: RequestStatus.loaded));
      },
    );
  }
}
