package org.helianto.sendgrid.controller;

import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Static page controller.
 * 
 * @author mauriciofernandesdecastro
 */
@Controller
@RequestMapping("/static")
public class StaticController {

	private static final Logger logger = LoggerFactory.getLogger(StaticController.class);
	
	/**
	 * Build email page.
	 * 
	 * @param model
	 * @param templateId
	 * @param matrixVars
	 */
	@RequestMapping(value="/template/{templateId}", method=RequestMethod.GET)
	public String message(Model model, @PathVariable String templateId, @MatrixVariable Map<String, LinkedList<String>> matrixVars
			, @RequestParam(required=false) String confirmationuri) {
		logger.debug("Static content from template: {}", templateId);
		
		model.addAttribute("staticuri", "#");
		model.addAttribute("staticContent", "/sendgrid/"+templateId);
		//não funciona 
		model.addAllAttributes(matrixVars);
		for (Entry<String, LinkedList<String>> entry : matrixVars.entrySet())
		{
			for (String string : entry.getValue()) {
				model.addAttribute(entry.getKey(), string);
				break;
			}
		}
		if(confirmationuri!=null){
			model.addAttribute("confirmationuri", confirmationuri);
		}
		
		return "frame-static";
	}

}

@@ -1166,16 +1166,64 @@
 ationuri
+, @RequestParam(required=false) String rejecturi
 ) %7B%0A%09%09lo
@@ -1325,16 +1325,19 @@
 , %22#%22);%0A
+%09%09%0A
 %09%09model.
@@ -1728,16 +1728,90 @@
 i);%0A%09%09%7D%0A
+%09%09if(rejecturi!=null)%7B%0A%09%09%09model.addAttribute(%22rejecturi%22, rejecturi);%0A%09%09%7D%0A
 %09%09%0A%09%09ret

add reject uri