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.FirebaseToken;
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;

/** Servlet that creates a new class Datastore. */
@WebServlet("/enterqueue")
public final class EnterQueue 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 doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Get the input from the form.
    // navigate to /_ah/admin to view Datastore

    datastore = DatastoreServiceFactory.getDatastoreService();
    System.setProperty(
        DatastoreServiceConfig.DATASTORE_EMPTY_LIST_SUPPORT, Boolean.TRUE.toString());

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

      String idToken = request.getParameter("idToken");
      FirebaseToken decodedToken = authInstance.verifyIdToken(idToken);

      ArrayList<String> updatedQueue = (ArrayList) classEntity.getProperty("studentQueue");
      updatedQueue.add(decodedToken.getUid());
      classEntity.setProperty("studentQueue", updatedQueue);

      datastore.put(classEntity);

      if (request.getParameter("enterTA") == null) {
        response.sendRedirect("/queue/student.html?classCode=" + classCode);
      } else {
        response.sendRedirect("/queue/ta.html?classCode=" + classCode);
      }

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

@@ -1823,24 +1823,80 @@
 e%22).trim();%0A
+%0A      if (request.getParameter(%22enterTA%22) == null) %7B%0A  
       Key cl
@@ -1945,16 +1945,18 @@
 ;%0A      
+  
 Entity c
@@ -1990,24 +1990,26 @@
 classKey);%0A%0A
+  
       String
@@ -2058,16 +2058,18 @@
 ;%0A      
+  
 Firebase
@@ -2133,16 +2133,18 @@
 %0A%0A      
+ 
+ 
 ArrayLis
@@ -2231,49 +2231,126 @@
     
-updatedQueue.add(decodedToken.getUid());%0A
+  String uID = decodedToken.getUid();%0A        if (!updatedQueue.contains(uID)) %7B%0A          updatedQueue.add(uID);%0A    
     
@@ -2409,24 +2409,28 @@
 ue);%0A%0A      
+    
 datastore.pu
@@ -2449,61 +2449,17 @@
 y);%0A
-%0A
       
-if (request.getParameter(%22enterTA%22) == null) %7B
+  %7D
 %0A   

Fix adding duplicates in queue