package org.yacos.web.system.controller;

import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

public class MailSenderService {
	public static MailSenderService instance = new MailSenderService();
	private MailSender mailSender;
	private String fromEmail;
	private String replyTo;
	
	public MailSenderService(){
		
	}
	
	public static MailSenderService getInstance(){
		if(instance == null){
			instance = new MailSenderService();
		}
		return instance;
	}
	
	public void sendEMail(String toEmail, String subject, String body){
		SimpleMailMessage message = composeEmail(toEmail, fromEmail, subject, body, replyTo, null, null);
		mailSender.send(message);
	}
	
	public SimpleMailMessage composeEmail(String toEmail, String fromEmail, String subject, String body){
		return composeEmail(toEmail, fromEmail, subject, body, fromEmail, null, null);
	}
	
	public SimpleMailMessage composeEmail(String toEmail, String fromEmail, String subject, String body, String replyTo, String cc, String bcc){
		SimpleMailMessage mailMessage = new SimpleMailMessage();
		
		mailMessage.setTo(toEmail);
		mailMessage.setFrom(fromEmail);
		mailMessage.setSubject(subject);
		mailMessage.setText(body);
		mailMessage.setReplyTo(replyTo);
		if(cc!=null){
			mailMessage.setCc(cc);
		}
		if(bcc != null){
			mailMessage.setBcc(bcc);
		}
		
		return mailMessage;
	}
	
	public String getFromEmail() {
		return fromEmail;
	}
	
	public void setFromEmail(String fromEmail) {
		this.fromEmail = fromEmail;
	}
	
	public String getReplyTo() {
		return replyTo;
	}
	
	public void setReplyTo(String replyTo) {
		this.replyTo = replyTo;
	}
	
	public void setMailSender(MailSender mailSender) {
		this.mailSender = mailSender; 
	}
	
	public MailSender getMailSender() {
		return mailSender;
	}
}
