package com.google.sps.servlets;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceConfig;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthException;
import com.google.firebase.auth.UserRecord;
import com.google.gson.Gson;
import com.google.sps.firebase.FirebaseAppManager;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/get-queue")
public class GetQueue extends HttpServlet {
  FirebaseAuth authInstance;
  DatastoreService datastore;

  @Override
  public void init(ServletConfig config) throws ServletException {
    try {
      authInstance = FirebaseAuth.getInstance(FirebaseAppManager.getApp());
    } catch (IOException e) {
      throw new ServletException(e);
    }
  }

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    datastore = DatastoreServiceFactory.getDatastoreService();
    System.setProperty(
        DatastoreServiceConfig.DATASTORE_EMPTY_LIST_SUPPORT, Boolean.TRUE.toString());

    try {
      // Retrive class entity
      String classCode = request.getParameter("classCode").trim();
      Key classKey = KeyFactory.stringToKey(classCode);
      Entity classEntity = datastore.get(classKey);

      // Get queue
      ArrayList<String> uidQueue = (ArrayList) classEntity.getProperty("studentQueue");

      // Reconstruct queue using names
      ArrayList<String> queue = new ArrayList<String>();
      for (String uid : uidQueue) {
        UserRecord userRecord = authInstance.getUser(uid);
        String studentName = userRecord.getEmail();
        queue.add(studentName);
      }

      response.setContentType("application/json;");
      response.getWriter().print(jsonQueue(queue));

    } catch (EntityNotFoundException e) {
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
    } catch (IllegalArgumentException e) {
      response.sendError(HttpServletResponse.SC_BAD_REQUEST);
    } catch (FirebaseAuthException e) {
      response.sendError(HttpServletResponse.SC_FORBIDDEN);
    }
  }

  private String jsonQueue(ArrayList<String> queue) {
    Gson gson = new Gson();
    return gson.toJson(queue);
  }
}

@@ -1099,16 +1099,29 @@
 tastore;
+%0A  Gson gson;
 %0A%0A  @Ove
@@ -1279,16 +1279,41 @@
 App());%0A
+      gson = new Gson();%0A
     %7D ca
@@ -2361,25 +2361,27 @@
 ).print(
-jsonQueue
+gson.toJson
 (queue))
@@ -2703,125 +2703,7 @@
 %0A  %7D
-%0A%0A  private String jsonQueue(ArrayList%3CString%3E queue) %7B%0A    Gson gson = new Gson();%0A    return gson.toJson(queue);%0A  %7D
 %0A%7D%0A

Refactor for easier testing