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.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.common.flogger.FluentLogger;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.annotation.MultipartConfig;
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 */
@MultipartConfig
@WebServlet("/api/destinations")
public class DestinationsServlet extends HttpServlet {

  private static final FluentLogger logger = FluentLogger.forEnclosingClass();  

  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);
    } catch (EntityNotFoundException e) {
      UserLocations userLocations = new UserLocations("", new ArrayList<String>());
      response.setContentType("application/json;");
      response.getWriter().println(gson.toJson(userLocations));
      logger.atInfo().withCause(e).log("Unable to find UserLocations Entity %s", userKey);
      return;
    }
    String start = (String) entity.getProperty("start");
    ArrayList<String> destinations = (ArrayList<String>) entity.getProperty("destinations");
    UserLocations userLocations = new UserLocations(start, destinations);
    response.setContentType("application/json;");
    response.getWriter().println(gson.toJson(userLocations));
  }

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    Entity entity;
    try {
      entity = datastore.get(userKey);
    } catch (EntityNotFoundException e) {
      UserLocations userLocations = new UserLocations("", new ArrayList<String>());
      response.setContentType("application/json;");
      response.getWriter().println(gson.toJson(userLocations));
      logger.atInfo().withCause(e).log("Unable to find UserLocations Entity %s", userKey);
      return;
    }
    String start = request.getParameter("start-location"); 
    String destination = request.getParameter("destinations");
    entity.setProperty("start", start);
    ArrayList<String> destinations = (ArrayList<String>) entity.getProperty("destinations");
    if(destinations == null){
      destinations = new ArrayList<String>();
    }
    destinations.add(destination);
    entity.setProperty("destinations", destinations);

    datastore.put(entity);
    UserLocations userLocations = new UserLocations(start, destinations);    
    response.setContentType("application/json;");
    response.getWriter().println(gson.toJson(userLocations));
  }
}

@@ -932,18 +932,16 @@
 Class();
-  
 %0A%0A  priv
@@ -1355,18 +1355,16 @@
 ();%0A  %7D%0A
-  
 %0A  @Over
@@ -2820,18 +2820,17 @@
 ation%22);
- 
 %0A
+
     Stri
@@ -3023,16 +3023,17 @@
 ;%0A    if
+ 
 (destina
@@ -3046,16 +3046,17 @@
 == null)
+ 
 %7B%0A      
@@ -3214,24 +3214,25 @@
 ut(entity);%0A
+%0A
     UserLoca
@@ -3292,20 +3292,16 @@
 ations);
-    
 %0A    res

fix java format