package control;


import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sun.mail.smtp.SMTPSSLTransport;

import boundary.DatabaseCommunicator;



/**
* A general email utility to send basic email messages.
* 
*/
public class Mailer {

	private final static Mailer instance = new Mailer();

	private Mailer() {
	}

	public final static Mailer getInstance() {
		return instance;
	}
	
	private final static MailHeaderParameters defaultHeader = new MailHeaderParameters();
	
	static{
		defaultHeader.setFromAddress("admin@ece567.com");
		defaultHeader.setSmtpHostName("HostName");
	}

	/**
	 * Send an email using the parameters provided.
	 * 
	 * @param header Contains the email information such as email server host name, from address, etc.
	 * @param message The message to send
	 * @throws MailerException If anything goes wrong
	 */
	public void sendMail(MailHeaderParameters header, String message, boolean isSSL) throws Exception {

		try {

			/**Set the host smtp address
			 */
			Properties props = new Properties();
			props.put("mail.smtp.host", header.getSmtpHostName());
			
			/**Get the default session
			 */
			Session session = Session.getDefaultInstance(props, null);
			session.setDebug(true);

			/**Create a message
			 */
			Message mailMessage = new MimeMessage(session);

			/**Set the from address
			 */
			InternetAddress addressFrom = new InternetAddress(header.getFromAddress());
			mailMessage.setFrom(addressFrom);

			/**Set the to-addresses
			 */
			if (header.getToAddresses() != null) {
				for (int t = 0; t < header.getToAddresses().length; t++) {
					InternetAddress addressTo = new InternetAddress(header.getToAddresses()[t]);
					mailMessage.addRecipient(Message.RecipientType.TO, addressTo);
				}
			}
			/**Set the message subject
			 */
			mailMessage.setSubject(header.getSubject());

			/**Set the message content type
			 */
			mailMessage.setContent(mailMessage, header.getMimeType());

			/**Set the message text
			 */
			mailMessage.setText(message);

			
			/**Send the message
			 */
			Transport.send(mailMessage);

		} catch (Throwable t) {
			
			String msg = "Problem sending mail message: " + t.getMessage();
			t.printStackTrace();
			throw new Exception( msg, t);
		
		}
	}

	/**
	 * Send an email using the parameters provided.
	 * 
	 * @param header Contains the email information such as email server host name, from address, etc.
	 * @param message The message to send
	 * @throws MailerException If anything goes wrong
	 */
	public void sendMail1(MailHeaderParameters header, String message, boolean isSSL) throws Exception {

		try {

			/**Set the host smtp address
			 */
			Properties props = new Properties();
			props.put("mail.smtp.host", header.getSmtpHostName());
			props.put("mail.smtp.starttls.enable", "true");
			props.put("mail.smtp.auth", "true");
	
			MailAuthenticator auth = new MailAuthenticator("srinathreddy035", "arunavenkat");
			
			/**Get the default session
			 */
			Session session = Session.getInstance(props, auth);
			session.setDebug(true);

			/**Create a message
			 */
			Message mailMessage = new MimeMessage(session);

			/**Set the from address
			 */
			InternetAddress addressFrom = new InternetAddress(header.getFromAddress());
			mailMessage.setFrom(addressFrom);

			/**Set the to-addresses
			 */
			if (header.getToAddresses() != null) {
				for (int t = 0; t < header.getToAddresses().length; t++) {
					InternetAddress addressTo = new InternetAddress(header.getToAddresses()[t]);
					mailMessage.addRecipient(Message.RecipientType.TO, addressTo);
				}
			}
			/**Set the message subject
			 */
			mailMessage.setSubject(header.getSubject());

			/**Set the message content type
			 */
			mailMessage.setContent(mailMessage, header.getMimeType());

			/**Set the message text
			 */
			mailMessage.setText(message);

			
			/**Send the message
			 */
			Transport.send(mailMessage);

		} catch (Throwable t) {
			
			String msg = "Problem sending mail message: " + t.getMessage();
			t.printStackTrace();
			throw new Exception( msg, t);
		
		}
	}

	
	public void sendMail(String[] toAddresses, String message) throws Exception {
		defaultHeader.setToAddresses(toAddresses);
		sendMail(defaultHeader,message, false);		
	}
	
	public static void main(String args[]){		
		//String message = DatabaseCommunicator.getPortfolios();
		//String[] toEmails = DatabaseCommunicator.getEmailAddresses();
		//try {
			//Mailer.getInstance().sendMail(toEmails,message);
		//} catch (Exception e) {			
		//	e.printStackTrace();
		//	System.out.println("Couldn't send email."+e);
		//}
	}
}
