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.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Transaction;
import com.google.appengine.api.datastore.TransactionOptions;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.common.collect.Iterables;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gson.Gson;
import com.google.sps.data.AuthInfo;
import com.google.sps.data.Trip;
import com.google.sps.data.TripLocation;
import com.google.sps.util.AuthChecker;
import com.google.sps.util.TripDataConverter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/trips-network")
public class TripsNetworkServlet extends HttpServlet {

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String userEmail = AuthChecker.getUserEmail(response);
    if (userEmail == null) {
      return;
    }

    Query tripQuery = new Query("trip")
        .setFilter(new FilterPredicate(Trip.ENTITY_PROPERTY_PAST_TRIP, FilterOperator.EQUAL, true));

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    PreparedQuery tripResults = datastore.prepare(tripQuery);
    Iterable<Entity> tripEntityIterable = tripResults.asIterable();
    for (Entity entity : tripEntityIterable) {
      Trip trip = TripDataConverter.convertEntityToTrip(entity);
    }
    List<Map<Key, List<TripLocation>>> tripLocationMapList = new ArrayList<>();
    Query tripLocationsQuery = new Query("trip-location")
        .setFilter(new FilterPredicate(TripLocation.ENTITY_PROPERTY_OWNER, FilterOperator.EQUAL, userEmail));

    response.setContentType("application/json");
    Gson gson = new Gson();

  }
}
@@ -1035,24 +1035,75 @@
 .ArrayList;%0A
+import java.util.Arrays;%0Aimport java.util.HashMap;%0A
 import java.
@@ -1975,143 +1975,465 @@
 ();%0A
-    for (Entity entity : tripEntityIterable) %7B%0A      Trip trip = TripDataConverter.convertEntityToTrip(entity);%0A    %7D%0A    List%3CMap%3CKey,
+%0A    Map%3CTrip, List%3CTripLocation%3E%3E tripsResponseMap = new HashMap%3C%3E();%0A    for (Entity entity : tripEntityIterable) %7B%0A      // query and store trip locations under the current trip in the map%0A      Trip trip = TripDataConverter.convertEntityToTrip(entity);%0A      Query tripLocationsQuery = new Query(%22trip-location%22).setAncestor(entity.getKey());%0A      Iterable%3CEntity%3E tripLocationEntityIterable = datastore.prepare(tripLocationsQuery).asIterable();%0A%0A     
  Lis
@@ -2447,18 +2447,16 @@
 ocation%3E
-%3E%3E
  tripLoc
@@ -2460,19 +2460,16 @@
 Location
-Map
 List = n
@@ -2494,250 +2494,499 @@
     
-Query tripLocationsQuery = new Query(%22
+  for (Entity tripEntity : 
 trip
--l
+L
 ocation
-%22)%0A        .setFilter(new FilterPredicate(TripLocation.ENTITY_PROPERTY_OWNER, FilterOperator.EQUAL, userEmail));%0A%0A    response.setContentType(%22application/json%22);%0A    Gson gson = new Gson();%0A
+EntityIterable) %7B%0A        TripLocation location = TripDataConverter.convertEntityToTripLocation(tripEntity);%0A        tripLocationList.add(location);%0A      %7D%0A      tripsResponseMap.put(trip, tripLocationList);%0A    %7D%0A%0A    response.setContentType(%22application/json%22);%0A    Gson gson = new Gson();%0A    String serializedJSON = gson.toJson(tripsResponseMap);%0A    response.getWriter().println(serializedJSON);%0A    response.setStatus(HttpServletResponse.SC_OK);
 %0A  %7D

Add GET for /trips-network endpoint