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.appengine.api.datastore.EntityNotFoundException;

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 Key userKey;

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

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    Entity entity; 
    try{ 
      entity = datastore.get(userKey);
      String start = (String) entity.getProperty("start");
      ArrayList<String> destinations = (ArrayList) entity.getProperty("destinations");
      UserLocations userLocations = new UserLocations(start, destinations);    
      response.setContentType("application/json;");
      response.getWriter().println(gson.toJson(userLocations));
    } catch(EntityNotFoundException e){
      System.out.println("error");
    } 
  }

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { 
    Entity entity;
    try{ 
      entity = datastore.get(userKey) ;
      places.addDestination(request.getParameter("destinations"));
      entity.setProperty("start", request.getParameter("start-location"));
      entity.setProperty("destinations", places.getDestinations());
      datastore.put(entity);
    } catch(EntityNotFoundException e){} 
    
  }

}
@@ -1297,41 +1297,8 @@
 %22);%0A
-    System.out.println(userKey);%0A
     
@@ -1330,24 +1330,24 @@
 tart%22, %22%22);%0A
+
     userEnti
@@ -1468,16 +1468,49 @@
 tKey();%0A
+    System.out.println(userKey);%0A
   %7D%0A%0A  @
@@ -1637,16 +1637,49 @@
 ntity; %0A
+    System.out.println(userKey);%0A
     try%7B
@@ -1827,16 +1827,24 @@
 rrayList
+%3CString%3E
 ) entity
@@ -2572,24 +2572,367 @@
 put(entity);
+%0A%0A      String start = (String) entity.getProperty(%22start%22);%0A      ArrayList%3CString%3E destinations = (ArrayList) entity.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 catch
@@ -2966,15 +2966,9 @@
 %7B%7D %0A
-    %0A
   %7D%0A
-%0A
 %7D

update doPost