package com.cim.idm.wmsextend.generic.schedule;

import com.cim.idm.framework.util.time.TimeStampUtil;
import com.cim.idm.wmsextend.generic.errorHandler.CustomException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.activation.CommandMap;
import javax.activation.MailcapCommandMap;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class EMailInterface
{
	private static Log log = LogFactory.getLog(EMailInterface.class);
	
	private String	smtpHostName		= "localhost";
	private String	smtpPort			= "25";
	private String	connectionTimeout	= "1000";
	private String	socketIOTimeout		= "1000";

	public String getSmtpHostName() 
	{
		return smtpHostName;
	}

	public void setSmtpHostName(String smtpHostName) 
	{
		this.smtpHostName = smtpHostName;
	}

	public String getSmtpPort() 
	{
		return smtpPort;
	}

	public void setSmtpPort(String smtpPort) 
	{
		this.smtpPort = smtpPort;
	}

	public String getConnectionTimeout() 
	{
		return connectionTimeout;
	}

	public void setConnectionTimeout(String connectionTimeout) 
	{
		this.connectionTimeout = connectionTimeout;
	}

	public String getSocketIOTimeout() 
	{
		return socketIOTimeout;
	}

	public void setSocketIOTimeout(String socketIOTimeout) 
	{
		this.socketIOTimeout = socketIOTimeout;
	}

	public EMailInterface()
	{
		MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
		mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
		mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
		mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
		mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
		mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
		CommandMap.setDefaultCommandMap(mc);
	}

	public EMailInterface(String smtpHostName, String smtpPort, String connectionTimeout, String socketIOTimeout)
	{
		MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
		mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
		mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
		mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
		mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
		mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
		CommandMap.setDefaultCommandMap(mc);

		this.setSmtpHostName(smtpHostName);
		this.setSmtpPort(smtpPort);
		this.setConnectionTimeout(connectionTimeout);
		this.setSocketIOTimeout(socketIOTimeout);
	}

	public void postMail(
			String smtpHostName, String smtpPort, String connectionTimeout, String socketIOTimeout, 
			List<String> recipients, String subject, String message, String fromUserName, String fromUserMail, String authUserName, String authPwd) throws CustomException
	{
		this.setSmtpHostName(smtpHostName);
		this.setSmtpPort(smtpPort);
		this.setConnectionTimeout(connectionTimeout);
		this.setSocketIOTimeout(socketIOTimeout);
		

		postMail(recipients, subject, message, fromUserName, fromUserMail, authUserName, authPwd);
	}
	
    public void postMail(List<String> recipients, String subject, String message, String fromUserName, String fromUserMail, String authUserName, String authPwd) throws CustomException
    {
    	List<String> toUserMailList = new ArrayList<String>();
		
    	if(recipients == null || recipients.size() <= 0)
    	{
    		throw new CustomException("MAIL-0004");
    	}
    	else
    	{
    		for(String toUserMail : recipients)
    		{
    			if(toUserMail.isEmpty())
    			{
    				log.info("to usermail is null");
    			}
    			else
    			{
    				toUserMailList.add(toUserMail);
    			}
    		}
    		
    		if(toUserMailList == null || toUserMailList.size() <= 0)
        	{
        		throw new CustomException("MAIL-0004");
        	}
    	}
    	if(fromUserMail == null || fromUserMail.isEmpty())
    	{
    		throw new CustomException("MAIL-0005");
    	}
    	if(authUserName == null || authUserName.isEmpty())
    	{
    		throw new CustomException("MAIL-0006");
    	}
//    	if(authPwd == null || authPwd.isEmpty())
//    	{
//    		throw new CustomException("MAIL-0007");
//    	}
    	
        String charSet = "UTF-8";
    	
    	log.info("SMTP Settings");
//		Properties props = System.getProperties();
//		props.remove("mail.transport.protocol");
//		props.remove("mail.smtp.host");
//		props.remove("mail.smtp.port");
//		props.remove("mail.smtp.auth");
//		props.remove("mail.smtp.connectiontimeout");
//		props.remove("mail.smtp.timeout");
//		props.remove("mail.smtp.starttls.enable");
//		props.remove("mail.smtp.ssl.enable");
//		props.remove("mail.smtp.ssl.trust");
        
    	try
    	{
	    	Properties props = new Properties();
//			props.put("mail.transport.protocol", "smtp");
	        props.put("mail.smtp.host", this.getSmtpHostName());
	        props.put("mail.smtp.port", this.getSmtpPort());        
//			props.put("mail.smtp.auth", "false");
	        if(this.getConnectionTimeout().isEmpty() == false)
	        {
	        	props.put("mail.smtp.connectiontimeout", this.getConnectionTimeout());
	        }
	        if(this.getSocketIOTimeout().isEmpty() == false)
	        {
	        	props.put("mail.smtp.timeout", this.getSocketIOTimeout());
	        }
//			props.put("mail.smtp.starttls.enable", "true");
//			props.put("mail.smtp.ssl.enable", "true");
//			props.put("mail.smtp.ssl.trust", this.smtpHostName);
	        
	        log.info("Create Session, MimeMessage");
	        Session sess = Session.getInstance(props);
	        MimeMessage msg = new MimeMessage(sess);
	
	        log.info("Sent Mail Time");
	        msg.setSentDate(TimeStampUtil.getCurrentTimestamp());
	
	        log.info("set Sender"); 
	        InternetAddress fromUserInfo = new InternetAddress();
	        fromUserInfo.setAddress(fromUserMail);
	        if(fromUserName.isEmpty() == false)
	        {
	        	fromUserInfo.setPersonal(fromUserName);
	        }
			msg.setFrom(fromUserInfo);
	        
	        log.info("set Receiver List");
	        InternetAddress[] addressTo = new InternetAddress[toUserMailList.size()];
			for (int i = 0; i < toUserMailList.size(); i++)
			{
				addressTo[i] = new InternetAddress(toUserMailList.get(i).toString());
			}
			msg.setRecipients(Message.RecipientType.TO, addressTo);
	        log.info("set Title");
	        msg.setSubject(subject, charSet);
	
	        log.info("set Contents");
	        msg.setText(message, charSet);
	
	        log.info("set Header");
	        msg.setHeader("Content-Type", "text/html");
	        msg.saveChanges();
	        
	        log.info("connecting...");
	        Transport tr = sess.getTransport("smtp");
	        try
	        {
	        	tr.connect(smtpHostName, authUserName, authPwd);
	        }
	        catch (Exception ex)
	        {
	        	tr.close();
	
	        	if(ex.getCause() == null || ex.getMessage() == null)
	        	{
	        		throw new CustomException("MAIL-0001");
	        	}
	        	else
	        	{
	        		throw new CustomException("MAIL-0003", ex.getMessage());
	        	}
	        }
	
			log.info("sending...");
			try
			{
				tr.sendMessage(msg, msg.getAllRecipients());
			}
			catch (Exception ex)
			{
	        	tr.close();
	        	
	        	if(ex.getCause() == null || ex.getMessage() == null)
	        	{
	        		throw new CustomException("MAIL-0002", ex.getMessage());
	        	}
	        	else
	        	{
	        		throw new CustomException("MAIL-0003", ex.getMessage());
	        	}
			}
			
        	tr.close();
    		
    		log.info("To Mail : " + toUserMailList.toString() + "...");
    		log.info("From Mail : " + fromUserMail);
    		log.info("Subject : " + subject);
    		log.info("[Completed]");
    	}
    	catch (CustomException cex)
    	{
    		throw cex;
    	}
    	catch (Exception ex)
    	{
    		throw new CustomException("MAIL-0003", ex.getMessage());
    	}
    }
}