// 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 = UserServiceFactory.getUserService();

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

  @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;
  }
}

@@ -1233,48 +1233,10 @@
 vice
- = UserServiceFactory.getUserService()
 ;%0A
+
 %0A  p
@@ -1283,16 +1283,16 @@
  = %22/%22;%0A
-
   privat
@@ -1346,16 +1346,241 @@
 html%22;%0A%0A
+  public LoginStatusServlet() %7B%7D%0A%0A  public LoginStatusServlet(UserService userService) %7B%0A    this.userService = userService;%0A  %7D%0A%0A  @Override%0A  public void init() %7B%0A    userService = UserServiceFactory.getUserService();%0A  %7D%0A%0A
   @Overr

Refactor login status servlet for dependency injection.