// 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.sps.data;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Collection;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;

/**
 * A class to make a Trip type, containing
 * a specific id, destinations, interests and routes 
 * to be used when updating a trip.
 */
public final class Trip {

  public static final String DATASTORE_ENTITY_KIND = "Trip";

  private final String tripKeyString;
  private final String start;
  private final ArrayList<String> destinations;
  private final ArrayList<String> interests;
  private final ArrayList<String> route;

  public Trip(String tripKeyString, String start, Collection<String> destinations,
              Collection<String> interests, Collection<String> route) {
    this.tripKeyString = checkNotNull(tripKeyString, "tripKeyString");
    this.start = checkNotNull(start, "start");
    checkNotNull(destinations, "destinations");
    checkNotNull(interests, "interests");
    checkNotNull(route, "route");
    this.destinations = new ArrayList<String>(destinations);
    this.interests = new ArrayList<String>(interests);
    this.route = new ArrayList<String>(route);
  }

  public List<String> getInterests() {
    return Collections.unmodifiableList(this.interests);
  }

  public List<String> getRoute() {
    return Collections.unmodifiableList(this.route);
  }

  public List<String> getDestinations() {
    return Collections.unmodifiableList(this.destinations);
  }

  public String getTripId() {
    return this.tripId;
  }

  public String getStart() {
    return this.start;
  }

  public Key getKey() {
    return KeyFactory.stringToKey(tripKeyString);
  }

  public static Trip fromEntity(Entity tripEntity) {
    checkArgument(tripEntity.getKind().equals(DATASTORE_ENTITY_KIND),
      "Wrong Entity kind. Expected %s, received %s", DATASTORE_ENTITY_KIND, tripEntity.getKind());
    String id = checkNotNull((String) tripEntity.getProperty("id"),
      "Trip entity does not contain an id");
    String start = checkNotNull((String) tripEntity.getProperty("start"),
      "Trip entity does not contain a start");
    ArrayList<String> destinations =
      checkNotNull((ArrayList<String>) tripEntity.getProperty("destinations"),
        "Trip entity does not contain destinations");
    ArrayList<String> interests =
      checkNotNull((ArrayList<String>) tripEntity.getProperty("interests"),
        "Trip entity does not contain interests");
    ArrayList<String> route =
      checkNotNull((ArrayList<String>) tripEntity.getProperty("route"),
        "Trip entity does not contain route");
    return new Trip(id, start, destinations, interests, route);
  }
} 
@@ -1254,29 +1254,25 @@
 inal String 
-tripK
+k
 eyString;%0A  
@@ -1455,21 +1455,17 @@
 (String 
-tripK
+k
 eyString
@@ -1594,21 +1594,17 @@
    this.
-tripK
+k
 eyString
@@ -1619,21 +1619,17 @@
 NotNull(
-tripK
+k
 eyString
@@ -1631,21 +1631,17 @@
 tring, %22
-tripK
+k
 eyString
@@ -2307,14 +2307,17 @@
  get
-TripId
+KeyString
 () %7B
@@ -2337,14 +2337,17 @@
 his.
-tripId
+keyString
 ;%0A  
@@ -2468,13 +2468,9 @@
 Key(
-tripK
+k
 eySt

Update variable names to remove trip redundancy