package com.github.ckwen.je.spring.mvc.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.apache.commons.lang.StringUtils;
import org.apache.velocity.app.VelocityEngine;
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.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.ui.velocity.VelocityEngineUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import freemarker.template.Template;

/**
 * <p>
 * Title: MainController
 * </p>
 * <p>
 * Description:
 * </p>
 * 
 * @author Wen
 * @created 2016年12月28日 下午2:15:13
 * @modified [who date description]
 * @check [who date description]
 */
@Controller
@RequestMapping("email")
public class EmailController {

    private static final String UTF_8 = "utf-8";

    private static final String REDIRECT_EMAIL = "redirect:/email/";

    private static final String ACTION_STATE = "actionState";

    private static final String SUCCESS = "success";

    private static final String ERROR = "error";

    @Value("${mail.username}")
    private String mailFrom;

    @Autowired
    private JavaMailSender sender;

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Autowired
    private VelocityEngine velocityEngine;

    @RequestMapping()
    public String index() {
        return "email_send";
    }

    /**
     * 最简单的文本邮件
     * 
     * @param redirectAttributes
     * @param email
     * @param subject
     * @param content
     * @return
     */
    @RequestMapping("send/1")
    public String send1(RedirectAttributes redirectAttributes, String email, String subject, String content) {
        try {

            Assert.isTrue(StringUtils.isNotBlank(email));
            Assert.isTrue(StringUtils.isNotBlank(subject));
            Assert.isTrue(StringUtils.isNotBlank(content));

            System.out.printf("email:%s, subject:%s, content:%s\n", email, subject, content);

            SimpleMailMessage smm = new SimpleMailMessage();

            smm.setFrom(mailFrom);

            smm.setTo(email);
            smm.setSubject(subject);
            smm.setText(content);

            sender.send(smm);

            redirectAttributes.addFlashAttribute(ACTION_STATE, SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            redirectAttributes.addFlashAttribute(ACTION_STATE, ERROR);
        }
        return REDIRECT_EMAIL;
    }

    /**
     * 发送html格式的邮件
     * 
     * @param redirectAttributes
     * @param email
     * @param subject
     * @param content
     * @return
     */
    @RequestMapping("send/2")
    public String send2(RedirectAttributes redirectAttributes, String email, String subject, String content) {
        try {

            Assert.isTrue(StringUtils.isNotBlank(email));
            Assert.isTrue(StringUtils.isNotBlank(subject));
            Assert.isTrue(StringUtils.isNotBlank(content));

            System.out.printf("email:%s, subject:%s, content:%s\n", email, subject, content);

            MimeMessage mimeMessage = sender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);

            messageHelper.setFrom(mailFrom);
            messageHelper.setTo(email);
            messageHelper.setSubject(subject);
            messageHelper.setText(content, true);

            sender.send(mimeMessage);

            redirectAttributes.addFlashAttribute(ACTION_STATE, SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            redirectAttributes.addFlashAttribute(ACTION_STATE, ERROR);
        }
        return REDIRECT_EMAIL;
    }

    /**
     * Velocity
     * 
     * @param redirectAttributes
     * @param email
     * @param subject
     * @return
     */
    @RequestMapping("send/3")
    public String send3(RedirectAttributes redirectAttributes, String email, String subject) {
        try {

            Assert.isTrue(StringUtils.isNotBlank(email));
            Assert.isTrue(StringUtils.isNotBlank(subject));

            System.out.printf("email:%s, subject:%s\n", email, subject);

            MimeMessage mimeMessage = sender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, UTF_8);

            messageHelper.setFrom(mailFrom);
            messageHelper.setTo(email);
            messageHelper.setSubject(subject);

            //

            Map<String, Object> map = new HashMap<>();
            map.put("email", email);
            map.put("subject", subject);

            String templateLocation = "template_velocity/test3.vm";

            String content = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateLocation, UTF_8, map);

            messageHelper.setText(content, true);

            sender.send(mimeMessage);

            redirectAttributes.addFlashAttribute(ACTION_STATE, SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            redirectAttributes.addFlashAttribute(ACTION_STATE, ERROR);
        }
        return REDIRECT_EMAIL;
    }

    /**
     * FreeMarker
     * 
     * @param redirectAttributes
     * @param email
     * @param subject
     * @return
     */
    @RequestMapping("send/4")
    public String send4(RedirectAttributes redirectAttributes, String email, String subject) {
        try {

            Assert.isTrue(StringUtils.isNotBlank(email));
            Assert.isTrue(StringUtils.isNotBlank(subject));

            System.out.printf("email:%s, subject:%s\n", email, subject);

            MimeMessage mimeMessage = sender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, UTF_8);

            messageHelper.setFrom(mailFrom);
            messageHelper.setTo(email);
            messageHelper.setSubject(subject);

            //
            Template template = freeMarkerConfigurer.getConfiguration().getTemplate("test3.vtl");
            Map<String, Object> map = new HashMap<>();
            map.put("email", email);
            map.put("subject", subject);
            String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);

            messageHelper.setText(content, true);

            sender.send(mimeMessage);

            redirectAttributes.addFlashAttribute(ACTION_STATE, SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            redirectAttributes.addFlashAttribute(ACTION_STATE, ERROR);
        }
        return REDIRECT_EMAIL;
    }

}
