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.inject.Singleton;
import com.google.tripmeout.frontend.TripModel;
import com.google.tripmeout.frontend.error.TripAlreadyExistsException;
import com.google.tripmeout.frontend.servlets.ServletUtil;
import com.google.tripmeout.frontend.storage.TripStorage;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.Optional;
import java.util.UUID;
import javax.inject.Inject;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Singleton
public class TripParentServlet extends HttpServlet {
  private static final FluentLogger logger = FluentLogger.forEnclosingClass();

  static final String STUB_USER_ID = "fakeUserId";

  private final Gson gson;
  private final TripStorage storage;

  @Inject
  public TripParentServlet(TripStorage storage, Gson gson) {
    this.gson = gson;
    this.storage = storage;
  }

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String userId = getUserId();
    response.setContentType("application/json");
    response.setStatus(HttpServletResponse.SC_OK);
    PrintWriter writer = response.getWriter();
    writer.print(gson.toJson(storage.getAllUserTrips(userId)));
    writer.flush();
    writer.close();
  }

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    logger.atInfo().log("received doPost");
    try {
      Optional<TripModel> requestTrip =
          ServletUtil.extractFromRequestBody(request, gson, TripModel.class);

      if (requestTrip.isPresent()) {
        try {
          TripModel resolvedTrip = resolveDefaults(requestTrip.get());
          storage.addTrip(resolvedTrip);
          response.setContentType("application/json");
          PrintWriter writer = response.getWriter();
          writer.println(gson.toJson((resolvedTrip)));
          writer.flush();
          writer.close();
        } catch (TripAlreadyExistsException e) {
          response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }

      } else {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
      }
    } catch (IOException e) {
      logger.atWarning().withCause(e).log("Error serving post request");
      response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
  }

  private String getUserId() {
    // TODO(issues/66): Gte userId from Userservice
    return STUB_USER_ID;
  }

  private TripModel resolveDefaults(TripModel trip) {
    return trip.toBuilder().setId(UUID.randomUUID().toString()).setUserId(getUserId()).build();
  }
}

@@ -1798,47 +1798,8 @@
 );%0A%0A
-      if (requestTrip.isPresent()) %7B%0A  
     
@@ -1802,26 +1802,24 @@
       try %7B%0A
-  
         Trip
@@ -1883,18 +1883,16 @@
         
-  
 storage.
@@ -1914,34 +1914,32 @@
 dTrip);%0A        
- 
- 
 response.setCont
@@ -1963,26 +1963,24 @@
 ion/json%22);%0A
-  
         Prin
@@ -2022,26 +2022,24 @@
 ();%0A        
-  
 writer.print
@@ -2075,26 +2075,24 @@
 ));%0A        
- 
- 
 writer.flush
@@ -2091,26 +2091,24 @@
 er.flush();%0A
-  
         writ
@@ -2115,26 +2115,24 @@
 er.close();%0A
-  
       %7D catc
@@ -2162,26 +2162,24 @@
 eption e) %7B%0A
-  
         resp
@@ -2240,30 +2240,47 @@
     
-  %7D%0A
+%7D
 %0A    
-  %7D else
+%7D catch (JsonParseException e)
  %7B%0A
-  
     
@@ -2333,32 +2333,24 @@
 D_REQUEST);%0A
-      %7D%0A
     %7D catch 

fix TripParentServlet error handling