// Copyright 2020 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.step.snippet.servlets;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** Servelet that renders user setting page */
@WebServlet("/user")
public class UserSettingServlet extends HttpServlet {
  private static final String WEBSITE_PARAMETER = "website";
  private static final String LANGUAGE_PARAMETER = "language";
  private static final String ID_PARAMETER = "id";
  private static final String USER_PARAMETER = "UserInfo";
  private String referer = "";

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
    // Forward the request to the template (which is a servlet itself).
    request.getRequestDispatcher("WEB-INF/templates/user_dashboard.jsp").forward(request, response);
    referer = request.getHeader("referer");
  }

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    UserService userService = UserServiceFactory.getUserService();
    if (!userService.isUserLoggedIn()) {
      response.sendRedirect("/home");
      return;
    }

    String website = request.getParameter(WEBSITE_PARAMETER);
    String language = request.getParameter(LANGUAGE_PARAMETER);
    String id = userService.getCurrentUser().getUserId();

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Entity entity = new Entity(USER_PARAMETER, id);
    entity.setProperty(ID_PARAMETER, id);
    entity.setProperty(WEBSITE_PARAMETER, website);
    entity.setProperty(LANGUAGE_PARAMETER, language);
    datastore.put(entity);
    response.sendRedirect(referer);
  }
}

@@ -802,16 +802,129 @@
 Entity;%0A
+import com.google.appengine.api.datastore.Query;%0Aimport com.google.appengine.api.datastore.Query.FilterOperator;%0A
 import c
@@ -1565,16 +1565,73 @@
 = %22id%22;%0A
+  private static final String EMAIL_PARAMETER = %22email%22;%0A
   privat
@@ -2347,16 +2347,96 @@
 n;%0A    %7D
+%0A    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
 %0A%0A    St
@@ -2617,124 +2617,373 @@
 ();%0A
-%0A
     
-DatastoreService datastore = DatastoreServiceFactory.getDatastoreService
+Query.FilterPredicate filterUser =%0A        new Query.FilterPredicate(ID_PARAMETER, FilterOperator.EQUAL, id);%0A    Query queryUser = new Query(USER_PARAMETER).setFilter(filterUser);%0A    Entity userEntity = datastore.prepare(queryUser).asSingleEntity
 ();%0A
+%0A
     
-Entity entity = new Entity(USER
+if (userEntity == null) %7B%0A      userEntity = new Entity(USER_PARAMETER);%0A      userEntity.setProperty(ID
 _PAR
@@ -2991,33 +2991,39 @@
 METER, id);%0A    
-e
+  userE
 ntity.setPropert
@@ -3016,34 +3016,37 @@
 ity.setProperty(
-ID
+EMAIL
 _PARAMETER, id);
@@ -3045,18 +3045,65 @@
 ER, 
-id);%0A    e
+userService.getCurrentUser().getEmail());%0A    %7D%0A    userE
 ntit
@@ -3149,17 +3149,21 @@
 e);%0A    
-e
+userE
 ntity.se
@@ -3221,17 +3221,21 @@
 ore.put(
-e
+userE
 ntity);%0A

add filter for existing users