package edu.tamu.framework.controller;

import static edu.tamu.framework.enums.ApiResponseType.SUCCESS;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import edu.tamu.framework.aspect.annotation.ApiMapping;
import edu.tamu.framework.aspect.annotation.Data;
import edu.tamu.framework.aspect.annotation.Shib;
import edu.tamu.framework.model.ApiResponse;
import edu.tamu.framework.model.Credentials;
import edu.tamu.framework.util.EmailUtility;

@RestController
@ApiMapping("/report")
public class ReportingController {
	
	@Autowired
	private EmailUtility emailUtility;
	
	@Autowired
	private ObjectMapper objectMapper;
	
	@ApiMapping(value = "/error", method = POST)
	public ApiResponse reportError(@Shib Object shibObj, @Data String data) throws Exception {

		Credentials shib = (Credentials) shibObj;
		
		Map<String, String> errorReport = new HashMap<String, String>();
		
		try {
			errorReport = objectMapper.readValue(data, new TypeReference<HashMap<String, String>>(){});
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		errorReport.put("user", shib.getFirstName() + " " + shib.getLastName() + " (" + shib.getUin() + ")");
		
		String content = "Error Report\n\n";
		
		content += "channel: " +  errorReport.get("channel") + "\n";
		content += "time: " +  new Date(Long.parseLong(errorReport.get("time"))*1000) + "\n";
		content += "type: " +  errorReport.get("type") + "\n";
		content += "message: " +  errorReport.get("message") + "\n";
		content += "user: " +  errorReport.get("user") + "\n";
		
		emailUtility.sendEmail("Error Report", content);
		
		return new ApiResponse(SUCCESS);
	}
	
}

@@ -1511,16 +1511,44 @@
 %5Cn%22;%0A%09%09%0A
+%09%09Date now = new Date();%0A%09%09%0A
 %09%09conten
@@ -1630,63 +1630,11 @@
 %22 + 
- new Date(Long.parseLong(errorReport.get(%22time%22))*1000)
+now
  + %22
@@ -1904,16 +1904,32 @@
 (SUCCESS
+, now.toString()
 );%0A%09%7D%0A%09%0A

Use server time for error reporting.