package eu.eudat.logic.services.utilities;

import eu.eudat.data.dao.entities.LoginConfirmationEmailDao;
import eu.eudat.data.entities.LoginConfirmationEmail;
import eu.eudat.models.data.mail.SimpleMail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

@Service("ConfirmationEmailService")
public class ConfirmationEmailServiceImpl implements ConfirmationEmailService {
	private static final Logger logger = LoggerFactory.getLogger(ConfirmationEmailServiceImpl.class);
	//private Logger logger;
	private Environment environment;

	public ConfirmationEmailServiceImpl(/*Logger logger,*/ Environment environment) {
//		this.logger = logger;
		this.environment = environment;
	}

	@Override
	public void createConfirmationEmail(LoginConfirmationEmailDao loginConfirmationEmailDao, MailService mailService, String email, UUID userId) {
		LoginConfirmationEmail confirmationEmail = new LoginConfirmationEmail();
		confirmationEmail.setEmail(email);
		confirmationEmail.setExpiresAt(Date
				.from(new Date()
						.toInstant()
						.plusSeconds(Long.parseLong(this.environment.getProperty("conf_email.expiration_time_seconds")))
				)
		);
		confirmationEmail.setUserId(userId);
		confirmationEmail.setIsConfirmed(false);
		confirmationEmail.setToken(UUID.randomUUID());
		confirmationEmail = loginConfirmationEmailDao.createOrUpdate(confirmationEmail);
		sentConfirmationEmail(confirmationEmail, mailService);
	}

	@Override
	public CompletableFuture sentConfirmationEmail(LoginConfirmationEmail confirmationEmail, MailService mailService) {
		return CompletableFuture.runAsync(() -> {
			SimpleMail mail = new SimpleMail();
			mail.setSubject(environment.getProperty("conf_email.subject"));
			mail.setContent(createContent(confirmationEmail.getToken(), mailService));
			mail.setTo(confirmationEmail.getEmail());
			try {
				mailService.sendSimpleMail(mail);
			} catch (Exception ex) {
				logger.error(ex.getMessage(), ex);
			}
		});
	}

	private String createContent(UUID confirmationToken, MailService mailService) {
		String content = mailService.getMailTemplateContent("classpath:templates/email/emailConfirmation.html");
		content = content.replace("{confirmationToken}", confirmationToken.toString());
		content = content.replace("{expiration_time}", secondsToTime(Integer.parseInt(this.environment.getProperty("conf_email.expiration_time_seconds"))));
		content = content.replace("{host}", this.environment.getProperty("dmp.domain"));

		return content;
	}

	private String secondsToTime(int seconds) {
		int sec = seconds % 60;
		int hour = seconds / 60;
		int min = hour % 60;
		hour = hour / 60;
		return (hour + ":" + min + ":" + sec);
	}
}