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.tripmeout.frontend.PlaceVisitModel;
import com.google.tripmeout.frontend.error.TripMeOutException;
import com.google.tripmeout.frontend.places.PlaceService;
import com.google.tripmeout.frontend.storage.PlaceVisitStorage;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PlaceVisitParentServlet 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 PlaceVisitParentServlet(
      PlaceVisitStorage placeStorage, Gson gson, PlaceService placeService) {
    this.placeStorage = placeStorage;
    this.gson = gson;
    this.placeService = placeService;
  }

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

      placeService.validatePlaceId(place.placesApiPlaceId());

      String tripId = ServletUtil.matchUriOrThrowError(request, TRIP_NAME_PATTERN).group(1);

      PlaceVisitModel newPlace =
          place.toBuilder().setId(UUID.randomUUID().toString()).setTripId(tripId).build();

      placeStorage.addPlaceVisit(newPlace);

      response.setContentType("applciation/json");
      writer.println(gson.toJson(newPlace));
      response.setStatus(HttpServletResponse.SC_CREATED);

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

  @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();
    }
  }
}

@@ -1347,32 +1347,37 @@
 Exception %7B%0A    
+try (
 PrintWriter writ
@@ -1397,33 +1397,25 @@
 .getWriter()
-;%0A    try
+)
  %7B%0A      Pla
@@ -2346,46 +2346,8 @@
 R);%0A
-    %7D finally %7B%0A      writer.close();%0A
     
@@ -2468,16 +2468,21 @@
 n %7B%0A    
+try (
 PrintWri
@@ -2518,17 +2518,9 @@
 er()
-;%0A    try
+)
  %7B%0A 
@@ -3035,46 +3035,8 @@
 R);%0A
-    %7D finally %7B%0A      writer.close();%0A
     

change finally to try-with-resources