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

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

    } 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

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

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

@@ -1773,19 +1773,22 @@
 %7B%0A      
-Key
+String
  classCo
@@ -1796,31 +1796,8 @@
 e = 
-KeyFactory.stringToKey(
 requ
@@ -1832,16 +1832,71 @@
 ).trim()
+;%0A      Key classKey = KeyFactory.stringToKey(classCode
 );%0A     
@@ -1936,20 +1936,19 @@
 et(class
-Code
+Key
 );%0A%0A    
@@ -2269,24 +2269,24 @@
 tedQueue);%0A%0A
-
       datast
@@ -2307,16 +2307,243 @@
 ntity);%0A
+%0A      if (request.getParameter(%22enterTA%22) == null) %7B%0A        response.sendRedirect(%22/queue/student.html?classCode=%22 + classCode);%0A      %7D else %7B%0A        response.sendRedirect(%22/queue/ta.html?classCode=%22 + classCode);%0A      %7D%0A%0A
     %7D ca

Add redirect to queue with class code url param