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.sps.ApplicationDefaults;
import com.google.sps.authentication.Authenticator;
import com.google.sps.firebase.FirebaseAppManager;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
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;

// Once class owner submits a roster, add or update those student users class registration
@WebServlet("/submit-roster")
public class SubmitRoster extends HttpServlet {

  private FirebaseAuth authInstance;
  private Authenticator auth;

  // Get the current session
  @Override
  public void init(ServletConfig config) throws ServletException {
    try {
      authInstance = FirebaseAuth.getInstance(FirebaseAppManager.getApp());
      auth = new Authenticator();
    } catch (IOException e) {
      throw new ServletException(e);
    }
  }

  // Add a student user to the datastore
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    String rosterNames = request.getParameter("roster").trim();
    String idToken = request.getParameter("idToken");

    // Split the emails and collapse whitespaces
    List<String> allClassEmails = Arrays.asList(rosterNames.split("\\s*,\\s*"));

    // Find the corresponding class Key
    String classCode = request.getParameter("classCode").trim();
    Key classKey = KeyFactory.stringToKey(classCode);

    if (auth.verifyTaOrOwner(idToken, classKey)) {
      for (String email : allClassEmails) {
        // Look for the student in the user datastore
        PreparedQuery queryUser =
            datastore.prepare(
                new Query("User")
                    .setFilter(new FilterPredicate("userEmail", FilterOperator.EQUAL, email)));

        Entity user;

        // If the student user entity doesnt exist yet, create one
        if (queryUser.countEntities() == 0) {

          List<Key> regClassesList = Arrays.asList(classKey);

          user = new Entity("User");
          user.setProperty("userEmail", email);
          user.setProperty("registeredClasses", regClassesList);
          user.setProperty("ownedClasses", Collections.emptyList());
          user.setProperty("taClasses", Collections.emptyList());

          datastore.put(user);
        } else {
          // If student already exists, update their registered class list
          user = queryUser.asSingleEntity();
          List<Key> regClassesList = (List<Key>) user.getProperty("registeredClasses");

          // Do not add a class that is already in the registered list
          if (!regClassesList.contains(classKey)) {
            regClassesList.add(classKey);
            user.setProperty("registeredClasses", regClassesList);

            datastore.put(user);
          }
        }
      }

      // Redirect to the class dashboard page
      response.sendRedirect(ApplicationDefaults.DASHBOARD + classCode);
    } else {
      response.sendError(HttpServletResponse.SC_FORBIDDEN);
    }

    // for (String email : allClassEmails) {
    //   // Look for the student in the user datastore
    //   PreparedQuery queryUser =
    //       datastore.prepare(
    //           new Query("User")
    //               .setFilter(new FilterPredicate("userEmail", FilterOperator.EQUAL, email)));

    //   Entity user;

    //   // If the student user entity doesnt exist yet, create one
    //   if (queryUser.countEntities() == 0) {

    //     List<Key> regClassesList = Arrays.asList(classKey);

    //     user = new Entity("User");
    //     user.setProperty("userEmail", email);
    //     user.setProperty("registeredClasses", regClassesList);
    //     user.setProperty("ownedClasses", Collections.emptyList());
    //     user.setProperty("taClasses", Collections.emptyList());

    //     datastore.put(user);
    //   } else {
    //     // If student already exists, update their registered class list
    //     user = queryUser.asSingleEntity();
    //     List<Key> regClassesList = (List<Key>) user.getProperty("registeredClasses");

    //     // Do not add a class that is already in the registered list
    //     if (!regClassesList.contains(classKey)) {
    //       regClassesList.add(classKey);
    //       user.setProperty("registeredClasses", regClassesList);

    //       datastore.put(user);
    //     }
    //   }
    // }

    // // Redirect to the class dashboard page
    // response.sendRedirect(ApplicationDefaults.DASHBOARD + classCode);
  }
}

@@ -3870,1490 +3870,8 @@
    %7D
-%0A%0A    // for (String email : allClassEmails) %7B%0A    //   // Look for the student in the user datastore%0A    //   PreparedQuery queryUser =%0A    //       datastore.prepare(%0A    //           new Query(%22User%22)%0A    //               .setFilter(new FilterPredicate(%22userEmail%22, FilterOperator.EQUAL, email)));%0A%0A    //   Entity user;%0A%0A    //   // If the student user entity doesnt exist yet, create one%0A    //   if (queryUser.countEntities() == 0) %7B%0A%0A    //     List%3CKey%3E regClassesList = Arrays.asList(classKey);%0A%0A    //     user = new Entity(%22User%22);%0A    //     user.setProperty(%22userEmail%22, email);%0A    //     user.setProperty(%22registeredClasses%22, regClassesList);%0A    //     user.setProperty(%22ownedClasses%22, Collections.emptyList());%0A    //     user.setProperty(%22taClasses%22, Collections.emptyList());%0A%0A    //     datastore.put(user);%0A    //   %7D else %7B%0A    //     // If student already exists, update their registered class list%0A    //     user = queryUser.asSingleEntity();%0A    //     List%3CKey%3E regClassesList = (List%3CKey%3E) user.getProperty(%22registeredClasses%22);%0A%0A    //     // Do not add a class that is already in the registered list%0A    //     if (!regClassesList.contains(classKey)) %7B%0A    //       regClassesList.add(classKey);%0A    //       user.setProperty(%22registeredClasses%22, regClassesList);%0A%0A    //       datastore.put(user);%0A    //     %7D%0A    //   %7D%0A    // %7D%0A%0A    // // Redirect to the class dashboard page%0A    // response.sendRedirect(ApplicationDefaults.DASHBOARD + classCode);
 %0A  %7D

Delete commented code.