package com.k_int.ciim.utils;

import java.util.*;

import javax.mail.*;
import javax.mail.internet.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MailSender 
{
	private static Log log = LogFactory.getLog(MailSender.class);
	
	String smtpServer;
	String smtpPort = null;
	String username = null;
	String password = null;
	String authRequired = null;
	
	public MailSender(String smtpServer, String smtpPort, String authRequired, String username, String password)
	{
		if(smtpServer == null)
		{
			throw new NullPointerException("smtpServer cannot be null");
		}
		if(authRequired.equalsIgnoreCase(Boolean.TRUE.toString()) && (username == null || password == null))
		{
			throw new NullPointerException("Username and Password are required for authentication");
		}
		
		this.smtpServer = smtpServer;
		this.smtpPort = smtpPort;
		this.username = username;
		this.password = password;
		this.authRequired = authRequired;
	}
	
	public void sendEmail(String fromEmailAddr, String recipientEmailAddr, String subject, String body)
	{
		Properties mailServerConfig = new Properties();
		
		mailServerConfig.put("mail.smtp.host", this.smtpServer);
		
		if(authRequired != null && authRequired.equalsIgnoreCase(Boolean.TRUE.toString()))
		{
			mailServerConfig.put("mail.smtp.port", this.smtpPort );
			mailServerConfig.put("mail.smtp.auth", this.authRequired);
			//Specifically required for gmail smtp
			mailServerConfig.put("mail.smtp.starttls.enable", "true");
			
			Session session = Session.getDefaultInstance( mailServerConfig, new javax.mail.Authenticator() {	protected PasswordAuthentication getPasswordAuthentication() {
																												return new PasswordAuthentication(username, password);}});

			sendEmail(fromEmailAddr, recipientEmailAddr, subject, body, session);
		}
		else
		{			
			//Here, no Authenticator argument is used (it is null).
		    //Authenticators are used to prompt the user for user
		    //name and password.
			Session session = Session.getDefaultInstance( mailServerConfig, null);
			
			sendEmail(fromEmailAddr, recipientEmailAddr, subject, body, session);
		}
	}
	
	private void sendEmail(String fromEmailAddr, String recipientEmailAddr, String subject, String body, Session session)
	{
		MimeMessage message = new MimeMessage( session );
	    
	    try
	    {
	    	message.setFrom(new InternetAddress(fromEmailAddr));
	    	message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipientEmailAddr));
	    	message.setSubject(subject);
	    	message.setContent(body,"text/html");
	    	Transport.send(message);
	    }
	    catch (MessagingException ex)
	    {
	    	log.error("Cannot send email. " + ex);
	    }
	}
	
	public static String getPublishRequestEmail(String editor, String group_name)
	{
		return 	"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\"http://www.w3.org/TR/html4/strict.dtd\">" 
				+ "<html>" 
				+ "	<head>"
				+ "		<title>CIIM Top Group Publication Request</title>" 
				+ "		<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">"
				+ "		<style></style>"
				+ "	</head>	"
				+ " <body>"
				+ " 	<p>" + editor + " has requested that " + group_name + " be published.</p>"
				+ " </body>"
				+ "</html>";
	}
}
