// Copyright 2020 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.appengine.api.datastore.Entity;
import com.google.gson.Gson;
import com.google.sps.data.Recipe;
import com.google.sps.utils.DatastoreUtils;
import com.google.sps.utils.RecipeCollector;
import com.google.sps.utils.UserCollector;
import com.google.sps.utils.UserConstants;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/api/inventory")
public class InventoryServlet extends AuthenticationServlet {
  /** Returns user's list of inventory items */
  @Override
  protected void get(HttpServletRequest request, HttpServletResponse response) throws IOException {
    Entity userEntity = DatastoreUtils.getUserEntity(mUserService);
    List<String> inventory =
        DatastoreUtils.getPropertyAsList(userEntity, UserConstants.PROPERTY_INVENTORY);

    response.setContentType("application/json;");
    response.getWriter().println(new Gson().toJson(inventory));
  }

  /** Adds an ingredient to user's inventory */
  @Override
  protected void post(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String ingredients = request.getReader().readLine().replaceAll("\\[|\\]|\"", ""); 
    System.out.println(ingredients);
    List<String> inventory = Arrays.asList(ingredients.split("\\s*,\\s*"));
    System.out.println(inventory);
    Entity userEntity = DatastoreUtils.getUserEntity(mUserService);
    UserCollector.addInventoryToUser(
        userEntity, inventory);
  }
}

@@ -929,24 +929,52 @@
 OException;%0A
+import java.util.ArrayList;%0A
 import java.
@@ -1716,17 +1716,24 @@
 * Adds a
-n
+ list of
  ingredi
@@ -1735,16 +1735,17 @@
 gredient
+s
  to user
@@ -1968,219 +1968,243 @@
     
-System.out.println(ingredients);%0A    List%3CString%3E inventory = Arrays.asList(ingredients.split(%22%5C%5Cs*,%5C%5Cs*%22));%0A    System.out.println(inventory);%0A    Entity userEntity = DatastoreUtils.getUserEntity(mUserService);
+Entity userEntity = DatastoreUtils.getUserEntity(mUserService);%0A    List%3CString%3E inventory;%0A    if (ingredients.isEmpty()) %7B%0A        inventory = null;%0A    %7D else%7B%0A        inventory = Arrays.asList(ingredients.split(%22%5C%5Cs*,%5C%5Cs*%22));%0A    %7D
 %0A   

fix formatting for empty inventory