package com.google.sps.servlets;

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.FetchOptions;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.google.gson.Gson;
import com.google.sps.data.Attendee;
import java.util.Date;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.sps.data.DatastoreClientInterface;
import com.google.sps.data.DatastoreClient;
import com.google.sps.data.AttendeeInterface;
import com.google.sps.data.Attendee;

/** 
 * Servlet that checks if given session Id exists and screen name is unique 
 * before storing users data in datastore and redirected them to the in
 * session webpage.  
 */
@WebServlet("/join-session")
public class JoinSessionServlet extends HttpServlet {
  private final String SCREEN_NAME_PARAM_KEY = "name";
  private final String SESSION_ID_PARAM_KEY = "session-id";

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws IOException {
      DatastoreClientInterface datastoreClient = new DatastoreClient();
      String screenName = request.getParameter(SCREEN_NAME_PARAM_KEY);
      String sessionId = request.getParameter(SESSION_ID_PARAM_KEY);
      Date timeLastPolled = new Date();
      List<AttendeeInterface> attendeeList = 
        datastoreClient.getAttendeesInSession(sessionId);
      if (attendeeList.isEmpty()) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
      }
      else {
          if (alreadyExists(screenName, attendeeList)) {
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            return;
          }
        AttendeeInterface attendee = 
          new Attendee(sessionId, screenName, timeLastPolled);
        datastoreClient.insertOrUpdateAttendee(attendee);
        response.setStatus(HttpServletResponse.SC_OK);
        response.sendRedirect("/session.html?session-id=" + sessionId + 
          "&name=" + screenName); 
      }
    }
    
    // Checks if a screenName already exists in a session.
    private boolean alreadyExists
      (String screenName, List<AttendeeInterface> attendeeList) {
        boolean found = false;
        for (int i = 0; i<attendeeList.size(); i++) {
          if (attendeeList.get(i).getScreenName().equals(screenName)) {
           found = true;
          }
        }
        return found;
    }
}       
@@ -1219,32 +1219,39 @@
 let %7B%0A  private 
+static 
 final String SCR
@@ -1289,16 +1289,23 @@
 private 
+static 
 final St
@@ -2533,16 +2533,18 @@
 s%0A      
+  
 (String 
@@ -2591,24 +2591,26 @@
 ndeeList) %7B%0A
+  
         bool
@@ -2636,16 +2636,18 @@
         
+  
 for (int
@@ -2690,24 +2690,26 @@
 %7B%0A          
+ 
+ 
 if (attendee
@@ -2769,16 +2769,19 @@
         
+   
 found = 
@@ -2786,16 +2786,30 @@
 = true;%0A
+            %7D%0A
         
@@ -2812,26 +2812,18 @@
       %7D%0A
+
   
-      %7D%0A
         

Fix comments