package cn.ezhizao.project.tool.email.service;

import cn.ezhizao.project.business.email.domain.BizEmail;
import cn.ezhizao.project.business.email.mapper.BizEmailMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Properties;

@Service
public class EmailService {
    @Value("${spring.mail.username}")
    private String from;
    private JavaMailSenderImpl javaMailSenderImpl;
    @Autowired
    private BizEmailMapper bizEmailMapper;
    @Resource
    private JavaMailSender javaMailSender;

    @Autowired
    public EmailService(BizEmailMapper bizEmailMapper) {
        this.bizEmailMapper = bizEmailMapper;
//        setEmailSender();
    }

    public void setEmailSender() {
        QueryWrapper<BizEmail> emailQuery = new QueryWrapper<>();
        emailQuery.eq("type", 0);
        emailQuery.eq("is_stop", 0);
        List<BizEmail> emailList = bizEmailMapper.selectList(emailQuery);
        if (emailList.size() > 0) {
            javaMailSenderImpl = new JavaMailSenderImpl();
            javaMailSenderImpl.setHost(emailList.get(0).getHost());
            javaMailSenderImpl.setPort(Integer.parseInt(emailList.get(0).getPort()));
            javaMailSenderImpl.setUsername(emailList.get(0).getEmailAddress());
            javaMailSenderImpl.setPassword(emailList.get(0).getAuthCode());
            javaMailSenderImpl.setProtocol(emailList.get(0).getProtocol());
            if (emailList.get(0).getIsSsl().equals(1)) {
                Properties properties = javaMailSenderImpl.getJavaMailProperties();
                properties.put("mail.smtp.auth", "true");
                properties.put("mail.smtp.starttls.enable", "true");
                properties.put("mail.smtp.ssl.enable", "true");
                properties.put("mail.smtp.ssl.socketFactory", "com.sun.mail.util.MailSSLSocketFactory");
                properties.put("mail.smtp.ssl.fallback", "false");
            }
        }
    }

    public void sendEmail(String to, String subject, String text) {
        setEmailSender();
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setTo(to);
            message.setSubject(subject);
            message.setText(text);
//        message.setFrom(from);
            message.setFrom(Objects.requireNonNull(javaMailSenderImpl.getUsername()));
            javaMailSenderImpl.send(message);
        } catch (Exception e) {
            SimpleMailMessage error = new SimpleMailMessage();
            error.setTo(to);
            error.setSubject(subject);
            error.setText(e.getMessage());
            error.setFrom(from);
            javaMailSender.send(error);
        }
    }
}
