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 NAME = "name";
  private static final String EMAIL = "email";
  private static final String INTERESTS = "interests";
  private static final String SKILLS = "skills";
  private static final String EVENTS_HOSTING = "eventsHosting";
  private static final String EVENTS_PARTICIPATING = "eventsParticipating";
  private static final String EVENTS_VOLUNTEERING = "eventsVolunteering";
  private static final String IMAGE_URL = "imageUrl";
  private static String email;

  /** Writes out information for the user corresponding to the logged-in email */
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    email = UserServiceFactory.getUserService().getCurrentUser().getEmail();
    Optional<User> userOptional = SpannerTasks.shallowReadUserFromEmail(email);
    User user;
    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(EVENTS_HOSTING, CommonUtils.createJsonArray(new HashSet<>()))
              .add(EVENTS_PARTICIPATING, CommonUtils.createJsonArray(new HashSet<>()))
              .add(EVENTS_VOLUNTEERING, CommonUtils.createJsonArray(new HashSet<>()))
              .add(IMAGE_URL, "")
              .build()
              .toString();
    } else {
      // TODO: When PRs for user profile events are merged, update the events here
      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(EVENTS_HOSTING, CommonUtils.createJsonArray(new HashSet<>()))
              .add(EVENTS_PARTICIPATING, CommonUtils.createJsonArray(new HashSet<>()))
              .add(EVENTS_VOLUNTEERING, CommonUtils.createJsonArray(new HashSet<>()))
              .add(IMAGE_URL, 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 {
    email = UserServiceFactory.getUserService().getCurrentUser().getEmail();
    // 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*"));
  }
}

@@ -1546,16 +1546,17 @@
 email);%0A
+%0A
     User
@@ -1566,37 +1566,16 @@
 er;%0A
-    String userJson;%0A
     if (
 !use
@@ -1574,9 +1574,8 @@
 if (
-!
 user
@@ -1612,582 +1612,101 @@
 user
-Json =%0A          Json.createObjectBuilder()%0A              .add(NAME, %22anonymous%22)%0A              .add(EMAIL, email)%0A              .add(INTERESTS, CommonUtils.createJsonArray(new HashSet%3C%3E()))%0A              .add(SKILLS, CommonUtils.createJsonArray(new HashSet%3C%3E()))%0A              .add(EVENTS_HOSTING, CommonUtils.createJsonArray(new HashSet%3C%3E()))%0A              .add(EVENTS_PARTICIPATING, CommonUtils.createJsonArray(new HashSet%3C%3E()))%0A              .add(EVENTS_VOLUNTEERING, CommonUtils.createJsonArray(new HashSet%3C%3E()))%0A              .add(IMAGE_URL, %22%22)%0A              
+ = userOptional.get();%0A    %7D else %7B%0A      user = new User.Builder(%22anonymous%22, email)
 .build()
 %0A   
@@ -1705,170 +1705,72 @@
 ld()
+;
 %0A
-   
       
-     .toString();%0A    %7D else %7B%0A      // TODO: When PRs for user profile events are merged, update the events here%0A      user = userOptional.get();%0A     
+SpannerTasks.insertOrUpdateUser(user);%0A    %7D%0A%0A    String
  use
@@ -1777,18 +1777,16 @@
 rJson =%0A
-  
         
@@ -1820,26 +1820,24 @@
             
-  
 .add(NAME, u
@@ -1859,26 +1859,24 @@
             
- 
- 
 .add(EMAIL, 
@@ -1882,18 +1882,16 @@
  email)%0A
-  
         
@@ -1968,26 +1968,24 @@
             
-  
 .add(SKILLS,
@@ -2036,34 +2036,32 @@
 )))%0A            
- 
- 
 .add(EVENTS_HOST
@@ -2103,34 +2103,32 @@
 w HashSet%3C%3E()))%0A
-  
             .add
@@ -2200,34 +2200,32 @@
 )))%0A            
-  
 .add(EVENTS_VOLU
@@ -2284,60 +2284,8 @@
 )))%0A
-              .add(IMAGE_URL, user.getImageUrl())%0A  
     
@@ -2301,18 +2301,16 @@
 build()%0A
-  
         
@@ -2326,23 +2326,16 @@
 ring();%0A
-    %7D%0A%0A
     resp

revert userprofileUpdateServlet