package it.enea.cruise.config;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import freemarker.cache.FileTemplateLoader;
import freemarker.cache.TemplateLoader;

@Slf4j
@Configuration	
public class JavaMailConfig {
	
	@Value("${spring.mail.host}")
	private String host;
	
	@Value("${spring.mail.port}")
	private Integer port;
	
	@Value("${spring.mail.username}")
	private String mailUsername;
	
	@Value("${spring.mail.password}")
	private String mailPassword;
	
	@Value("${spring.mail.properties.mail.smtp.auth}")
	private Boolean auth;
	
	@Value("${spring.mail.properties.mail.smtp.starttls.enable}")
	private Boolean startTLSEnable;
	
	@Value("${spring.mail.templates.path}")
	private String mailTemplatesPath;

	
	
	@Bean
	public JavaMailSender getJavaMailSender() {
	    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

		if (host == null || port == null) {
			log.error("Email bean is not configured. Please check required info on application.properties");
			return mailSender;
		}
	 	    
	    mailSender.setHost(host);
	    mailSender.setPort(port);
	    
	    mailSender.setUsername(mailUsername);
	    mailSender.setPassword(mailPassword);
	    
	    Properties props = mailSender.getJavaMailProperties();
	    props.put("mail.transport.protocol", "smtp");
	    props.put("mail.smtp.auth", auth);
	    props.put("mail.smtp.starttls.enable", startTLSEnable);
	    props.put("mail.debug", "true");
	    
	    return mailSender;
	}

	@Bean 
	public FreeMarkerConfigurer freemarkerClassLoaderConfig() throws IOException {
		File templates = ResourceUtils.getFile(mailTemplatesPath);
		freemarker.template.Configuration configuration = 
				new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_31);
	    TemplateLoader templateLoader = new FileTemplateLoader(templates);
	    configuration.setTemplateLoader(templateLoader);
	    FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
	    freeMarkerConfigurer.setConfiguration(configuration);
	    return freeMarkerConfigurer; 
	}
	
	@Bean 
	public FreeMarkerViewResolver freemarkerViewResolver() { 
	    FreeMarkerViewResolver resolver = new FreeMarkerViewResolver(); 
	    resolver.setCache(true); 
	    resolver.setPrefix(""); 
	    resolver.setSuffix(".ftl"); 
	    return resolver; 
	}
	
	@Bean
	public ResourceBundleMessageSource emailMessageSource() {
	    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
	    messageSource.setBasename("mailMessages");
	    return messageSource;
	}
	
}
