package com.yonge.toolset.thirdparty.message.provider;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import com.yonge.toolset.thirdparty.message.MessageSenderPlugin;

@Service
public class CommEmailPlugin implements MessageSenderPlugin, InitializingBean {

	private final static Logger log = LoggerFactory.getLogger(CommEmailPlugin.class);

	private String hostName;

	private int smtpPort;

	private String userName;

	private String password;

	private String from;

	private String fromName;

	public void setHostName(String hostName) {
		this.hostName = hostName;
	}

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

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public void setFromName(String fromName) {
		this.fromName = fromName;
	}

	public static String getName() {
		return "email";
	}

	@Override
	public void afterPropertiesSet() throws Exception {

	}

	private boolean checkProperties() {
		if (StringUtils.isBlank(hostName)) {
			throw new RuntimeException("SMTP服务器没有配置");
		}

		if (smtpPort == 0) {
			throw new RuntimeException("SMTP服务器端口没有配置");
		}

		if (StringUtils.isBlank(userName)) {
			throw new RuntimeException("SMTP服务器认证用户名没有配置");
		}

		if (StringUtils.isBlank(password)) {
			throw new RuntimeException("SMTP服务器认证密码没有配置");
		}

		if (StringUtils.isBlank(from)) {
			throw new RuntimeException("发件人邮箱没有配置");
		}

		// 落款名字
		if (StringUtils.isBlank(fromName)) {
			fromName = from;
		}
		return true;
	}

	public static void main(String[] args) throws Exception {
		CommEmailPlugin cep = new CommEmailPlugin();
		cep.setUserName("lexiaoyavip@163.com");
		cep.setHostName("smtp.163.com");
		cep.setSmtpPort(465);
		cep.setPassword("HMBJPGYXFOLEBGIP");
		cep.setFrom("lexiaoyavip@163.com");
		cep.setFromName("乐小雅");
		cep.send("测试邮件",
				 "<b style='color:red'>测试邮件内容</b>",
				 "yanite1234@sina.com",
				 null, null, null, null);
	}

	@Override
	public boolean send(String subject, String content, String receiver, String url, String jpushType, String sound, String channelId) throws Exception {
		if (hostName == null || hostName.equals("")) {
			log.error("邮件配置未能加载");
			return false;
		}
		this.checkProperties();
		Email email = new SimpleEmail();
		email.setHostName(hostName);
		email.setDebug(true);
		email.setSslSmtpPort(smtpPort + "");
		email.setAuthenticator(new DefaultAuthenticator(userName, password));
		email.setSSLOnConnect(true);
		try {
			email.setFrom(from, fromName);
			email.setSubject(subject);
			email.setContent(content, "text/html;charset=utf8");
			email.addTo(receiver);
			email.send();
			return true;
		} catch (EmailException e) {
			throw new RuntimeException("发送邮件出现异常", e);
		}
	}

	@Override
	public boolean batchSend(String subject, String content, String[] receivers, String url, String jpushType, String sound, String channelId) throws Exception {
		for (String rev : receivers) {
			try {
				send(subject, content, rev, url, jpushType, sound, channelId);
			} catch(EmailException e) {
				throw new RuntimeException("发送邮件出现异常", e);
			}
		}
		return true;
	}

}
