package com.google.sps.servlets;

import com.google.appengine.api.users.UserServiceFactory;
import com.google.sps.data.Event;
import com.google.sps.data.User;
import com.google.sps.utilities.CommonUtils;
import com.google.sps.utilities.EventRanker;
import com.google.sps.utilities.SpannerTasks;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/event-ranker")
public class EventRankerServlet extends HttpServlet {
  /** Returns events in order of relevance */
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String email = UserServiceFactory.getUserService().getCurrentUser().getEmail();
    Optional<User> userOptional = SpannerTasks.shallowReadUserFromEmail(email);
    User user;
    if (userOptional.isPresent()) {
      user = userOptional.get();
      // TODO: Replace getAllEvents() with the appropriate method to get all results to display
      Set<Event> relevantEvents = SpannerTasks.getAllEvents();
      List<Event> rankedRelevantEvents = EventRanker.rankEvents(user, relevantEvents);
      response.setContentType("application/json;");
      response.getWriter().println(CommonUtils.convertToJson(rankedRelevantEvents));
    } else {
      response.sendError(HttpServletResponse.SC_BAD_REQUEST, "User not found.");
    }
  }
}

@@ -85,16 +85,45 @@
 actory;%0A
+import com.google.gson.Gson;%0A
 import c
@@ -148,16 +148,16 @@
 .Event;%0A
-
 import c
@@ -341,24 +341,75 @@
 OException;%0A
+import java.util.Arrays;%0Aimport java.util.HashSet;%0A
 import java.
@@ -728,16 +728,64 @@
 rvlet %7B%0A
+  private static String EVENTS_KEY = %22events%22;%0A%0A
   /** Re
@@ -1191,158 +1191,148 @@
     
-// TODO: Replace getAllEvents() with the appropriate method to get all results to display%0A      Set%3CEvent%3E relevantEvents = SpannerTasks.getAllEvents(
+Set%3CEvent%3E relevantEvents =%0A          new HashSet%3CEvent%3E(Arrays.asList(new Gson().fromJson(request.getParameter(EVENTS_KEY), Event%5B%5D.class))
 );%0A 

Fix servlet to not hard-code events