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

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import util.UserAuthUtil;

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

  private static final String CONTENT_TYPE_TEXT_HTML = "text/html;";
  private static final String REDIRECT_LINK = "/Login";       // TODO @Vincent

  // Returns whether user is logged in or logged out, with a URL to either login or logout
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType(CONTENT_TYPE_TEXT_HTML);
    PrintWriter out = response.getWriter();
    if (UserAuthUtil.isUserLoggedIn()) {
      out.println("User is logged in. Logout below");
      out.println(UserAuthUtil.getLogoutURL(REDIRECT_LINK));

      if (UserAuthUtil.isUserAuthorized()) {
        out.println("User is authorized");
      } else {
        out.println("User is not authorized");
      }
    } else {
      out.println("User is logged out. Login below");
      out.println(UserAuthUtil.getLoginURL(REDIRECT_LINK));
    }
    out.println("{\"isLoggedOut\":{\"login_url\":\"/loginURL\"},\"isLoggedIn\":{}}");
  }
}
@@ -1551,482 +1551,129 @@
 tln(
-%22User is logged in. Logout below%22);%0A      out.println(UserAuthUtil.getLogoutURL(REDIRECT_LINK));%0A%0A      if (UserAuthUtil.isUserAuthorized()) %7B%0A        out.println(%22User is authorized%22);%0A      %7D else %7B%0A        out.println(%22User is not authorized%22);%0A      %7D%0A    %7D else %7B%0A      out.println(%22User is logged out. Login below%22);%0A      out.println(UserAuthUtil.getLoginURL(REDIRECT_LINK));%0A    %7D%0A    out.println(%22%7B%5C%22isLoggedOut%5C%22:%7B%5C%22login_url%5C%22:%5C%22/loginURL%5C%22%7D,%5C%22isLoggedIn%5C%22:%7B%7D%7D%22);
+UserAuthUtil.getLogoutURL(REDIRECT_LINK));%0A    %7D else %7B%0A      out.println(UserAuthUtil.getLoginURL(REDIRECT_LINK));%0A    %7D
 %0A  %7D

Clean up login servlet output