// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.maps;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceConfig;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.maps.model.PlaceType;
import com.google.sps.data.Trip;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet that returns all PlaceTypes from Places API. Stores which placeTypes, or interests, user
 * selects in datastore with their specific Trip Entity.
 */
@WebServlet("/api/places")
public final class PlacesServlet extends HttpServlet {

  /** {@link Type} of an {@link ArrayList} containing {@link String}, for gson decoding. */
  private static final Type ARRAYLIST_STRING = new TypeToken<ArrayList<String>>() {}.getType();

  private final Gson gson = new Gson();
  private final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

  /* Distinguishes null values from empty lists in DatastoreService. */
  @Override
  public void init() {
    System.setProperty(
        DatastoreServiceConfig.DATASTORE_EMPTY_LIST_SUPPORT, Boolean.TRUE.toString());
  }

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    ArrayList<String> places = new ArrayList<>();
    for (PlaceType location : PlaceType.values()) {
      String place = formatLocation(location);
      places.add(place);
    }
    response.setContentType("application/json;");
    response.getWriter().println(gson.toJson(places));
  }

  /**
   * Format string by capitalizing and adding spaces in-between words.
   *
   * @param location is a PlaceType that indicates a type of location/interest.
   * @return is the modified location name.
   */
  private String formatLocation(PlaceType location) {
    String place = location.toString();
    place = place.substring(0, 1).toUpperCase() + place.substring(1);
    place = place.replace('_', ' ');
    return place;
  }

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String tripKeyString = request.getParameter("tripKey");
    ArrayList<String> interests = gson.fromJson(request.getReader(), ARRAYLIST_STRING);
    Trip trip = Trip.getTrip(datastore, tripKeyString);
    if (trip == null) {
      response.setStatus(response.SC_NOT_FOUND);
      return;
    }
    trip.setInterests(interests);
    datastore.put(trip.toEntity());
  }
}

@@ -1028,24 +1028,105 @@
 .ArrayList;%0A
+import java.util.Arrays;%0Aimport java.util.Collections;%0Aimport java.util.HashSet;%0A
 import javax
@@ -1854,24 +1854,1022 @@
 reService();
+%0A  private final HashSet%3CString%3E nonInterests =%0A      new HashSet%3CString%3E(%0A          Arrays.asList(%0A              %22accounting%22,%0A              %22atm%22,%0A              %22cemetery%22,%0A              %22courthouse%22,%0A              %22dentist%22,%0A              %22electrician%22,%0A              %22electronics_store%22,%0A              %22fire_station%22,%0A              %22funeral_home%22,%0A              %22hardware_store%22,%0A              %22home_goods_store%22,%0A              %22insurance_agency%22,%0A              %22hospital%22,%0A              %22lawyer%22,%0A              %22locksmith%22,%0A              %22moving_company%22,%0A              %22painter%22,%0A              %22physiotherapist%22,%0A              %22plumber%22,%0A              %22police%22,%0A              %22primary_school%22,%0A              %22real_estate_agency%22,%0A              %22roofing_contractor%22,%0A              %22secondary_school%22,%0A              %22storage%22,%0A              %22taxi_stand%22));%0A  private final HashSet%3CString%3E customInterests =%0A      new HashSet%3CString%3E(Arrays.asList(%22animals%22, %22fashion%22, %22nature%22, %22night_life%22));
 %0A%0A  /* Disti

Add custom list of noninterests and additional interests