package cn.bunny.module.mail.utils;

import cn.bunny.entity.system.email.EmailSend;
import cn.bunny.entity.system.email.EmailSendInit;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;

public class MailSenderUtil {
    private final String username;
    private final JavaMailSenderImpl javaMailSender;

    /**
     * 初始化构造函数进行当前类赋值
     */
    public MailSenderUtil(EmailSendInit emailSendInit) {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost(emailSendInit.getHost());
        javaMailSender.setPort(emailSendInit.getPort());
        javaMailSender.setUsername(emailSendInit.getUsername());
        javaMailSender.setPassword(emailSendInit.getPassword());
        javaMailSender.setProtocol("smtps");
        javaMailSender.setDefaultEncoding("UTF-8");

        this.username = emailSendInit.getUsername();
        this.javaMailSender = javaMailSender;
    }

    /**
     * 综合邮箱发送
     *
     * @param emailSend 邮件消息
     */
    public void sendEmail(EmailSend emailSend) throws MessagingException {
        MailSendCheckUtil.check(emailSend);

        // 创建 MimeMessage 对象用于发送邮件富文本或者附件
        MimeMessage message = javaMailSender.createMimeMessage();
        // 创建 MimeMessageHelper
        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        // 设置发送人
        helper.setFrom(username);
        // 设置邮件接受者
        helper.setTo(emailSend.getSendTo());
        // 设置邮件主题
        helper.setSubject(emailSend.getSubject());
        // 设置发送消息 为富文本
        helper.setText(emailSend.getMessage(), emailSend.getIsRichText());
        // 设置抄送人
        helper.setCc(emailSend.getCcParam().split(","));
        // 邮件添加附件
        MultipartFile[] files = emailSend.getFile();
        for (MultipartFile file : files) {
            helper.addAttachment(Objects.requireNonNull(file.getOriginalFilename()), file);
        }

        // 发送邮件
        javaMailSender.send(message);
    }

    /**
     * 发送邮件-简单
     *
     * @param emailSend 邮件消息
     */
    public void sendSimpleEmail(EmailSend emailSend) {
        MailSendCheckUtil.check(emailSend);
        // 创建邮件消息体 SimpleMailMessage 发送简单邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();

        // 设置邮件发送人
        mailMessage.setFrom(username);
        // 设置邮件接受者
        mailMessage.setTo(emailSend.getSendTo());
        // 设置邮件主题
        mailMessage.setSubject(emailSend.getSubject());
        // 设置邮件消息
        mailMessage.setText(emailSend.getMessage());

        javaMailSender.send(mailMessage);
    }

    /**
     * 发送带附件邮件
     *
     * @param emailSend 邮件消息
     */
    public void sendAttachmentEmail(EmailSend emailSend, MultipartFile file, boolean isRich) throws MessagingException {
        MailSendCheckUtil.check(emailSend);
        // 创建 MimeMessage 对象用户发送附件或者是富文本内容
        MimeMessage mailMessage = javaMailSender.createMimeMessage();
        // 创建 MimeMessageHelper
        MimeMessageHelper helper = new MimeMessageHelper(mailMessage, true);

        // 奢姿邮件发送人
        helper.setFrom(username);
        // 设置邮件接受者
        helper.setTo(emailSend.getSendTo());
        // 设置邮件消息
        helper.setText(emailSend.getMessage(), isRich);
        // 设置邮件主题
        helper.setSubject(emailSend.getSubject());
        // 邮件添加附件
        helper.addAttachment(Objects.requireNonNull(file.getOriginalFilename()), file);

        // 发送邮件
        javaMailSender.send(mailMessage);
    }

    /**
     * 发送富文本邮件
     *
     * @param emailSend 邮件消息
     */
    public void sendRichText(EmailSend emailSend, boolean isRich) throws MessagingException {
        MailSendCheckUtil.check(emailSend);
        // 创建 MimeMessage 对象用户发送附件或者是富文本内容
        MimeMessage mailMessage = javaMailSender.createMimeMessage();
        // 创建 MimeMessageHelper
        MimeMessageHelper helper = new MimeMessageHelper(mailMessage, true);

        // 设置邮件发送者
        helper.setFrom(username);
        // 设置邮件接受者
        helper.setTo(emailSend.getSendTo());
        // 设置邮件主题
        helper.setSubject(emailSend.getSubject());
        // 设置邮件富文本，后面跟true 表示HTML格式发送
        helper.setText(emailSend.getMessage(), isRich);

        // 发送邮件
        javaMailSender.send(mailMessage);
    }

    /**
     * 发送带抄送的邮件
     *
     * @param emailSend 邮件消息
     */
    public void sendCC(EmailSend emailSend, boolean isRich) throws MessagingException {
        MailSendCheckUtil.check(emailSend);
        // 创建 MimeMessage 对象用于发送邮件富文本或者附件
        MimeMessage message = javaMailSender.createMimeMessage();
        // 创建 MimeMessageHelper
        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        // 设置发送人
        helper.setFrom(username);
        // 设置邮件接受者
        helper.setTo(emailSend.getSendTo());
        // 设置邮件主题
        helper.setSubject(emailSend.getSubject());
        // 设置发送消息 为富文本
        helper.setText(emailSend.getMessage(), isRich);
        // 设置抄送人
        helper.setCc(emailSend.getCcParam().split(","));

        // 发送邮件
        javaMailSender.send(message);
    }
}
