package com.google.sps.servlets;

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

/** Servlet that handles user profile edits. */
@WebServlet("/profile-update")
public class UserProfileUpdateServlet extends HttpServlet {
  private static final String email =
      UserServiceFactory.getUserService().getCurrentUser().getEmail();

  /** Writes out information for the user corresponding to the logged-in email */
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    Optional<User> userOptional = SpannerTasks.shallowReadUserFromEmail(email);
    String userJson;
    if (!userOptional.isPresent()) {
      userJson =
          Json.createObjectBuilder()
              .add("name", "anonymous")
              .add("email", email)
              .add("interests", CommonUtils.createJsonArray(new HashSet<>()))
              .add("skills", CommonUtils.createJsonArray(new HashSet<>()))
              .add("eventsHosting", CommonUtils.createJsonArray(new HashSet<>()))
              .add("eventsParticipating", CommonUtils.createJsonArray(new HashSet<>()))
              .add("eventsVolunteering", CommonUtils.createJsonArray(new HashSet<>()))
              .add("imageUrl", "")
              .build()
              .toString();
    } else {
      // TODO: When PRs for user profile events are merged, update the events here
      User user = userOptional.get();
      userJson =
          Json.createObjectBuilder()
              .add("name", user.getName())
              .add("email", email)
              .add("interests", CommonUtils.createJsonArray(user.getInterests()))
              .add("skills", CommonUtils.createJsonArray(user.getSkills()))
              .add("eventsHosting", CommonUtils.createJsonArray(new HashSet<>()))
              .add("eventsParticipating", CommonUtils.createJsonArray(new HashSet<>()))
              .add("eventsVolunteering", CommonUtils.createJsonArray(new HashSet<>()))
              .add("imageUrl", user.getImageUrl())
              .build()
              .toString();
    }
    response.setContentType("application/json;charset=UTF-8");
    response.getWriter().println(userJson);
  }

  /** Given user fields, inserts or updates the corresponding entry in storage */
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Get the input from the form.
    String name = request.getParameter("name");
    Set<String> interests = new HashSet<>(splitAsList(request.getParameter("interests")));
    Set<String> skills = new HashSet<>(splitAsList(request.getParameter("skills")));
    User updatedUser =
        new User.Builder(name, email).setInterests(interests).setSkills(skills).build();

    SpannerTasks.insertOrUpdateUser(updatedUser);
    response.sendRedirect("/profile.html");
  }

  private static List<String> splitAsList(String values) {
    return Arrays.asList(values.split("\\s*,\\s*"));
  }
}

@@ -700,16 +700,480 @@
 rvlet %7B%0A
+  private static final String NAME = %22name%22;%0A  private static final String EMAIL = %22email%22;%0A  private static final String INTERESTS = %22interests%22;%0A  private static final String SKILLS = %22skills%22;%0A  private static final String EVENTS_HOSTING = %22eventsHosting%22;%0A  private static final String EVENTS_PARTICIPATING = %22eventsParticipating%22;%0A  private static final String EVENTS_VOLUNTEERING = %22eventsVolunteering%22;%0A  private static final String IMAGE_URL = %22imageUrl%22;%0A
   privat
@@ -1678,22 +1678,20 @@
    .add(
-%22name%22
+NAME
 , %22anony
@@ -1708,39 +1708,37 @@
            .add(
-%22email%22
+EMAIL
 , email)%0A       
@@ -1745,35 +1745,33 @@
        .add(
-%22interests%22
+INTERESTS
 , CommonUtil
@@ -1821,32 +1821,30 @@
        .add(
-%22skills%22
+SKILLS
 , CommonUtil
@@ -1894,39 +1894,38 @@
        .add(
-%22eventsHosting%22
+EVENTS_HOSTING
 , CommonUtil
@@ -1979,37 +1979,36 @@
    .add(
-%22eventsParticipating%22
+EVENTS_PARTICIPATING
 , Common
@@ -2066,36 +2066,35 @@
    .add(
-%22eventsVolunteering%22
+EVENTS_VOLUNTEERING
 , Common
@@ -2152,26 +2152,25 @@
    .add(
-%22imageUrl%22
+IMAGE_URL
 , %22%22)%0A  
@@ -2424,22 +2424,20 @@
    .add(
-%22name%22
+NAME
 , user.g
@@ -2465,23 +2465,21 @@
    .add(
-%22email%22
+EMAIL
 , email)
@@ -2498,27 +2498,25 @@
    .add(
-%22interests%22
+INTERESTS
 , Common
@@ -2578,24 +2578,22 @@
    .add(
-%22skills%22
+SKILLS
 , Common
@@ -2652,31 +2652,30 @@
    .add(
-%22eventsHosting%22
+EVENTS_HOSTING
 , Common
@@ -2733,37 +2733,36 @@
    .add(
-%22eventsParticipating%22
+EVENTS_PARTICIPATING
 , Common
@@ -2820,36 +2820,35 @@
    .add(
-%22eventsVolunteering%22
+EVENTS_VOLUNTEERING
 , Common
@@ -2906,26 +2906,25 @@
    .add(
-%22imageUrl%22
+IMAGE_URL
 , user.g
@@ -3374,22 +3374,20 @@
 rameter(
-%22name%22
+NAME
 );%0A    S
@@ -3456,27 +3456,25 @@
 rameter(
-%22interests%22
+INTERESTS
 )));%0A   
@@ -3542,24 +3542,22 @@
 rameter(
-%22skills%22
+SKILLS
 )));%0A   

Use constants instead