package ink.wgink.module.sms.builder.email;

import ink.wgink.module.sms.pojo.MailAuthenticator;
import ink.wgink.module.sms.pojo.MailSenderInfo;
import ink.wgink.properties.sms.EmailProperties;
import org.apache.commons.lang3.StringUtils;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: EmailBuilder
 * @Description: 邮件构建
 * @Author: wanggeng
 * @Date: 2021/5/1 5:35 下午
 * @Version: 1.0
 */
public class EmailSendBuilder {

    /**
     * 邮件发送
     *
     * @param emailProperties
     * @return
     */
    public static EmailSend getEmailSend(EmailProperties emailProperties, String toEmail) {
        try {
            return new EmailSend(getMessage(getMailSenderInfo(emailProperties, toEmail)));
        } catch (MessagingException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 邮件发送者信息
     *
     * @return
     */
    private static MailSenderInfo getMailSenderInfo(EmailProperties emailProperties, String toEmail) {
        MailSenderInfo mailInfo = new MailSenderInfo();
        mailInfo.setMailServerHost(emailProperties.getSmtp());
        mailInfo.setMailServerPort(emailProperties.getPort());
        mailInfo.setValidate(true);
        mailInfo.setUserName(emailProperties.getSenderEmail());
        mailInfo.setPassword(emailProperties.getSenderPassword());
        mailInfo.setFromAddress(emailProperties.getSenderEmail());
        mailInfo.setToAddress(toEmail);
        StringBuilder ccSB = new StringBuilder();
        emailProperties.getCcPersons().forEach(cc -> {
            if (ccSB.length() > 0) {
                ccSB.append(",");
            }
            ccSB.append(cc);
        });
        mailInfo.setToCc(ccSB.toString());
        return mailInfo;
    }

    /**
     * 邮件消息
     *
     * @param mailInfo
     * @return
     * @throws MessagingException
     */
    private static Message getMessage(MailSenderInfo mailInfo) throws MessagingException {
        // 判断是否需要身份认证
        MailAuthenticator authenticator = null;
        Properties pro = mailInfo.getProperties();
        if (mailInfo.isValidate()) {
            // 如果需要身份认证，则创建一个密码验证器
            authenticator = new MailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
        }
        // 根据邮件会话属性和密码验证器构造一个发送邮件的session
        Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
        // 根据session创建一个邮件消息
        Message mailMessage = new MimeMessage(sendMailSession);
        // 创建邮件发送者地址
        Address from = new InternetAddress(mailInfo.getFromAddress());
        // 设置邮件消息的发送者
        mailMessage.setFrom(from);
        // 创建邮件的接收者地址，并设置到邮件消息中
        InternetAddress[] to = InternetAddress.parse(mailInfo.getToAddress());
        mailMessage.setRecipients(Message.RecipientType.TO, to);
        if (!StringUtils.isBlank(mailInfo.getToCc())) {
            InternetAddress[] cc = InternetAddress.parse(mailInfo.getToCc());
            mailMessage.setRecipients(Message.RecipientType.CC, cc);
        }
        // 设置邮件消息发送的时间
        mailMessage.setSentDate(new Date());
        return mailMessage;
    }

}
