// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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.KeyFactory;
import com.google.common.flogger.FluentLogger;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.sps.data.Trip;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** Servlet that handles data for stops */
@WebServlet("/api/stop")
public final class StopsServlet extends HttpServlet {
  private final Gson gson = new Gson();
  private final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
  private static final Type ARRAYLIST_STRING = new TypeToken<ArrayList<String>>() {}.getType();
  private static final FluentLogger logger = FluentLogger.forEnclosingClass();

  /* Passes saved destinations stops (if any) to be shown in the schedule panel */
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    List<String> stops = Collections.emptyList();
    try {
      Entity entity = datastore.get(KeyFactory.createKey(Trip.KIND, 5910974510923776L));
      stops = (ArrayList<String>) entity.getProperty("destinations");
    } catch (EntityNotFoundException e) {
      logger.atInfo().withCause(e).log("Could not retrieve stops due to faulty key", e);
    }
    response.setContentType("application/json;");
    response.getWriter().println(gson.toJson(stops));
  }

  /* Modifies the destinations array of Trip entity in datastore */
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    ArrayList<String> stops = gson.fromJson(request.getReader(), ARRAYLIST_STRING);
    System.out.println("dopost");
    try {
      Entity entity = datastore.get(KeyFactory.createKey(Trip.KIND, 5910974510923776L));
      entity.setProperty("destinations", stops);
      datastore.put(entity);
    } catch (EntityNotFoundException e) {
      logger.atInfo().withCause(e).log("Could not retrieve stops due to faulty key", e);
    }
  }
}

@@ -856,24 +856,71 @@
 dException;%0A
+import com.google.appengine.api.datastore.Key;%0A
 import com.g
@@ -1555,140 +1555,8 @@
 t %7B%0A
-  private final Gson gson = new Gson();%0A  private final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();%0A
   pr
@@ -1727,16 +1727,227 @@
 ass();%0A%0A
+  private final Gson gson = new Gson();%0A  private final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();%0A  private final Key key = KeyFactory.createKey(Trip.KIND, 5910974510923776L);%0A%0A
   /* Pas
@@ -2232,58 +2232,11 @@
 get(
-K
+k
 ey
-Factory.createKey(Trip.KIND, 5910974510923776L)
 );%0A 
@@ -2377,32 +2377,43 @@
 ithCause(e).log(
+%0A          
 %22Could not retri
@@ -2420,35 +2420,71 @@
 eve 
-stops due to faulty key%22, e
+Entity for Trip with key %25s while trying to get the stops%22, key
 );%0A 
@@ -2865,42 +2865,8 @@
 G);%0A
-    System.out.println(%22dopost%22);%0A
     
@@ -2911,58 +2911,11 @@
 get(
-KeyFactory.createKey(Trip.KIND, 5910974510923776L)
+key
 );%0A 
@@ -3072,16 +3072,27 @@
 (e).log(
+%0A          
 %22Could n
@@ -3107,35 +3107,74 @@
 eve 
-stops due to faulty key%22, e
+Entity for Trip with key %25s while trying to update the stops%22, key
 );%0A 

address comments