package cn.iselab.mooctest.user.service.impl;

import cn.iselab.mooctest.user.configure.MailProperties;
import cn.iselab.mooctest.user.constants.VerifyConstants;
import cn.iselab.mooctest.user.service.ConfigurationService;
import cn.iselab.mooctest.user.service.MailService;
import cn.iselab.mooctest.user.web.exception.ConfigurationNotExistException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

/**
 * @Description: 作用描述
 * @Author: xuexb
 * @CreateDate: 18-12-25$ 下午5:01$
 */
@Service
public class MailServiceImpl implements MailService {

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    TemplateEngine templateEngine;

    @Autowired
    MailProperties mailProperties;

    private static ArrayList<String> adminEmails = new ArrayList<>();

    @Autowired
    private ConfigurationService configurationService;

    // static {
    //     adminEmails.add("zychen@nju.edu.cn");
    //     adminEmails.add("chrong.fang@gmail.com");
    //     adminEmails.add("xujianfeng@mooctest.com");
    //     adminEmails.add("huangyong@mooctest.com");
    // }
    static {
        adminEmails.add("171256175@qq.com");
        adminEmails.add("xuexiaobo.dlmu@gmail.com");
    }


    @Override
    public void sendNormalEmail(String sender, String receiver, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject(subject);
        message.setText(content);
        message.setTo(receiver);
        message.setFrom(sender);

        mailSender.send(message);
    }

    @Override
    public void sendHtmlEmail(String sender, String receiver, String subject, String content) throws MessagingException {
        String senderName = getPlatformName();
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message,true);
        try {
            helper.setFrom(sender, senderName);
        } catch (UnsupportedEncodingException e) {
            logger.error("设置发件人名称出错", e);
        }
        helper.setTo(receiver);
        helper.setSubject(subject);
        helper.setText(content, true);
        mailSender.send(message);

    }



    @Override
    public void sendVerifyEmail(String sender, String receiver, String code) throws MessagingException {
        Context context = new Context();
        context.setVariable("verifyCode", code);
        String emailContent = templateEngine.process("email/verify_mail", context);

        String senderName = getPlatformName();
        sendHtmlEmail(sender,receiver, "【验证码-" + senderName + "】", emailContent);
    }

    private String getPlatformName() {
        String senderName = VerifyConstants.SENDER_NAME;
        try {
            senderName = configurationService.getPlatformNameConfiguration();
        } catch (ConfigurationNotExistException e) {
            e.printStackTrace();
        }
        return senderName;
    }

    @Override
    public void sendArrearsEmail() {
        Context context = new Context();
        context.setVariable("message", "平台所使用的短信服务可能已欠费，请尽快前往充值");
        context.setVariable("rechargeUrl", "http://www.yuntongxun.com/");
        String emailContent = templateEngine.process("email/arrears_mail", context);
        MimeMessage message = mailSender.createMimeMessage();
        String[] receivers = adminEmails.toArray(new String[adminEmails.size()]);

        String senderName = getPlatformName();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message,true);
            helper.setFrom(mailProperties.getUsername(), senderName);
            helper.setTo(receivers);
            helper.setSubject("【" + senderName + "】短信服务欠费通知");
            helper.setText(emailContent, true);
        } catch (UnsupportedEncodingException e) {
            logger.error("设置发件人名称时编码出错", e);
        } catch (MessagingException e){
            logger.error("设置发件人信息出错", e);
        }

        mailSender.send(message);
    }
}
