package com.google.sps.servlets;

import com.google.cloud.Date;
import com.google.gson.Gson;
import com.google.sps.data.Event;
import com.google.sps.data.User;
import com.google.sps.utilities.CommonUtils;
import com.google.sps.utilities.SpannerTasks;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/create-event")
public class EventCreationServlet extends HttpServlet {
  /** Returns event details from database */
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String eventId = request.getParameter("eventId");
    Optional<Event> eventOptional = SpannerTasks.getEventById(eventId);

    // If event DNE, sends 404 ERR to frontend
    if (eventOptional.isPresent()) {
      Event event = eventOptional.get();
      response.setContentType("text/html;");
      response.getWriter().println(new Gson().toJson(event));
    } else {
      response.sendError(
          HttpServletResponse.SC_NOT_FOUND,
          String.format("No events found with event ID %s", eventId));
    }
  }

  /** Posts new created event to database and redirects to page with created event details*/
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String name = request.getParameter("name");
    String[] parsedDate = request.getParameter("date").split("/");
    Date date =
         Date.fromYearMonthDay(
            /*Year=*/ Integer.parseInt(parsedDate[2]),
            /*Month=*/ Integer.parseInt(parsedDate[0]),
            /*Day=*/ Integer.parseInt(parsedDate[1]));
    String time = request.getParameter("time");
    String description = request.getParameter("description");
    String location = request.getParameter("location");
    Set<String> labels = Collections.unmodifiableSet(new HashSet<>(
        Arrays.asList("None"))); // hardcoded for now, we need to create label pool first

    User host = SpannerTasks.getLoggedInUser().get();
    Event event = new Event.Builder(name, description, labels, location, date, time, host).build();
    SpannerTasks.insertorUpdateEvent(event);

    String redirectUrl = "/event-details.html?eventId=" + event.getId();
    response.sendRedirect(redirectUrl);
    // Event in database
    response.getWriter().println(CommonUtils.convertToJson(SpannerTasks.getEventById(event.getId()).get().toBuilder().build()));
  }
}

@@ -234,48 +234,171 @@
 ies.
-SpannerTasks;%0Aimport java.io.IOException
+PrefilledInformationConstants;%0Aimport com.google.sps.utilities.SpannerTasks;%0Aimport java.io.IOException;%0Aimport java.lang.StringBuilder;%0Aimport java.util.ArrayList
 ;%0Aim
@@ -472,24 +472,47 @@
 il.HashSet;%0A
+import java.util.List;%0A
 import java.
@@ -723,16 +723,347 @@
 esponse;
+%0Aimport com.google.cloud.language.v1.Document;%0Aimport com.google.cloud.language.v1.LanguageServiceClient;%0Aimport com.google.cloud.language.v1.ClassifyTextRequest;%0Aimport com.google.cloud.language.v1.ClassifyTextResponse;%0Aimport com.google.cloud.language.v1.ClassificationCategory;%0Aimport com.google.cloud.language.v1.Document.Type;
 %0A%0A@WebSe
@@ -2525,165 +2525,1297 @@
 %22);%0A
+%0A
     S
-et%3CString%3E labels = Collections.unmodifiableSet(new HashSet%3C%3E(%0A        Arrays.asList(%22None%22))); // hardcoded for now, we need to create label pool first
+tring text = new StringBuilder().append(name).append(%22 %22).append(description).toString();%0A    ArrayList%3CString%3E categoryNames = new ArrayList%3CString%3E();;%0A%0A    if (text.trim().split(%22%5C%5Cs+%22).length %3E 20) %7B%0A    // Use Gcloud NLP API to predict labels based on user inputted event name and description%0A        try (LanguageServiceClient language = LanguageServiceClient.create()) %7B%0A        Document doc = Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build();%0A        ClassifyTextRequest req = ClassifyTextRequest.newBuilder().setDocument(doc).build();%0A        // Detect categories in the given text%0A        ClassifyTextResponse res = language.classifyText(req);%0A            for (ClassificationCategory category : res.getCategoriesList()) %7B%0A                String categoryName = category.getName().split(%22/%22)%5B1%5D;%0A%0A                if (category.getConfidence() %3E= 0.5 && PrefilledInformationConstants.INTERESTS.contains(categoryName) ) %7B%0A                    categoryNames.add(categoryName);%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A    // Add user inputted and NLP suggested labels together%0A    Set%3CString%3E labels = new HashSet%3C%3E();%0A    labels.addAll(new HashSet%3C%3E(categoryNames));%0A    labels.addAll(new HashSet%3C%3E(splitAsList(request.getParameter(%22interests%22))));
 %0A%0A  
@@ -3962,24 +3962,25 @@
 t).build();%0A
+%0A
     SpannerT
@@ -4283,11 +4283,128 @@
 )));%0A  %7D
+%0A%0A  private static List%3CString%3E splitAsList(String values) %7B%0A    return Arrays.asList(values.split(%22%5C%5Cs*,%5C%5Cs*%22));%0A  %7D
 %0A%7D%0A

add nlp api