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.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.SortDirection;

import com.google.gson.Gson;
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;

/** Servlet that returns start location and destinations user inputs */
@WebServlet("/api/destinations")
public class DestinationsServlet extends HttpServlet {

private final UserLocations places = new UserLocations("", new ArrayList<String>());
private final Gson gson = new Gson();
private final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
private Long userKey;

  @Override
  public void init() {
    Entity userEntity = new Entity("UserInputs", "test");
    System.out.println(userKey);
    userKey= userEntity.getKey().getId();
    userEntity.setProperty("start", "");
    userEntity.setProperty("destinations", new ArrayList<String>());
    datastore.put(userEntity);
  }

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    Query userInput = new Query("UserInputs");
    PreparedQuery query = datastore.prepare(userInput);
    for(Entity user: query.asIterable()) {
      Long key = user.getKey().getId();
      if(key == userKey) {
          System.out.println(key);
        Entity currentUser = user;
        String start = (String) currentUser.getProperty("start");
        ArrayList<String> destinations = (ArrayList) currentUser.getProperty("destinations");
        UserLocations userLocations = new UserLocations(start, destinations);    
        response.setContentType("application/json;");
        response.getWriter().println(gson.toJson(userLocations));
        return;
      }
    }
    UserLocations userLocations = new UserLocations("", new ArrayList<String>());    
    response.setContentType("application/json;");
    response.getWriter().println(gson.toJson(userLocations));
  }

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { 
    Query userInput = new Query("UserInputs");
    PreparedQuery query = datastore.prepare(userInput);
    for(Entity user: query.asIterable()) {
      Long key = user.getKey().getId();
      if(key == userKey) {
        Entity currentUser = user;
        places.addDestination(request.getParameter("destinations"));
        currentUser.setProperty("start", request.getParameter("start-location"));
        currentUser.setProperty("destinations", places.getDestinations());
        datastore.put(currentUser);

        String start = (String) currentUser.getProperty("start");
        ArrayList<String> destinations = (ArrayList) currentUser.getProperty("destinations");
        UserLocations userLocations = new UserLocations(start, destinations);    
        response.setContentType("application/json;");
        response.getWriter().println(gson.toJson(userLocations));
      }
    }
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    
  }

}
@@ -473,16 +473,83 @@
 rection;
+%0Aimport com.google.appengine.api.datastore.EntityNotFoundException;
 %0A%0Aimport
@@ -1186,20 +1186,19 @@
 private 
-Long
+Key
  userKey
@@ -1330,50 +1330,8 @@
 y);%0A
-    userKey= userEntity.getKey().getId();%0A
     
@@ -1467,16 +1467,50 @@
 ntity);%0A
+    userKey= userEntity.getKey();%0A
   %7D%0A%0A  @
@@ -1625,289 +1625,73 @@
     
-Query userInput = new Query(%22UserInputs%22);%0A    PreparedQuery query = datastore.prepare(userInput);%0A    for(Entity user: query.asIterable()) %7B%0A      Long key = user.getKey().getId();%0A      if(key == userKey) %7B%0A          System.out.println(key);%0A        Entity currentUser = user;%0A  
+Entity entity; %0A    try%7B %0A      entity = datastore.get(userKey);%0A
     
@@ -1712,35 +1712,30 @@
  = (String) 
-currentUser
+entity
 .getProperty
@@ -1737,34 +1737,32 @@
 perty(%22start%22);%0A
-  
       ArrayList%3C
@@ -1792,35 +1792,30 @@
 (ArrayList) 
-currentUser
+entity
 .getProperty
@@ -1824,34 +1824,32 @@
 destinations%22);%0A
-  
       UserLocati
@@ -1904,34 +1904,32 @@
 tinations);    %0A
-  
       response.s
@@ -1962,34 +1962,32 @@
 /json;%22);%0A      
-  
 response.getWrit
@@ -2036,600 +2036,273 @@
     
-    return;%0A      %7D%0A    %7D%0A    UserLocations userLocations = new UserLocations(%22%22, new ArrayList%3CString%3E());    %0A    response.setContentType(%22application/json;%22);%0A    response.getWriter().println(gson.toJson(userLocations));%0A  %7D%0A%0A  @Override%0A  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException %7B %0A    Query userInput = new Query(%22UserInputs%22);%0A    PreparedQuery query = datastore.prepare(userInput);%0A    for(Entity user: query.asIterable()) %7B%0A      Long key = user.getKey().getId();%0A      if(key == userKey) %7B%0A        Entity currentUser = user;%0A  
+%7D catch(EntityNotFoundException e)%7B%0A      System.out.println(%22error%22);%0A    %7D %0A  %7D%0A%0A  @Override%0A  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException %7B %0A    Entity entity;%0A    try%7B %0A      entity = datastore.get(userKey) ;%0A
     
@@ -2366,37 +2366,30 @@
 s%22));%0A      
-  currentUser
+entity
 .setProperty
@@ -2449,21 +2449,14 @@
     
-  
-currentUser
+entity
 .set
@@ -2513,18 +2513,16 @@
 ;%0A      
-  
 datastor
@@ -2531,478 +2531,58 @@
 put(
-currentUser);%0A%0A        String start = (String) currentUser.getProperty(%22start%22);%0A        ArrayList%3CString%3E destinations = (ArrayList) currentUser.getProperty(%22destinations%22);%0A        UserLocations userLocations = new UserLocations(start, destinations);    %0A        response.setContentType(%22application/json;%22);%0A        response.getWriter().println(gson.toJson(userLocations));%0A      %7D%0A    %7D%0A    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
+entity);%0A    %7D catch(EntityNotFoundException e)%7B%7D 
 %0A   

fix datastore keys