package com.google.tripmeout.frontend.servlet;

import com.google.common.flogger.FluentLogger;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.maps.GeoApiContext;
import com.google.maps.PlaceDetailsRequest;
import com.google.maps.errors.InvalidRequestException;
import com.google.maps.model.PlaceDetails;
import com.google.tripmeout.frontend.PlaceVisitModel;
import com.google.tripmeout.frontend.error.TripMeOutException;
import com.google.tripmeout.frontend.storage.PlaceVisitStorage;
import com.google.tripmeout.frontend.error.TripMeOutException;
import com.google.gson.JsonParseException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PlaceParentServlet extends HttpServlet {
  private static final FluentLogger logger = FluentLogger.forEnclosingClass();
  private static final Pattern TRIP_NAME_PATTERN = Pattern.compile(".*/trips/([^/]+)/placeVisits");

  private final PlaceVisitStorage placeStorage;
  private final Gson gson;
  //private final PlaceService placeService;

  public PlaceParentServlet(PlaceVisitStorage placeStorage, Gson gson) {
    this.placeStorage = placeStorage;
    this.gson = gson;
    //this.placeService = placeService;
  }

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    try {
      PlaceVisitModel place = ServletUtil.extractFromRequestBody(request.getReader(), gson, PlaceVisitModel.class);
      //validatePlaceId(place.placesApiPlaceId());

      String tripId = ServletUtil.matchUriOrThrowError(request, TRIP_NAME_PATTERN).group(1);
      logger.atInfo().log("finished matching");

      PlaceVisitModel newPlace = PlaceVisitModel.builder()
                                     .setTripId(tripId)
                                     .setPlacesApiPlaceId(place.placesApiPlaceId())
                                     .setUserMark(PlaceVisitModel.UserMark.YES)
                                     .build();

      placeStorage.updateUserMarkOrAddPlaceVisit(newPlace, PlaceVisitModel.UserMark.YES);

      response.setStatus(HttpServletResponse.SC_OK);

    } catch (TripMeOutException e) {
      response.setStatus(e.restStatusCode());
    } catch (JsonParseException e) {
      response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    } catch (InvalidRequestException e) {
      response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    } catch (Exception e) {
      logger.atWarning().log(e.getMessage());
      response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
  }

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    PrintWriter writer = response.getWriter();
    try {
      String tripId = ServletUtil.matchUriOrThrowError(request, TRIP_NAME_PATTERN).group(1);
      List<PlaceVisitModel> nearbyPlaces = placeStorage.getTripPlaceVisits(tripId);
      response.setContentType("application/json");
      writer.println(gson.toJson(nearbyPlaces));
      response.setStatus(HttpServletResponse.SC_OK);
    } catch (TripMeOutException e) {
      response.setStatus(e.restStatusCode());
    } catch (Exception e) {
      response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    } finally {
      writer.close();
    }
  }
}

@@ -733,16 +733,39 @@
 tional;%0A
+import java.util.UUID;%0A
 import j
@@ -1882,24 +1882,25 @@
 atching%22);%0A%0A
+%0A
       PlaceV
@@ -1908,16 +1908,24 @@
 sitModel
+.Builder
  newPlac
@@ -1925,16 +1925,23 @@
 newPlace
+Builder
  = Place
@@ -1961,16 +1961,90 @@
 ilder()%0A
+                                     .setId(UUID.randomUUID().toString())%0A
         
@@ -2258,46 +2258,184 @@
 YES)
+;%0A
 %0A      
-                
+if (place.placeName() != null) %7B%0A          newPlaceBuilder = newPlaceBuilder.setPlaceName(place.placeName());%0A
       
+%7D%0A%0A
       
-   
+PlaceVisitModel newPlace = newPlaceBuilder
 .bui

set id and name fields in doPost