package com.ichangzuo.module.mail.service.impl;

import com.ichangzuo.config.property.MailProperties;
import com.ichangzuo.module.mail.service.MailService;
import groovy.lang.GString;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Properties;

/**
 * 邮件服务实现类
 *
 * @author Ray
 * @since 2024/8/17
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class MailServiceImpl implements MailService {
    private final JavaMailSender mailSender;
    private final MailProperties mailProperties;

    /**
     * 发送简单文本邮件
     *
     * @param to      收件人地址
     * @param subject 邮件主题
     * @param text    邮件内容
     */
    @Override
    public boolean sendMail(String to, String subject, String text) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(mailProperties.getFrom());
            message.setTo(to);
            message.setSubject(subject);
            message.setText(text);
//

            // 直接创建 JavaMailSenderImpl 实现类
//            JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
//
//            mailSender.setDefaultEncoding("utf-8");
//
//            mailSender.setHost("smtp.qq.com");              // 设置邮箱服务器
//            mailSender.setPort(465);                        // 设置端口
//            mailSender.setUsername("747692844@qq.com");    // 设置用户名
//            mailSender.setPassword("<你的密码/授权码>");      // 设置密码（记得替换为你实际的密码、授权码）
//            mailSender.setProtocol("smtps");                // 设置协议
//
//            Properties properties = new Properties();           // 配置项
//            properties.put("mail.smtp.connectiontimeout", 5000);
//            properties.put("mail.smtp.timeout", 3000);
//            properties.put("mail.smtp.writetimeout", "5000");
//            properties.put("mail.smtp.auth", true);
//            properties.put("mail.smtp.starttls.enable", true);
//            properties.put("mail.smtp.starttls.required", true);

//            mailSender.setJavaMailProperties(properties); // 设置配置项
//
//            // 创建一个邮件消息
//            MimeMessage message = mailSender.createMimeMessage();
//
//            // 创建 MimeMessageHelper
//            MimeMessageHelper helper = new MimeMessageHelper(message, false);
//
//            // 发件人邮箱和名称
//            helper.setFrom(mailProperties.getFrom());
//            // 收件人邮箱
//            helper.setTo(to);
//            // 邮件标题
//            helper.setSubject(subject);
//            // 邮件正文，第二个参数表示是否是HTML正文
//            helper.setText(text, true);
//
            mailSender.send(message);

        } catch (MailException e) {
            log.error("发送邮件失败{}", e.getMessage());
            return false;
        }

        return true;
    }

    @Override
    public boolean sendHtmlMail(String to, String subject, String text) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, "UTF-8");
            helper.setFrom(mailProperties.getFrom());
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text, true);  // true表示支持HTML内容

            mailSender.send(message);
        } catch (MessagingException e) {
            log.error("发送邮件失败{}", e.getMessage());

            return false;
        }

        return true;
    }

    /**
     * 发送带附件的邮件
     *
     * @param to       收件人地址
     * @param subject  邮件主题
     * @param text     邮件内容
     * @param filePath 附件路径
     */
    @Override
    public boolean sendMailWithAttachment(String to, String subject, String text, String filePath) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(mailProperties.getFrom());
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text, true);  // true表示支持HTML内容

            FileSystemResource file = new FileSystemResource(new File(filePath));
            helper.addAttachment(file.getFilename(), file);

            mailSender.send(message);
        } catch (MessagingException e) {
            log.error("发送邮件失败{}", e.getMessage());

            return false;
        }

        return true;
    }
}
