package com.google.sps.servlets;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.sps.data.User;
import java.io.IOException;
import java.util.List;
import java.util.Collections;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** Servlet responsible for deleting recipes. */
@WebServlet("/add-list")
public class AddToListServlet extends HttpServlet {

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    long id = Long.parseLong(request.getParameter("id"));
    String idToken = request.getParameter("idToken");
    String type = request.getParameter("type");

    boolean contains = false;

    User user = new User(idToken);
    List<Key> keys;
    if (type.equals("cookbook")) {
      keys = user.getCookbookList();
    } else if (type.equals("planner")) {
      keys = user.getPlannerList();
    } else {
      keys = Collections.emptyList();
      System.out.println("AddToListServlet: invalid type");
    }

    Key key = KeyFactory.createKey("Recipe", id);
    if (keys.contains(key)) {
      contains = true;
    } else {
      contains = false;
    }

    // returns whether the doPost will remove the recipe or not
    response.setContentType("text/html;");
    response.getWriter().println(String.valueOf(contains));  
  }

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    long id = Long.parseLong(request.getParameter("id"));
    String idToken = request.getParameter("idToken");
    String type = request.getParameter("type");

    Key key = KeyFactory.createKey("Recipe", id);
    User user = new User(idToken);

    if (type.equals("cookbook")) {
      user.addCookbookKey(key);
    } else if (type.equals("planner")) {
      user.addPlannerKey(key);
    } else {
      System.out.println("AddToListServlet: invalid type");
    }
  }
}
@@ -451,13 +451,11 @@
 for 
-delet
+add
 ing 
@@ -460,16 +460,53 @@
 g recipe
+ keys to user's planner/cookbook list
 s. */%0A@W
@@ -581,16 +581,81 @@
 vlet %7B%0A%0A
+  // checks whether the recipe already exists in the user's list%0A
   @Overr
@@ -749,32 +749,64 @@
 s IOException %7B%0A
+    // gets values from request%0A
     long id = Lo
@@ -989,38 +989,163 @@
     
-User user = new User(idToken);
+// creates a user based on the passed in user id token%0A    User user = new User(idToken);%0A    // gets the appropriate list based on the passed in list type
 %0A   
@@ -1436,53 +1436,160 @@
     
-Key key = KeyFactory.createKey(%22Recipe%22, id);
+// creates a key based on the passed in recipe ID%0A    Key key = KeyFactory.createKey(%22Recipe%22, id);%0A    // checks whether the key already exists in list
 %0A   
@@ -1686,72 +1686,8 @@
  %7D%0A%0A
-    // returns whether the doPost will remove the recipe or not%0A
     
@@ -1792,16 +1792,54 @@
   %0A  %7D%0A%0A
+  // adds a recipe key to user's list%0A
   @Overr
@@ -1938,24 +1938,56 @@
 Exception %7B%0A
+    // gets values from request%0A
     long id 
@@ -2143,89 +2143,275 @@
     
-Key key = KeyFactory.createKey(%22Recipe%22, id);%0A    User user = new User(idToken);%0A
+// creates a key based on the passed in recipe ID%0A    Key key = KeyFactory.createKey(%22Recipe%22, id);%0A    // creates a user based on the passed in user id token%0A    User user = new User(idToken);%0A%0A    // adds created key to appropriate list based on passed in list type
 %0A   

add comments to AddToListServlet.java