package com.google.sps.servlets;

import com.google.appengine.api.users.UserServiceFactory;
import com.google.gson.Gson;
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.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
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 {
  private static String EVENTS_KEY = "events";
 
  /** Returns events in order of relevance */
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    ArrayList<String> eventIds = new ArrayList<>();
    String[] eventIdsParameter = (request.getParameter(EVENTS_KEY).split(","));
    for (String event : eventIdsParameter) {
        eventIds.add(event.replace("\"", "").replace("[", "").replace("]", ""));
    }
    String email = UserServiceFactory.getUserService().getCurrentUser().getEmail();
    Optional<User> userOptional = SpannerTasks.shallowReadUserFromEmail(email);
    User user;
    if (userOptional.isPresent()) {
      user = userOptional.get();
      Set<Event> relevantEvents = new HashSet<Event>(SpannerTasks.getEventsFromIds(eventIds));
      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.");
    }
  }
}


@@ -226,16 +226,68 @@
 nUtils;%0A
+import com.google.sps.utilities.GetLabelCategories;%0A
 import c
@@ -723,16 +723,44 @@
 esponse;
+%0Aimport org.javatuples.Pair;
 %0A%0A@WebSe
@@ -883,17 +883,16 @@
 vents%22;%0A
- 
 %0A  /** R
@@ -1041,16 +1041,233 @@
 ption %7B%0A
+    String email = UserServiceFactory.getUserService().getCurrentUser().getEmail();%0A    Optional%3CUser%3E userOptional = SpannerTasks.shallowReadUserFromEmail(email);%0A    User user;%0A    if (userOptional.isPresent()) %7B%0A  
     Arra
@@ -1310,24 +1310,26 @@
 st%3C%3E();%0A    
+ 
+ 
 String%5B%5D eve
@@ -1396,16 +1396,18 @@
 ));%0A    
+  
 for (Str
@@ -1447,16 +1447,18 @@
         
+ 
+ 
 eventIds
@@ -1530,224 +1530,11 @@
     
-%7D%0A    String email = UserServiceFactory.getUserService().getCurrentUser().getEmail();%0A    Optional%3CUser%3E userOptional = SpannerTasks.shallowReadUserFromEmail(email);%0A    User user;%0A    if (userOptional.isPresent()) %7B
+  %7D
 %0A   
@@ -1569,18 +1569,19 @@
 ;%0A      
-Se
+Lis
 t%3CEvent%3E
@@ -1582,16 +1582,22 @@
 Event%3E r
+ankedR
 elevantE
@@ -1608,27 +1608,37 @@
 s = 
-new HashSet%3CEvent%3E(
+EventRanker.rankEvents(user, 
 Span
@@ -1679,95 +1679,8 @@
 ));%0A
-      List%3CEvent%3E rankedRelevantEvents = EventRanker.rankEvents(user, relevantEvents);%0A
     

Connect event ranker servlet