package com.google.sps.servlets;

import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
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.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.sps.data.User;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/settings")
public class UserSettingsServlet extends HttpServlet {
  // This regex checks that user names are alphanumeric and (5 - 25) characters.
  // ([^\s\d]( ?){1}){5,25}

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    System.out.println("Settings fetch\n\n");
    response.setContentType("application/json");
    String idToken = (String) request.getParameter("idToken");
    User user = new User(idToken);
  }

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    System.out.println("Settings post\n\n");
    String idToken = (String) request.getParameter("idToken");
    if (idToken == null ) {
        System.out.println("idtoken was null\n");
        return;
    }

    User user = new User(idToken);

    String nameInput = (String) request.getParameter("name");
    String blobKey = (String) request.getParameter("blobKey");

    BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
    Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(request);
    List<BlobKey> blobKeys = blobs.get("imageFile");

    if (blobKeys == null || blobKeys.isEmpty()) {
      response.sendRedirect("/");
    } else {
      response.sendRedirect("/serve?blobkey=" + blobKeys.get(0).getKeyString());
    }
  }
}

@@ -31,297 +31,8 @@
 s;%0A%0A
-import com.google.appengine.api.blobstore.BlobInfo;%0Aimport com.google.appengine.api.blobstore.BlobInfoFactory;%0Aimport com.google.appengine.api.blobstore.BlobKey;%0Aimport com.google.appengine.api.blobstore.BlobstoreService;%0Aimport com.google.appengine.api.blobstore.BlobstoreServiceFactory;%0A
 impo
@@ -208,123 +208,8 @@
 ty;%0A
-import com.google.appengine.api.images.ImagesService;%0Aimport com.google.appengine.api.images.ImagesServiceFactory;%0A
 impo
@@ -269,53 +269,8 @@
 on;%0A
-import java.util.List;%0Aimport java.util.Map;%0A
 impo
@@ -442,16 +442,85 @@
 ponse;%0A%0A
+// So far this servlet allows fetching and uploading a display name.%0A
 @WebServ
@@ -678,33 +678,111 @@
 .%0A  
-// (%5B%5E%5Cs%5Cd%5D( ?)%7B1%7D)%7B5,25%7D
+static final String namePattern = %22%5E%5B%5C%5Cw-_%5D+$%22;%0A  static final String consecutiveSpacePattern = %22 %7B2,%7D%22
 %0A%0A  
@@ -894,54 +894,8 @@
 n %7B%0A
-    System.out.println(%22Settings fetch%5Cn%5Cn%22);%0A
     
@@ -1037,16 +1037,111 @@
 Token);%0A
+    String displayName = user.getName();%0A    reponse.setAttribute(%22displayName%22, displayName);%0A
   %7D%0A%0A  @
@@ -1257,44 +1257,47 @@
     
-System.out.println(%22Settings post%5Cn%5C
+reponse.setContentType(%22application/jso
 n%22);
@@ -1364,109 +1364,8 @@
 %22);%0A
-    if (idToken == null ) %7B%0A        System.out.println(%22idtoken was null%5Cn%22);%0A        return;%0A    %7D%0A%0A
     
@@ -1454,24 +1454,29 @@
 er(%22name
+Input
 %22);%0A    
 String b
@@ -1471,331 +1471,389 @@
     
-String blobKey = (String) request.getParameter(%22blobKey%22);%0A%0A    BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();%0A    Map%3CString, List%3CBlobKey%3E%3E blobs = blobstoreService.getUploads(request);%0A    List%3CBlobKey%3E blobKeys = blobs.get(%22imageFil
+nameInput.trim();%0A%0A    if (namePattern.isMatch(nameInput) && !consecutiveSpacePattern.isMatch(nameInput)) %7B%0A      user.setName(nameInput);%0A      response.setAttribute(%22success%22, true);%0A    %7D else %7B%0A      if (!namePattern.isMatch(nameInput)) %7B%0A        reponse.setAttribute(%22message%22, %22Invalid character in requested nam
 e%22);%0A
-%0A
+  
     
-if (blobKeys == null %7C%7C blobKeys.isEmpty(
+%7D else if (consecutiveSpacePattern.isMatch(nameInput
 )) %7B
@@ -1851,32 +1851,34 @@
 Input)) %7B%0A      
+ 
+ 
 response.sendRed
@@ -1876,25 +1876,68 @@
 e.se
-ndRedirect(%22/
+tAttribute(%22message%22, %22Consecutive spaces not allowed.
 %22);%0A
+  
     
@@ -1947,24 +1947,26 @@
 lse %7B%0A      
+ 
+ 
 response.sen
@@ -1968,71 +1968,106 @@
 e.se
-ndRedirect(%22/serve?blobkey=%22 + blobKeys.get(0).getKeyString());
+tAttribute(%22message%22, %22Invalid name input%22).%0A      %7D%0A      response.setAttribute(%22success%22, false)
 %0A   

add settings servlet functionality for fetching and editing display name