package com.njzscloud.common.email.util;

import cn.hutool.extra.spring.SpringUtil;
import com.njzscloud.common.core.ex.Exceptions;
import com.njzscloud.common.core.tuple.Tuple2;
import com.njzscloud.common.email.MailMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.MailAuthenticationException;
import org.springframework.mail.MailException;
import org.springframework.mail.MailParseException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.util.ByteArrayDataSource;
import java.util.Collections;
import java.util.List;

/**
 * 电子邮件工具
 */
@Slf4j
public class EMailUtil {
    private static final String FROM;
    private static final JavaMailSender MAIL_SENDER;

    static {
        MAIL_SENDER = SpringUtil.getBean(JavaMailSender.class);
        FROM = SpringUtil.getProperty("spring.mail.username");
    }

    /**
     * 发送简单文本邮件
     *
     * @param to      接收者邮件
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    public void sendSimpleMail(String to, String subject, String content) {
        sendSimpleMail(Collections.singletonList(to), subject, content);
    }

    /**
     * 发送简单文本邮件
     *
     * @param tos     接收者邮件
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    public void sendSimpleMail(List<String> tos, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(FROM);
        message.setTo(tos.toArray(new String[0]));
        message.setSubject(subject);
        message.setText(content);
        try {
            MAIL_SENDER.send(message);
        } catch (MailException e) {
            throw Exceptions.error(e,
                    e instanceof MailParseException ? "邮件消息解析失败" :
                            e instanceof MailAuthenticationException ? "邮件服务器认证失败" : "邮件发送失败", e);
        }
    }

    /**
     * 发送复杂邮件
     *
     * @param mailMessage 消息
     */
    public void sendComplexMail(MailMessage mailMessage) {
        MimeMessage message = MAIL_SENDER.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(FROM);
            helper.setTo(mailMessage.getTos().toArray(new String[0]));
            helper.setSubject(mailMessage.getSubject());
            helper.setText(mailMessage.getContent(), mailMessage.isHtml());
            List<Tuple2<String, ByteArrayDataSource>> attachmentList = mailMessage.getAttachmentList();
            if (attachmentList != null && !attachmentList.isEmpty()) {
                for (Tuple2<String, ByteArrayDataSource> dataSource : attachmentList) {
                    helper.addAttachment(dataSource.get_0(), dataSource.get_1());
                }
            }
        } catch (MessagingException e) {
            throw Exceptions.error(e, "邮件创建失败");
        }

        try {
            MAIL_SENDER.send(message);
        } catch (MailException e) {
            throw Exceptions.error(e,
                    e instanceof MailParseException ? "邮件消息解析失败" :
                            e instanceof MailAuthenticationException ? "邮件服务器认证失败" : "邮件发送失败");
        }
    }
}
