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 static final String SCREEN_NAME_PARAM_KEY = "name";
  private static 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;
    }
}       
@@ -1216,16 +1216,17 @@
 ervlet %7B
+ 
 %0A  priva
@@ -1443,16 +1443,18 @@
 se)%0A    
+  
 throws I
@@ -1466,16 +1466,18 @@
 ption %7B%0A
+  
       Da
@@ -1538,32 +1538,34 @@
 Client();%0A      
+ 
+ 
 String screenNam
@@ -1615,24 +1615,26 @@
 KEY);%0A      
+  
 String sessi
@@ -1690,16 +1690,18 @@
 ;%0A      
+ 
+ 
 Date tim
@@ -1722,24 +1722,26 @@
 new Date();%0A
+  
       List%3CA
@@ -1774,32 +1774,36 @@
 List = %0A        
+ 
+   
 datastoreClient.
@@ -1838,24 +1838,26 @@
 nId);%0A      
+ 
+ 
 if (attendee
@@ -1870,24 +1870,28 @@
 sEmpty()) %7B%0A
+    
         resp
@@ -1942,26 +1942,30 @@
 UND);%0A      
-%7D%0A
+  %7D%0A  
       else %7B
@@ -1971,24 +1971,26 @@
 %7B%0A          
+  
 if (alreadyE
@@ -2020,24 +2020,28 @@
 deeList)) %7B%0A
+    
             
@@ -2102,24 +2102,28 @@
             
+  
+  
 return;%0A    
@@ -2120,34 +2120,40 @@
 turn;%0A          
-%7D%0A
+  %7D%0A    
         Attendee
@@ -2184,16 +2184,22 @@
         
+ 
+     
 new Atte
@@ -2243,32 +2243,36 @@
 olled);%0A        
+   
+ 
 datastoreClient.
@@ -2305,32 +2305,36 @@
 endee);%0A        
+    
 response.setStat
@@ -2364,16 +2364,20 @@
 SC_OK);%0A
+    
         
@@ -2439,18 +2439,23 @@
 sionId +
- 
 %0A
+      
         
@@ -2483,24 +2483,26 @@
 me); %0A      
+ 
+ 
 %7D%0A    %7D%0A    
@@ -2669,39 +2669,8 @@
 %7B%0A  
-        boolean found = false;%0A
     
@@ -2725,32 +2725,36 @@
 ) %7B%0A            
+  
+  
 if (attendeeList
@@ -2817,15 +2817,20 @@
     
-found =
+      return
  tru
@@ -2836,34 +2836,40 @@
 ue;%0A            
-%7D%0A
+    %7D%0A  
           %7D%0A    
@@ -2878,16 +2878,18 @@
     
+  
 return f
 ound
@@ -2888,12 +2888,12 @@
 rn f
-ound
+alse
 ;%0A  

Fix formatting