package com.google.sps.servlets.course;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.firebase.auth.FirebaseAuth;
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("/participants")
public class Participants extends HttpServlet {
  private FirebaseAuth authInstance;
  private 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();

    try {
      ArrayList<String> classParticipants = new ArrayList<String>();

      // Retrieve class entity
      String classCode = request.getParameter("classCode").trim();
      Key classKey = KeyFactory.stringToKey(classCode);

      String type = request.getParameter("type"); // Students or TAs

      PreparedQuery results;

      if (!type.equals("student")) {
        // Filter for TAs that teach this class
        Query query =
            new Query("User")
                .setFilter(new FilterPredicate("taClasses", FilterOperator.EQUAL, classKey));
        results = datastore.prepare(query);
      } else {
        // Filter for students that are in this class
        Query query2 =
            new Query("User")
                .setFilter(
                    new FilterPredicate("registeredClasses", FilterOperator.EQUAL, classKey));
        results = datastore.prepare(query2);
      }

      // Store the emails
      for (Entity entity : results.asIterable()) {
        String email = (String) entity.getProperty("userEmail");
        classParticipants.add(email);
      }

      response.setContentType("application/json;");
      Gson gson = new Gson();
      String json = gson.toJson(classParticipants);
      response.getWriter().println(json);

    } catch (IllegalArgumentException e) {
      response.sendError(HttpServletResponse.SC_BAD_REQUEST);
    }

    // try {
    //   ArrayList<String> classTAs = new ArrayList<String>();

    //   // Retrieve class entity
    //   String classCode = request.getParameter("classCode").trim();
    //   Key classKey = KeyFactory.stringToKey(classCode);

    //   // Filter for TAs that teach this class
    //   Query query =
    //       new Query("User")
    //           .setFilter(new FilterPredicate("taClasses", FilterOperator.EQUAL, classKey));
    //   PreparedQuery results = datastore.prepare(query);

    //   // Store the TA emails
    //   for (Entity entity : results.asIterable()) {
    //     String email = (String) entity.getProperty("userEmail");
    //     classTAs.add(email);
    //   }

    //   response.setContentType("application/json;");
    //   Gson gson = new Gson();
    //   String json = gson.toJson(classTAs);
    //   response.getWriter().println(json);

    // } catch (IllegalArgumentException e) {
    //   response.sendError(HttpServletResponse.SC_BAD_REQUEST);
    // }
  }
}

@@ -1782,24 +1782,54 @@
 classCode);%0A
+ %0A      // Get students or TAs
 %0A      Strin
@@ -1871,26 +1871,8 @@
 %22); 
-// Students or TAs
 %0A%0A  
@@ -1932,24 +1932,25 @@
 tudent%22)) %7B%0A
+%0A
         // F
@@ -2190,16 +2190,17 @@
  else %7B%0A
+%0A
         
@@ -2950,1011 +2950,13 @@
 T);%0A
+
     %7D
-%0A%0A    // try %7B%0A    //   ArrayList%3CString%3E classTAs = new ArrayList%3CString%3E();%0A%0A    //   // Retrieve class entity%0A    //   String classCode = request.getParameter(%22classCode%22).trim();%0A    //   Key classKey = KeyFactory.stringToKey(classCode);%0A%0A    //   // Filter for TAs that teach this class%0A    //   Query query =%0A    //       new Query(%22User%22)%0A    //           .setFilter(new FilterPredicate(%22taClasses%22, FilterOperator.EQUAL, classKey));%0A    //   PreparedQuery results = datastore.prepare(query);%0A%0A    //   // Store the TA emails%0A    //   for (Entity entity : results.asIterable()) %7B%0A    //     String email = (String) entity.getProperty(%22userEmail%22);%0A    //     classTAs.add(email);%0A    //   %7D%0A%0A    //   response.setContentType(%22application/json;%22);%0A    //   Gson gson = new Gson();%0A    //   String json = gson.toJson(classTAs);%0A    //   response.getWriter().println(json);%0A%0A    // %7D catch (IllegalArgumentException e) %7B%0A    //   response.sendError(HttpServletResponse.SC_BAD_REQUEST);%0A    // %7D
 %0A  %7D

Delete commented code.