package com.ema.common.tools.email.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.Mail;
import cn.hutool.extra.mail.MailAccount;
import com.ema.common.tools.email.EmailConfig;
import com.ema.common.tools.email.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.File;
import java.util.List;

/**
 * 邮件发送服务
 *
 * @author LIJIAN
 * @since 2026-03-24
 */
@Slf4j
@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private EmailConfig emailConfig;

    private MailAccount mailAccount;

    /**
     * 初始化邮件账号配置
     */
    @PostConstruct
    public void init() {
        mailAccount = new MailAccount();
        mailAccount.setHost(emailConfig.getHost());
        mailAccount.setPort(emailConfig.getPort());
        mailAccount.setUser(emailConfig.getUser());
        mailAccount.setPass(emailConfig.getPass());
        mailAccount.setFrom(emailConfig.getFrom());
        mailAccount.setAuth(true);
        mailAccount.setSslEnable(true);
        log.info("邮件服务初始化成功，发件人：{}", emailConfig.getFrom());
    }


    @Override
    public boolean sendText(String to, String subject, String content) {
        return sendText(to, null, subject, content);
    }


    @Override
    public boolean sendText(String to, String cc, String subject, String content) {
        try {
            Mail mail = Mail.create(mailAccount)
                    .setTos(to)
                    .setTitle(subject)
                    .setContent(content, false);
            if (StrUtil.isNotBlank(cc)) {
                mail.setCcs(cc.split(","));
            }
            mail.send();
            log.info("文本邮件发送成功，收件人：{}，主题：{}", to, subject);
            return true;
        } catch (Exception e) {
            log.error("文本邮件发送失败，收件人：{}，主题：{}，错误：{}", to, subject, e.getMessage(), e);
            return false;
        }
    }


    @Override
    public boolean sendHtml(String to, String subject, String content) {
        return sendHtml(to, null, subject, content);
    }

    @Override
    public boolean sendHtml(String to, String cc, String subject, String content) {
        try {
            Mail mail = Mail.create(mailAccount)
                    .setTos(to)
                    .setTitle(subject)
                    .setContent(content, true);
            if (StrUtil.isNotBlank(cc)) {
                mail.setCcs(cc.split(","));
            }
            mail.send();
            log.info("HTML邮件发送成功，收件人：{}，主题：{}", to, subject);
            return true;
        } catch (Exception e) {
            log.error("HTML邮件发送失败，收件人：{}，主题：{}，错误：{}", to, subject, e.getMessage(), e);
            return false;
        }
    }

    @Override
    public boolean sendWithFiles(String to, String subject, String content, List<String> files) {
        try {
            Mail mail = Mail.create(mailAccount)
                    .setTos(to)
                    .setTitle(subject)
                    .setContent(content, false);
            
            if (CollUtil.isNotEmpty(files)) {
                for (String file : files) {
                    mail.setFiles(new File(file));
                }
            }
            mail.send();
            log.info("带附件邮件发送成功，收件人：{}，主题：{}，附件数：{}", to, subject, files.size());
            return true;
        } catch (Exception e) {
            log.error("带附件邮件发送失败，收件人：{}，主题：{}，错误：{}", to, subject, e.getMessage(), e);
            return false;
        }
    }

    @Override
    public boolean sendHtmlWithFiles(String to, String subject, String content, List<String> files) {
        try {
            Mail mail = Mail.create(mailAccount)
                    .setTos(to)
                    .setTitle(subject)
                    .setContent(content, true);
            
            if (CollUtil.isNotEmpty(files)) {
                for (String file : files) {
                    mail.setFiles(new File(file));
                }
            }
            mail.send();
            log.info("带附件HTML邮件发送成功，收件人：{}，主题：{}，附件数：{}", to, subject, files.size());
            return true;
        } catch (Exception e) {
            log.error("带附件HTML邮件发送失败，收件人：{}，主题：{}，错误：{}", to, subject, e.getMessage(), e);
            return false;
        }
    }

    @Override
    public boolean sendToList(List<String> toList, String subject, String content, boolean isHtml) {
        if (CollUtil.isEmpty(toList)) {
            log.warn("收件人列表为空，取消发送");
            return false;
        }
        try {
            Mail mail = Mail.create(mailAccount)
                    .setTos(toList.toArray(new String[0]))
                    .setTitle(subject)
                    .setContent(content, isHtml);
            mail.send();
            log.info("群发邮件成功，收件人数：{}，主题：{}", toList.size(), subject);
            return true;
        } catch (Exception e) {
            log.error("群发邮件失败，收件人数：{}，主题：{}，错误：{}", toList.size(), subject, e.getMessage(), e);
            return false;
        }
    }


}
