// 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 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 User type, containing an optional 
 * email String and a list of trip IDs the user can have. 
 * Users that are not logged in can only have one trip ID.
 */
public final class User {

  private final String email;
  private final String userId;
  private final ArrayList<String> tripIds;

  public static final String DATASTORE_ENTITY_KIND = "user";

  public User(String email, String userId, Collection<String> tripIds) {
    this.email = checkNotNull(email, "email");
    this.userId = checkNotNull(userId, "userId");
    checkNotNull(tripIds, "tripIds");
    this.tripIds = new ArrayList<>(tripIds);
  }

  public String getEmail() {
    return this.email;
  }

  public List<String> getTripIds() {
    return Collections.unmodifiableList(this.tripIds);
  }

  public String getUserId() {
    return this.userId;
  }

  public static User fromEntity(Entity userEntity) {
    checkNotNull(userEntity, "User entity is null");
    checkArgument(userEntity.getKind().equals(DATASTORE_ENTITY_KIND),
      "Wrong entity kind. Expected %s, received %s", DATASTORE_ENTITY_KIND, userEntity.getKind());
    String email = checkNotNull((String) userEntity.getProperty("email"),
      "User entity does not contain an email");
    String userId = checkNotNull((String) userEntity.getProperty("userId"),
      "User entity does not contain a user Id");
    ArrayList<String> tripIds =
      checkNotNull((ArrayList<String>) userEntity.getProperty("tripIds"),
        "User entity does not contain trip Ids");
    return new User(email, userId, tripIds);
  }
} 
@@ -662,16 +662,117 @@
 Entity;%0A
+import com.google.appengine.api.datastore.Key;%0Aimport com.google.appengine.api.datastore.KeyFactory;%0A
 import s
@@ -1267,22 +1267,25 @@
  String 
-userId
+keyString
 ;%0A  priv
@@ -1419,22 +1419,25 @@
  String 
-userId
+keyString
 , Collec
@@ -1516,22 +1516,25 @@
    this.
-userId
+keyString
  = check
@@ -1545,23 +1545,29 @@
 ull(
-userId, %22userId
+keyString, %22keyString
 %22);%0A
@@ -1831,14 +1831,17 @@
  get
-UserId
+keyString
 () %7B
@@ -1857,22 +1857,105 @@
 rn this.
-userId
+keyString;%0A  %7D%0A%0A  public Key getKey() %7B%0A    return KeyFactory.stringToKey(this.keyString)
 ;%0A  %7D%0A%0A 
@@ -2361,22 +2361,25 @@
  String 
-userId
+keyString
  = check
@@ -2419,22 +2419,25 @@
 operty(%22
-userId
+keyString
 %22),%0A    
@@ -2474,15 +2474,18 @@
 n a 
-user Id
+key string
 %22);%0A
@@ -2671,14 +2671,17 @@
 il, 
-userId
+keyString
 , tr

Allow key retrieval for user