package com.google.tripmeout.frontend.serialization;

import com.google.appengine.repackaged.com.google.api.client.json.Json;
import com.google.common.base.Strings;
import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import com.google.tripmeout.frontend.PlaceVisitModel;
import java.io.IOException;

public class GsonPlaceVisitTypeAdapter extends TypeAdapter<PlaceVisitModel> {
  private static final String PLACE_VISIT_MODEL_PLACE_ID_JSON_FIELD_NAME = "placeId";
  private static final String PLACE_VISIT_MODEL_TRIP_ID_JSON_FIELD_NAME = "tripId";
  private static final String PLACE_VISIT_MODEL_NAME_JSON_FIELD_NAME = "name";
  private static final String PLACE_VISIT_MODEL_USER_MARK_JSON_FIELD_NAME = "userMark";
  private static final String PLACE_VISIT_MODEL_LATITUDE_JSON_FIELD_NAME = "latitude";
  private static final String PLACE_VISIT_MODEL_LONGITUDE_JSON_FIELD_NAME = "longitude";

  @Override
  public PlaceVisitModel read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
      reader.nextNull();
      return null;
    }
    PlaceVisitModel.Builder placeBuilder = PlaceVisitModel.builder();
    reader.beginObject();
    while (reader.hasNext()) {
      JsonToken token = reader.peek();
      if (token.equals(JsonToken.NAME)) {
        String name = reader.nextName();
        switch (name) {
          case PLACE_VISIT_MODEL_PLACE_ID_JSON_FIELD_NAME:
            placeBuilder.setPlaceId(reader.nextString());
            break;
          case PLACE_VISIT_MODEL_NAME_JSON_FIELD_NAME:
            placeBuilder.setName(reader.nextString());
            break;
          case PLACE_VISIT_MODEL_TRIP_ID_JSON_FIELD_NAME:
            placeBuilder.setTripId(reader.nextString());
            break;
          case PLACE_VISIT_MODEL_USER_MARK_JSON_FIELD_NAME:
            placeBuilder.setUserMark(reader.nextString());
          case PLACE_VISIT_MODEL_LATITUDE_JSON_FIELD_NAME:
            placeBuilder.setLatitude(reader.nextDouble());
            break;
          case PLACE_VISIT_MODEL_LONGITUDE_JSON_FIELD_NAME:
            placeBuilder.setLongitude(reader.nextDouble());
            break;
          default:
            throw new JsonParseException(
                String.format("Unknown field name %s for type Trip Model", name));
        }
      }
    }
    reader.endObject();
    return placeBuilder.build();
  }

  @Override
  public void write(JsonWriter writer, PlaceVisitModel place) throws IOException {
    if (place == null) {
      writer.nullValue();
      return;
    }
    writer.beginObject();
    writeUnlessNullOrEmpty(writer, PLACE_VISIT_MODEL_PLACE_ID_JSON_FIELD_NAME, place.placeId());
    writeUnlessNullOrEmpty(writer, PLACE_VISIT_MODEL_NAME_JSON_FIELD_NAME, place.name());
    writeUnlessNullOrEmpty(writer, PLACE_VISIT_MODEL_TRIP_ID_JSON_FIELD_NAME, place.tripId());
    writeUnlessNullOrEmpty(writer, PLACE_VISIT_MODEL_USER_MARK_JSON_FIELD_NAME, place.userMark());
    writeUnlessNullOrEmpty(writer, PLACE_VISIT_MODEL_LATITUDE_JSON_FIELD_NAME, place.latitude());
    writeUnlessNullOrEmpty(writer, PLACE_VISIT_MODEL_LONGITUDE_JSON_FIELD_NAME, place.longitude());
    writer.endObject();
  }

  private static void writeUnlessNullOrEmpty(JsonWriter writer, String name, String value)
      throws IOException {
    if (Strings.isNullOrEmpty(value)) {
      writer.name(name);
      writer.value(value);
    }
  }

  private static void writeUnlessNullOrEmpty(JsonWriter writer, String name, double value)
      throws IOException {
    if (Strings.isNullOrEmpty(value)) {
      writer.name(name);
      writer.value(value);
    }
  }
}

@@ -1416,24 +1416,79 @@
 en.NAME)) %7B%0A
+        System.out.println(%22Property %22+reader.peek());%0A
         Stri
@@ -1512,24 +1512,24 @@
 nextName();%0A
-
         swit
@@ -1591,32 +1591,90 @@
 SON_FIELD_NAME:%0A
+           System.out.println(%22Place ID %22+reader.peek());%0A
             plac
@@ -1781,32 +1781,86 @@
 SON_FIELD_NAME:%0A
+           System.out.println(%22Name %22+reader.peek());%0A
             plac
@@ -1967,32 +1967,89 @@
 SON_FIELD_NAME:%0A
+           System.out.println(%22Trip ID %22+reader.peek());%0A
             plac
@@ -2160,32 +2160,91 @@
 SON_FIELD_NAME:%0A
+           System.out.println(%22User Mark %22+reader.peek());%0A
             plac
@@ -2278,32 +2278,51 @@
 .nextString());%0A
+            break;%0A
           case P
@@ -2356,32 +2356,91 @@
 SON_FIELD_NAME:%0A
+            System.out.println(%22Latitude %22+reader.peek());%0A
             plac
@@ -2553,32 +2553,92 @@
 SON_FIELD_NAME:%0A
+            System.out.println(%22Longitude %22+reader.peek());%0A
             plac
@@ -3516,34 +3516,15 @@
 rite
-UnlessNullOrEmpty(writer, 
+r.name(
 PLAC
@@ -3557,26 +3557,82 @@
 N_FIELD_NAME
-, 
+);%0A    System.out.println(%22Ran Writer%22);%0A    writer.value(
 place.latitu
@@ -3651,34 +3651,15 @@
 rite
-UnlessNullOrEmpty(writer, 
+r.name(
 PLAC
@@ -3701,27 +3701,92 @@
 NAME
-, place.longitude()
+);%0A    writer.value(place.longitude());%0A    System.out.println(%22Ran Finished Writer%22
 );%0A 
@@ -4038,226 +4038,9 @@
  %7D%0A%0A
+
   
-private static void writeUnlessNullOrEmpty(JsonWriter writer, String name, double value)%0A      throws IOException %7B%0A    if (Strings.isNullOrEmpty(value)) %7B%0A      writer.name(name);%0A      writer.value(value);%0A    %7D%0A  %7D
 %0A%7D%0A

fix error