package com.google.sps.servlets;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
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.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.EntityNotFoundException;

import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import com.google.sps.data.User;
import java.util.Map;
import java.util.HashMap;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

    UserService userService = UserServiceFactory.getUserService();

    String urlToRedirect = "/";
    String userId = "null";
    String logoutUrl = "null";
    String loginUrl = "null";
    String userEmail = "null";

    // Boolean to update User's main page 
    Boolean showNewTab = false;

    User user;
    if (userService.isUserLoggedIn()) {
      userId = userService.getCurrentUser().getUserId();
      userEmail = userService.getCurrentUser().getEmail();
      logoutUrl = userService.createLogoutURL(urlToRedirect);
      showNewTab = true;
    } else {
      loginUrl = userService.createLoginURL(urlToRedirect);
    }

    user = new User(userId, userEmail);

    if ((userId != "null") && (!isUserInDatastore(user))) {
      storeUserToDatastore(user);
    }

    // Aggregate response information
    Map<String, Object> jsonInfo = new HashMap<>();
    jsonInfo.put("userInfo", user);
    jsonInfo.put("showTabStatus", showNewTab);
    jsonInfo.put("logoutUrl", logoutUrl);
    jsonInfo.put("loginUrl", loginUrl);

    response.setContentType("application/json;");
    response.getWriter().println(new Gson().toJson(jsonInfo));
  }

  public boolean isUserInDatastore(User user) {

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Key userKey = KeyFactory.createKey("User", user.getUserId());
    Entity userEntity;

    // Check if user is in datastore
    try {
      userEntity = datastore.get(userKey);
      user.setUserKey(KeyFactory.keyToString(userKey));
      return true;
    } catch (EntityNotFoundException e) {
      return false;
    }
  }

  public void storeUserToDatastore(User user) {

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    // Create new user entity 
    Entity newEntity = user.createEntity();

    // Create User's key and add to object
    Key key = newEntity.getKey(); 
    String userKeyStr = KeyFactory.keyToString(key);
    user.setUserKey(userKeyStr);

    datastore.put(newEntity);
  }

}
@@ -979,17 +979,16 @@
 ption %7B%0A
-%0A
     User
@@ -1662,17 +1662,16 @@
 %0A    if 
-(
 (userId 
@@ -1683,14 +1683,12 @@
 ull%22
-)
  && 
-(
 !isU
@@ -1708,17 +1708,16 @@
 e(user))
-)
  %7B%0A     
@@ -2166,33 +2166,32 @@
 re(User user) %7B%0A
-%0A
     DatastoreSer
@@ -2348,45 +2348,8 @@
 y;%0A%0A
-    // Check if user is in datastore%0A
     
@@ -2593,17 +2593,16 @@
 user) %7B%0A
-%0A
     Data
@@ -2678,39 +2678,8 @@
 );%0A%0A
-    // Create new user entity %0A
     
@@ -2721,52 +2721,8 @@
 y();
-%0A%0A    // Create User's key and add to object
 %0A   
@@ -2870,14 +2870,13 @@
 ntity);%0A
+
   %7D%0A
-%0A
 %7D

Remove unnecessary comment