package com.google.sps.servlets;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.sps.data.EventVolunteering;
import com.google.sps.utilities.CommonUtils;
import com.google.sps.utilities.SpannerTasks;
import java.io.IOException;
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("/user-events")
public class UserProfileEventsServlet extends HttpServlet {
  private static final String EVENT_TYPE = "event-type";
  private static final String VOLUNTEERING = "volunteering";

  /** 
   * Gets the current user's events corresponding to the event type specified as a parameter.
   */
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String eventType = request.getParameter(EVENT_TYPE);
    User user = UserServiceFactory.getUserService().getCurrentUser();

    if (user == null) {
      response.sendRedirect("/index.html");
      return;
    } else if (eventType == null) {
      response.sendError(
          HttpServletResponse.SC_BAD_REQUEST, String.format("No event type specified."));
      return;
    }

    try {
      response.getWriter().println(getEventsJSONByEmail(eventType, user.getEmail()));
    } catch (IllegalArgumentException e) {
      response.sendError(
        HttpServletResponse.SC_BAD_REQUEST, String.format("Invalid event type."));
    }
  }

  /**
   * Get the events of the given type for the given email.
   * @param eventType type of event data to retrieve
   * @param userEmail user for which to retrieve events
   * @return JSON representing the events
   * @throw IllegalArgumentException if eventType is invalid
   */
  private String getEventsJSONByEmail(String eventType, String userEmail) {
    String eventJSON = "";
    switch (eventType) {
      case VOLUNTEERING:
        Set<EventVolunteering> eventsVolunteering =
            SpannerTasks.getEventsVolunteeringByEmail(userEmail);
        eventJSON = CommonUtils.convertToJson(eventsVolunteering);
        break;
      // TO DO: add case statements for hosting and participating with retrieval of data
      default:
        throw new IllegalArgumentException("Invalid event type.");
    }
    return eventJSON;
  }
}

@@ -1770,25 +1770,29 @@
 ng the event
-s
+ data
 %0A   * @throw
@@ -1943,17 +1943,13 @@
 vent
+s
 JSON
- = %22%22
 ;%0A  
@@ -2123,24 +2123,25 @@
        event
+s
 JSON = Commo
@@ -2374,16 +2374,16 @@
 ;%0A    %7D%0A
-
     retu
@@ -2390,16 +2390,17 @@
 rn event
+s
 JSON;%0A  

Fix from code review.