package com.faforever.api.email;

import com.faforever.api.config.FafApiProperties;
import com.faforever.api.config.FafApiProperties.PasswordReset;
import com.faforever.api.config.FafApiProperties.Registration;
import com.faforever.api.error.ApiException;
import com.faforever.api.error.Error;
import com.faforever.api.error.ErrorCode;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.regex.Pattern;

@Service
@Slf4j
public class EmailService {
  private static final Pattern EMAIL_PATTERN = Pattern.compile(".+@.+\\..+$");
  private final DomainBlacklistRepository domainBlacklistRepository;
  private final FafApiProperties properties;
  private final EmailSender emailSender;

  public EmailService(DomainBlacklistRepository domainBlacklistRepository, FafApiProperties properties, EmailSender emailSender) {
    this.domainBlacklistRepository = domainBlacklistRepository;
    this.properties = properties;
    this.emailSender = emailSender;
  }

  /**
   * Checks whether the specified email address as a valid format and its domain is not blacklisted.
   */
  @Transactional(readOnly = true)
  public void validateEmailAddress(String email) {
    if (!EMAIL_PATTERN.matcher(email).matches()) {
      throw new ApiException(new Error(ErrorCode.EMAIL_INVALID, email));
    }
    if (domainBlacklistRepository.existsByDomain(email.substring(email.lastIndexOf('@') + 1))) {
      throw new ApiException(new Error(ErrorCode.EMAIL_BLACKLISTED, email));
    }
  }

  @SneakyThrows
  public void sendActivationMail(String username, String email, String activationUrl) {
    Registration registration = properties.getRegistration();
    emailSender.sendMail(
        registration.getFromEmail(),
        registration.getFromName(),
        email,
        registration.getSubject(),
        String.format(registration.getHtmlFormat(), username, activationUrl)
    );
  }

  @SneakyThrows
  public void sendPasswordResetMail(String username, String email, String passwordResetUrl) {
    PasswordReset passwordReset = properties.getPasswordReset();
    emailSender.sendMail(
        passwordReset.getFromEmail(),
        passwordReset.getFromName(),
        email,
        passwordReset.getSubject(),
        String.format(passwordReset.getHtmlFormat(), username, passwordResetUrl)
    );
  }
}

@@ -500,24 +500,56 @@
 sactional;%0A%0A
+import java.text.MessageFormat;%0A
 import java.
@@ -1825,26 +1825,24 @@
 Mail(%0A      
-  
 registration
@@ -1854,26 +1854,24 @@
 romEmail(),%0A
-  
       regist
@@ -1890,34 +1890,32 @@
 omName(),%0A      
-  
 email,%0A        r
@@ -1907,26 +1907,24 @@
 mail,%0A      
- 
- 
 registration
@@ -1940,32 +1940,37 @@
 ct(),%0A      
-  String
+MessageFormat
 .format(regi
@@ -2235,26 +2235,24 @@
 Mail(%0A      
-  
 passwordRese
@@ -2265,26 +2265,24 @@
 romEmail(),%0A
-  
       passwo
@@ -2306,26 +2306,24 @@
 me(),%0A      
-  
 email,%0A     
@@ -2319,26 +2319,24 @@
 mail,%0A      
- 
- 
 passwordRese
@@ -2361,16 +2361,21 @@
     
-  String
+MessageFormat
 .for

use MessageFormat in EmailService