package com.google.sps.servlets;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.DatastoreServiceConfig;
import java.io.IOException;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import com.google.sps.data.Trip;
import com.google.sps.data.UserInfo;

@WebServlet("/entity")
public class EntityServlet extends HttpServlet {
  private final Gson gson = new Gson();
  private final UserService userService = UserServiceFactory.getUserService();
  private final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    Key tripKey = createTripEntity();
    com.google.appengine.api.users.User user =  userService.getCurrentUser();
    UserInfo userInfo = addUserEntity(user, tripKey);
    String json = gson.toJson(KeyFactory.keyToString(tripKey));
    response.setContentType("application/json;");
    response.getWriter().println(json);
  } 
 
 /*
  * Adds new Trip entity with empty properties
  **/
  public Key createTripEntity(){
    System.setProperty(DatastoreServiceConfig.DATASTORE_EMPTY_LIST_SUPPORT, Boolean.TRUE.toString());
    Entity tripEntity = new Entity(Trip.DATASTORE_ENTITY_KIND);
    tripEntity.setProperty("start", "");
    tripEntity.setProperty("destinations", new ArrayList<String>());
    tripEntity.setProperty("interests", new ArrayList<String>());
    tripEntity.setProperty("route", new ArrayList<String>());
    datastore.put(tripEntity);
    Key tripKey = tripEntity.getKey();
    tripEntity.setProperty("keyString", KeyFactory.keyToString(tripKey));
    datastore.put(tripEntity);
    return tripKey;
  }

  public UserInfo addUserEntity(com.google.appengine.api.users.User user) {
    // Create a key based on the user ID
    Key userKey = KeyFactory.createKey(UserInfo.DATASTORE_ENTITY_KIND, user.getUserId());
    UserInfo userInfo;
    try {
      // try to retrieve the entity with the key 
      Entity userEntity = datastore.get(userKey);
      addTripForUser(userEntity, tripKey);
      datastore.put(userEntity);
      userInfo = UserInfo.fromEntity(userEntity);
    } catch (EntityNotFoundException exception) {
      // If the user doesn't exist yet or is new, create a new user
      Entity newUserEntity = new Entity(userKey);
      newUserEntity.setProperty("email", user.getEmail());
      newUserEntity.setProperty("userId", user.getUserId());
      newUserEntity.setProperty("tripIds", new ArrayList<String>());
      datastore.put(newUserEntity);
      userInfo = UserInfo.fromEntity(newUserEntity);
    }
    return userInfo;
  }

  public void addTripForUser(Entity userEntity, Key tripKey) {
    ArrayList<String> tripIds;
    if (userEntity.getProperty("tripIds") == null){
      tripIds =  new ArrayList<String>();
    }
    else{
      tripIds = (ArrayList<String>) userEntity.getProperty("tripIds");
    }
    tripIds.add(KeyFactory.keyToString(tripKey));
    userEntity.setProperty("tripIds", tripIds);
  }
}
@@ -1227,11 +1227,10 @@
 d do
-Pos
+Ge
 t(Ht
@@ -1310,46 +1310,8 @@
 n %7B%0A
-    Key tripKey = createTripEntity();%0A
     
@@ -1426,25 +1426,401 @@
 ity(user
-, tripKey
+);%0A    String json = gson.toJson(userInfo.getTripIds());%0A    response.setContentType(%22application/json%22);%0A    response.getWriter().println(json);%0A  %7D%0A%0A  @Override%0A  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException %7B%0A    Key tripKey = createTripEntity();%0A    String keyString = KeyFactory.keyToString(tripKey);%0A    addTripForUser(keyString
 );%0A    S
@@ -1923,17 +1923,16 @@
 ion/json
-;
 %22);%0A    
@@ -3012,51 +3012,8 @@
 y);%0A
-      addTripForUser(userEntity, tripKey);%0A
     
@@ -3602,188 +3602,224 @@
 ser(
-Entity userEntity, Key tripKey) %7B%0A    ArrayList%3CString%3E tripIds;%0A    if (userEntity.getProperty(%22tripIds%22) == null)%7B%0A      tripIds =  new ArrayList%3CString%3E();%0A    %7D%0A    else%7B%0A     
+String tripKey) %7B%0A    Key userKey = KeyFactory.createKey(UserInfo.DATASTORE_ENTITY_KIND, userService.getCurrentUser().getUserId());  %0A    try%7B%0A      Entity userEntity = datastore.get(userKey);%0A      ArrayList%3CString%3E
  tri
@@ -3878,28 +3878,24 @@
 ripIds%22);%0A  
-  %7D%0A
     tripIds.
@@ -3902,39 +3902,16 @@
 add(
-KeyFactory.keyToString(
 tripKey)
 );%0A 
@@ -3906,23 +3906,24 @@
 tripKey)
-)
 ;%0A    
+  
 userEnti
@@ -3958,13 +3958,165 @@
 ipIds);%0A
+    %7D catch (EntityNotFoundException exception) %7B%0A      logger.atInfo().withCause(e).log(%22Unable to find User Entity %25s%22, userKey);%0A      return;%0A    %7D%0A
   %7D%0A%7D

fix addTripForUSer