import 'dart:async';

import 'package:meta/meta.dart';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';

import 'package:{{ name }}/repository/AuthenticationRepository.dart';

part 'otp_event.dart';
part 'otp_state.dart';

class OtpBloc extends Bloc<OtpEvent, OtpState> {
  final AuthenticationRepository authenticationRepository;

  OtpBloc({
    @required this.authenticationRepository,
  })  : assert(authenticationRepository != null);

  @override
  OtpState get initialState => OtpInitial();

  @override
  Stream<OtpState> mapEventToState(
    OtpEvent event,
  ) async* {
    if (event is OtpSendButtonPressed) {
      yield OtpLoading();

      try {
        await authenticationRepository.reset(
          email: event.otp
        );

        yield OtpSuccces();
      } catch (error) {
        yield OtpFailure(error: error.toString());
      }
    }
  }
}
