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.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** Servlet that returns start location and destinations user inputs */
@MultipartConfig
@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");
    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<String>) entity.getProperty("destinations");
      UserLocations userLocations = new UserLocations(start, destinations); 
      System.out.println(userLocations.getDestinations());   
      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);

      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){} 
  }
}
@@ -540,16 +540,63 @@
 ception;
+%0Aimport com.google.common.flogger.FluentLogger;
 %0A%0Aimport
@@ -900,16 +900,17 @@
 ponse;%0A%0A
+%0A
 /** Serv
@@ -1288,24 +1288,101 @@
 eService();%0A
+private static final FluentLogger logger = FluentLogger.forEnclosingClass();%0A
 private Key 
@@ -2295,34 +2295,90 @@
     
-System.out.println(%22error%22
+logger.atInfo().withCause(e).log(%22Unable to find UserLocations Entity %25s%22, userKey
 );%0A 
@@ -3144,32 +3144,32 @@
 serLocations));%0A
-
     %7D catch(Enti
@@ -3191,16 +3191,112 @@
 tion e)%7B
+%0A      logger.atInfo().withCause(e).log(%22Unable to find UserLocations Entity %25s%22, userKey);%0A    
 %7D %0A  %7D%0A%7D

add logging error message