import 'package:aitrainer_app/repository/user_repository.dart';
import 'package:aitrainer_app/util/common.dart';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';

part 'password_reset_event.dart';
part 'password_reset_state.dart';

class PasswordResetBloc extends Bloc<PasswordResetEvent, PasswordResetState> {
  final UserRepository userRepository;
  PasswordResetBloc({required this.userRepository}) : super(PasswordResetInitial()) {
    on<PasswordResetEmailChange>(_onEmailChange);
    on<PasswordResetSubmit>(_onSubmit);
  }

  void _onEmailChange(PasswordResetEmailChange event, Emitter<PasswordResetState> emit) {
    emit(PasswordResetLoading());
    final String email = event.email;
    userRepository.setEmail(email);
    emit(PasswordResetReady());
  }

  void _onSubmit(PasswordResetSubmit event, Emitter<PasswordResetState> emit) async {
    emit(PasswordResetLoading());
    await userRepository.resetPassword();
    emit(PasswordResetReady());
  }

  String? emailValidation(String email) {
    String? message = Common.emailValidation(email);

    return message;
  }
}
