package mil.dds.anet.resources;

import java.util.List;

import javax.annotation.security.PermitAll;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import io.dropwizard.auth.Auth;
import mil.dds.anet.AnetObjectEngine;
import mil.dds.anet.beans.Person;
import mil.dds.anet.beans.Report;
import mil.dds.anet.views.SimpleView;

@Path("")
@PermitAll
public class HomeResource {

	@GET
	@Path("{path: .*}")
	@Produces(MediaType.TEXT_HTML)
	public SimpleView reactIndex(@Auth Person p) {
		//Get a list of any reports this person wrote that need to be approved, and reports that this person can approve.
		List<Report> myApprovals = AnetObjectEngine.getInstance().getReportDao().getReportsForMyApproval(p);
		List<Report> myPending = AnetObjectEngine.getInstance().getReportDao().getMyReportsPendingApproval(p);
		SimpleView view = new SimpleView("/views/react.ftl");
		view.addToContext("myApprovals", myApprovals);
		view.addToContext("myPending", myPending);
		return view;
	}

	@GET
	@Path("/legacy")
	public SimpleView index(@Auth Person p) {
		//Get a list of any reports this person wrote that need to be approved, and reports that this person can approve.
		List<Report> myApprovals = AnetObjectEngine.getInstance().getReportDao().getReportsForMyApproval(p);
		List<Report> myPending = AnetObjectEngine.getInstance().getReportDao().getMyReportsPendingApproval(p);
		SimpleView view = new SimpleView("/views/index.ftl");
		view.addToContext("myApprovals", myApprovals);
		view.addToContext("myPending", myPending);
		return view;
	}

	public static String ALL_TYPES = "people,reports,positions,poams,locations";

	@GET
	@Path("/api/search")
	public SimpleView search(@QueryParam("q") String query, @QueryParam("types") String types) {
		if (types == null) { types = ALL_TYPES;}
		types = types.toLowerCase();

		SimpleView view = new SimpleView("/views/search.ftl");
		if (types.contains("people")) {
			view.addToContext("people", AnetObjectEngine.getInstance().getPersonDao().searchByName(query));
		}
		if (types.contains("reports")) {
			view.addToContext("reports", AnetObjectEngine.getInstance().getReportDao().search(query));
		}
		if (types.contains("positions")) {
			view.addToContext("positions", AnetObjectEngine.getInstance().getPositionDao().search(query));
		}
		if (types.contains("poams")) {
			view.addToContext("poams", AnetObjectEngine.getInstance().getPoamDao().search(query));
		}
		if (types.contains("locations")) {
			view.addToContext("locations", AnetObjectEngine.getInstance().getLocationDao().searchByName(query));
		}
		return view;
	}
}

@@ -48,16 +48,42 @@
 il.List;
+%0Aimport java.util.HashMap;
 %0A%0Aimport
@@ -1760,33 +1760,85 @@
 arch%22)%0A%09
-public SimpleView
+@Produces(MediaType.APPLICATION_JSON)%0A%09public HashMap%3CString, Object%3E
  search(
@@ -1986,63 +1986,73 @@
 %0A%0A%09%09
-SimpleView view = new SimpleView(%22/views/search.ftl%22
+HashMap%3CString, Object%3E result = new HashMap%3CString, Object%3E(
 );%0A
+%0A
 %09%09if
@@ -2084,32 +2084,25 @@
 )) %7B%0A%09%09%09
-view.addToContex
+result.pu
 t(%22peopl
@@ -2215,32 +2215,25 @@
 )) %7B%0A%09%09%09
-view.addToContex
+result.pu
 t(%22repor
@@ -2343,32 +2343,25 @@
 )) %7B%0A%09%09%09
-view.addToContex
+result.pu
 t(%22posit
@@ -2471,32 +2471,25 @@
 )) %7B%0A%09%09%09
-view.addToContex
+result.pu
 t(%22poams
@@ -2595,32 +2595,25 @@
 )) %7B%0A%09%09%09
-view.addToContex
+result.pu
 t(%22locat
@@ -2693,16 +2693,17 @@
 ));%0A%09%09%7D%0A
+%0A
 %09%09return
@@ -2699,23 +2699,25 @@
 %7D%0A%0A%09%09return 
-view
+result
 ;%0A%09%7D%0A%7D%0A

Return JSON in /api/search