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 'ResetLinkEvent.dart';
part 'ResetLinkState.dart';

class ResetLinkBloc extends Bloc<ResetLinkEvent, ResetLinkState> {
  final AuthenticationRepository authenticationRepository;

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

  @override
  ResetLinkState get initialState => ResetLinkInitial();

  @override
  Stream<ResetLinkState> mapEventToState(ResetLinkEvent event) async* {
    if (event is ResetButtonPressed) {
      yield ResetLinkLoading();

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

        yield ResetLinkSuccces();
      } catch (error) {
        yield ResetLinkFailure(error: error.toString());
      }
    }
  }
}