// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.sps.servlets;

import com.google.common.collect.ListMultimap;
import com.google.common.collect.ArrayListMultimap;
import com.google.sps.utilities.CommonUtils;
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;

/** Servlet that handles comments data. */
@WebServlet("/searchData")
public class SearchDataServlet extends HttpServlet {
  private ListMultimap<String, String> keywordToEventIds = ArrayListMultimap.create();
  private static final String KEYWORD = "keyword";
  private static final String EVENT_ID = "event-id";
  private static final String DESCRIPTION = "description";

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String keyword = request.getParameter(KEYWORD);

    if (keyword == null) {
        
    }

    response.setContentType("application/json;");
    response.getWriter().println(CommonUtils.convertToJson(keywordToEventIds.get(keyword)));
  }

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String eventId = request.getParameter(EVENT_ID);
    String description = CommonUtils.getParameter(request, DESCRIPTION, "");

    String[] keywordsInDescription = description.split("[^a-zA-Z0-9']+");
    addWordsToIndex(eventId, keywordsInDescription);

    response.sendRedirect("/events-feed.html");
  }

  private void addWordsToIndex(String eventId, String[] keywords) {
    for (String keyword : keywords) {
      keywordToEventIds.put(keyword, eventId);
    }
  }
}
@@ -649,16 +649,21 @@
 collect.
+Array
 ListMult
@@ -693,37 +693,32 @@
 .common.collect.
-Array
 ListMultimap;%0Aim
@@ -792,59 +792,8 @@
 on;%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0A
 impo
@@ -994,21 +994,62 @@
 les 
-comments data
+search queries and addition of event keywords to index
 . */
@@ -1543,17 +1543,16 @@
 YWORD);%0A
-%0A
     if (
@@ -1576,18 +1576,125 @@
 %7B%0A      
-  
+response.sendError(HttpServletResponse.SC_BAD_REQUEST, String.format(%22No keyword specified.%22));%0A      return;
 %0A    %7D%0A%0A
@@ -2082,16 +2082,166 @@
 ON, %22%22);
+%0A    if (eventId == null) %7B%0A      response.sendError(HttpServletResponse.SC_BAD_REQUEST, String.format(%22No event ID specified.%22));%0A      return;%0A    %7D
 %0A%0A    St
@@ -2311,25 +2311,28 @@
 +%22);%0A    add
-W
+Keyw
 ordsToIndex(
@@ -2436,17 +2436,20 @@
 void add
-W
+Keyw
 ordsToIn
@@ -2585,8 +2585,9 @@
  %7D%0A  %7D%0A%7D
+%0A

Add error-checking to servlet.