package org.egl_cepgl.pm.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.util.Properties;

@Configuration
public class EmailSenderConfiguration
{
    @Value("${spring.mail.host}")
    private String emailhost;
    @Value("${spring.mail.port}")
    private Integer emailport;
    @Value("${spring.mail.username}")
    private String emailusername;
    @Value("${spring.mail.password}")
    private String emailpassword;

    @Bean
    public JavaMailSender getJavaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(emailhost);
        mailSender.setPort(emailport);

        mailSender.setUsername(emailusername);
        mailSender.setPassword(emailpassword);

        Properties props = mailSender.getJavaMailProperties();
        props.put("mail.smtp.host", emailhost);
        props.put("mail.smtp.port", emailport);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.ssl.enable", "true");
        return mailSender;
    }

    @Primary
    @Bean
    public FreeMarkerConfigurationFactoryBean factoryBean() {
        FreeMarkerConfigurationFactoryBean bean=new FreeMarkerConfigurationFactoryBean();
        bean.setTemplateLoaderPath("classpath:/templates/fm/");
        return bean;
    }
}