content: package com.alkl1m.taskmanager.service.user;
  
  import com.alkl1m.taskmanager.entity.User;
  import com.alkl1m.taskmanager.entity.VerificationToken;
  import com.alkl1m.taskmanager.repository.UserRepository;
  import com.alkl1m.taskmanager.repository.VerificationTokenRepository;
  import com.alkl1m.taskmanager.service.PasswordResetToken.PasswordResetTokenService;
  import lombok.RequiredArgsConstructor;
  import org.springframework.security.crypto.password.PasswordEncoder;
  import org.springframework.stereotype.Service;
  
  import java.util.Calendar;
  import java.util.Optional;
  
  @Service
  @RequiredArgsConstructor
  public class UserServiceImpl implements UserService{
      private final UserRepository userRepository;
      private final PasswordResetTokenService passwordResetTokenService;
      private final PasswordEncoder passwordEncoder;
      private final VerificationTokenRepository tokenRepository;
      @Override
      public Optional<User> findByEmail(String email){
          return userRepository.findByEmail(email);
      }
  
      @Override
      public void createPasswordResetTokenForUser(User user, String passwordToken){
          passwordResetTokenService.createPasswordResetTokenForUser(user, passwordToken);
      }
  
      @Override
      public void saveUserVerificationToken(User theUser, String token) {
          var verificationToken = new VerificationToken(token, theUser);
          tokenRepository.save(verificationToken);
      }
  
      @Override
      public boolean validatePasswordResetToken(String passwordResetToken) {
          return passwordResetTokenService.validatePasswordResetToken(passwordResetToken);
      }
  
      @Override
      public User findUserByPasswordToken(String passwordResetToken) {
          return passwordResetTokenService.findUserByPasswordToken(passwordResetToken).get();
      }
  
  
      @Override
      public boolean validateToken(String theToken) {
          VerificationToken token = tokenRepository.findByToken(theToken);
          if(token == null){
              return false;
          }
          User user = token.getUser();
          Calendar calendar = Calendar.getInstance();
          if ((token.getExpirationTime().getTime()-calendar.getTime().getTime())<= 0){
              return false;
          }
          user.setEnabled(true);
          userRepository.save(user);
          return true;
      }
  
      @Override
      public void changePassword(User theUser, String newPassword) {
          theUser.setPassword(passwordEncoder.encode(newPassword));
          userRepository.save(theUser);
      }
  
      @Override
      public boolean oldPasswordIsValid(User user, String oldPassword){
          return passwordEncoder.matches(oldPassword, user.getPassword());
      }
  }
  