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.EmbeddedEntity;
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 {
  private FirebaseAuth authInstance;
  private DatastoreService datastore;
  private Gson gson;

  @Override
  public void init(ServletConfig config) throws ServletException {
    try {
      authInstance = FirebaseAuth.getInstance(FirebaseAppManager.getApp());
      gson = new Gson();
    } 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 {
      // Retrieve class entity
      String classCode = request.getParameter("classCode").trim();
      Key classKey = KeyFactory.stringToKey(classCode);
      Entity classEntity = datastore.get(classKey);

      // Get queue
      ArrayList<EmbeddedEntity> entityQueue =
          (ArrayList<EmbeddedEntity>) classEntity.getProperty("studentQueue");

      // Reconstruct queue using names
      ArrayList<String> queue = new ArrayList<String>();
      for (EmbeddedEntity elem : entityQueue) {
        String uid = elem.getProperties().keySet().stream().findFirst().orElse(null);
        UserRecord userRecord = authInstance.getUser(uid);
        String studentName = userRecord.getEmail();
        queue.add(studentName);
      }

      response.setContentType("application/json;");
      response.getWriter().print(gson.toJson(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);
    }
  }
}

@@ -721,16 +721,52 @@
 anager;%0A
+import com.google.sps.queue.Queue;%0A%0A
 import j
@@ -809,24 +809,52 @@
 .ArrayList;%0A
+import java.util.Optional;%0A%0A
 import javax
@@ -1918,24 +1918,80 @@
 e%22).trim();%0A
+      String idToken = request.getParameter(%22idToken%22);%0A
       Key cl
@@ -2083,24 +2083,91 @@
 classKey);%0A%0A
+      String TaID = authInstance.verifyIdToken(idToken).getUid();%0A%0A
       // Get
@@ -2681,16 +2681,621 @@
      %7D%0A%0A
+      Optional%3CQueue.Helping%3E beingHelpedEntity = ((EmbeddedEntity) classEntity.getProperty(%22beingHelped%22)).getProperties().entrySet().stream()%0A            .filter(entry -%3E ((EmbeddedEntity) entry.getValue()).getProperty(%22taID%22).equals(TaID))%0A            .map(entry -%3E %7B%0A            try %7B%0A              return new Queue.Helping(authInstance.getUser(entry.getKey()).getEmail(),%0A                  (String) ((EmbeddedEntity) entry.getValue()).getProperty(%22workspaceID%22));%0A            %7D catch (FirebaseAuthException e) %7B%0A              throw new RuntimeException(e);%0A            %7D%0A          %7D).findFirst();%0A%0A%0A%0A
       re
@@ -3383,21 +3383,64 @@
 .toJson(
-queue
+new Queue(queue, beingHelpedEntity.orElse(null))
 ));%0A%0A   

Update get Queue to get being helped.