package com.google.sps.servlets;

import com.google.gson.Gson;
import com.google.sps.data.Book;
import com.google.sps.data.BookReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Returns book titles and reviews as a JSON hashmap, with IDs, e.g. {4:[{"title": Othello,
 * "reviews": {"Nice", "Bad"}}]}
 */
@WebServlet("/book-data")
public class BookServlet extends HttpServlet {
  private Map<Integer, Book> books;

  @Override
  public void init() throws ServletException {

    BookReader reader = new BookReader(System.getProperty("user.home") + "/step-2020-wipeout-jr/portfolio/src/main/webapp/WEB-INF/20_books.csv");
    books = reader.makeBookList();
  }

//   private HashMap<Integer, Book> readBooks() throws ServletException {
//     HashMap<Integer, Book> allBooks = new HashMap<Integer, Book>();
//     String path = "/WEB-INF/"
//         + "20_books.csv";
//     try (Scanner scanner =
//              new Scanner(getServletContext().getResourceAsStream(path)).useDelimiter("\\Z")) {
//       String content = scanner.next().replaceAll("[\\r\\n]+", "");
//       String[] lines = content.split("NEXTBOOK"); // lines[i] represents one row of the file

//       String current_title = "";
//       Book.Builder current_builder = Book.builder().title("null");
//       int currentId = 0;

//       for (int i = 1; i < lines.length; i++) {
//         String[] cells = lines[i].split(",");
//         String title = cells[0];
//         String genre = cells[8];
//         String review = cells[13];
//         if (current_title.equals(title)) {
//           // add review:
//           current_builder.addReview(review);
//         } else {
//           // close old book:
//           if (i != 1) {
//             Book book = current_builder.build();
//             allBooks.put(currentId++, book);
//           }
//           // start building new book
//           current_builder = Book.builder().title(title).genre(genre).addReview(review);
//           current_title = title;
//         }
//       }
//       Book book = current_builder.build();
//       allBooks.put(currentId, book);
//     } catch (Exception e) {
//       throw new ServletException("Error reading CSV file", e);
//     }
//     return allBooks;
// >>>>>>> master
//   }

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType("application/json");
    Gson gson = new Gson();
    String json = gson.toJson(books);
    response.getWriter().println(json);
  }
}

@@ -996,1603 +996,8 @@
  %7D%0A%0A
-//   private HashMap%3CInteger, Book%3E readBooks() throws ServletException %7B%0A//     HashMap%3CInteger, Book%3E allBooks = new HashMap%3CInteger, Book%3E();%0A//     String path = %22/WEB-INF/%22%0A//         + %2220_books.csv%22;%0A//     try (Scanner scanner =%0A//              new Scanner(getServletContext().getResourceAsStream(path)).useDelimiter(%22%5C%5CZ%22)) %7B%0A//       String content = scanner.next().replaceAll(%22%5B%5C%5Cr%5C%5Cn%5D+%22, %22%22);%0A//       String%5B%5D lines = content.split(%22NEXTBOOK%22); // lines%5Bi%5D represents one row of the file%0A%0A//       String current_title = %22%22;%0A//       Book.Builder current_builder = Book.builder().title(%22null%22);%0A//       int currentId = 0;%0A%0A//       for (int i = 1; i %3C lines.length; i++) %7B%0A//         String%5B%5D cells = lines%5Bi%5D.split(%22,%22);%0A//         String title = cells%5B0%5D;%0A//         String genre = cells%5B8%5D;%0A//         String review = cells%5B13%5D;%0A//         if (current_title.equals(title)) %7B%0A//           // add review:%0A//           current_builder.addReview(review);%0A//         %7D else %7B%0A//           // close old book:%0A//           if (i != 1) %7B%0A//             Book book = current_builder.build();%0A//             allBooks.put(currentId++, book);%0A//           %7D%0A//           // start building new book%0A//           current_builder = Book.builder().title(title).genre(genre).addReview(review);%0A//           current_title = title;%0A//         %7D%0A//       %7D%0A//       Book book = current_builder.build();%0A//       allBooks.put(currentId, book);%0A//     %7D catch (Exception e) %7B%0A//       throw new ServletException(%22Error reading CSV file%22, e);%0A//     %7D%0A//     return allBooks;%0A// %3E%3E%3E%3E%3E%3E%3E master%0A//   %7D%0A%0A
   @O

delete deadcode