// 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.servlets;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gson.Gson;
import com.google.sps.data.AuthenticationInformation;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet with a GET handler that returns the login status and information about the user.
 */
@WebServlet("/login-status")
public class LoginStatusServlet extends HttpServlet {
  private UserService userService;

  private static final String HOME_PAGE_URL = "/";
  private static final String LOGIN_PAGE_URL = "/login.html";

  public LoginStatusServlet() {}

  public LoginStatusServlet(UserService userService) {
    this.userService = userService;
  }

  @Override
  public void init() {
    userService = UserServiceFactory.getUserService();
  }

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    boolean loggedIn = userService.isUserLoggedIn();
    AuthenticationInformation authentication;

    if (loggedIn) {
      String logoutUrl = userService.createLogoutURL(LOGIN_PAGE_URL);
      String email = userService.getCurrentUser().getEmail();
      authentication = new AuthenticationInformation(logoutUrl, email);
    } else {
      String loginUrl = userService.createLoginURL(HOME_PAGE_URL);
      authentication = new AuthenticationInformation(loginUrl);
    }

    // Convert the authentication information to JSON.
    String json = convertToJson(authentication);

    // Send the JSON as the response.
    response.setContentType("application/json;");
    response.getWriter().println(json);
  }

  /**
   * Converts an AuthenticationInformation object into a JSON string using the Gson library.
   */
  private static String convertToJson(AuthenticationInformation authenticationInformation) {
    Gson gson = new Gson();
    String json = gson.toJson(authenticationInformation);
    return json;
  }
}

@@ -1200,44 +1200,8 @@
 t %7B%0A
-  private UserService userService;%0A%0A
   pr
@@ -1317,37 +1317,45 @@
 %0A  p
-ublic LoginStatusServlet() %7B%7D
+rivate final UserService userService;
 %0A%0A  
@@ -1380,17 +1380,25 @@
 Servlet(
-U
+) %7B%0A    u
 serServi
@@ -1392,33 +1392,35 @@
     userService 
-u
+= U
 serService) %7B%0A  
@@ -1417,36 +1417,20 @@
 vice
-) %7B%0A    this.userService = u
+Factory.getU
 serS
@@ -1435,16 +1435,18 @@
 rService
+()
 ;%0A  %7D%0A%0A 
@@ -1450,46 +1450,35 @@
 %0A%0A  
-@Override%0A  public void init() %7B%0A    u
+public LoginStatusServlet(U
 serS
@@ -1476,35 +1476,33 @@
 let(UserService 
-= U
+u
 serServiceFactor
@@ -1491,36 +1491,52 @@
  userService
-Factory.getU
+) %7B%0A    this.userService = u
 serService()
@@ -1525,34 +1525,32 @@
 ce = userService
-()
 ;%0A  %7D%0A%0A  @Overri

Mark user service final in login status servlet.