package com.google.sps.servlets;

import static org.apache.lucene.util.SloppyMath.haversinMeters;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.gson.Gson;
import com.google.sps.data.Grid;
import com.google.sps.data.Location;
import com.google.sps.data.Marker;
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;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;
import java.util.HashMap;
import java.util.Arrays;
import java.lang.*;

/** Handles fetching and saving markers data. */
@WebServlet("/numberOfReports")
public class NumberOfReportsServlet extends HttpServlet {
  private static final Gson gson = new Gson();
  private static final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
  //List<Grid> reportsInGrids = new ArrayList<Grid>();
  double lat;
  double lng;

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
    MarkerServlet markerServlet = new MarkerServlet();
    List<Marker> markers = markerServlet.getMarkers(request);
    List<Grid> reportsInGrids = new ArrayList<Grid>();
    reportsInGrids = getNumOfReportsforAll(reportsInGrids, markers);
    String json = gson.toJson(reportsInGrids); //returns the number of reports for all grids
    response.setContentType("application/json");
    response.getWriter().println(json);    
  }

  //for each marker in the arraylist, get num of reports
  public List<Grid> getNumOfReportsforAll(List<Grid> reportsInGrids, List<Marker> markers){
    for(Marker marker : markers){
        //if new grid, create object and +1 reports
      if(uniqueInList(reportsInGrids, new Grid(marker.getLat(), marker.getLng()))){
        Grid grid = new Grid(marker.getLat(), marker.getLng());  
        grid.setNumOfReports(1);
        reportsInGrids.add(grid);
      }else{
        Grid grid = findGridToIncrease(reportsInGrids, new Grid(marker.getLat(), marker.getLng()));
        grid.setNumOfReports(1); //increase reports on that grid by 1
      }
    }
    return reportsInGrids;
  }

  public boolean uniqueInList(List<Grid> reportsInGrids,Grid gridToCompare){
    if(reportsInGrids.size() == 0){
        return true;
    }
    for(Grid grid : reportsInGrids){
      if(Arrays.equals(gridToCompare.createFromLatLng(), grid.createFromLatLng())){
        return false;
      }
    }
    return true;
  }

  public Grid findGridToIncrease(List<Grid> reportsInGrids, Grid gridToFind){
    for(Grid grid : reportsInGrids){
      if(Arrays.equals(gridToFind.createFromLatLng(), grid.createFromLatLng())){
        return grid;
      }
    }
    return null;
  }
}

@@ -31,72 +31,8 @@
 s;%0A%0A
-import static org.apache.lucene.util.SloppyMath.haversinMeters;%0A
 impo
@@ -343,78 +343,8 @@
 on;%0A
-import com.google.sps.data.Grid;%0Aimport com.google.sps.data.Location;%0A
 impo
@@ -657,113 +657,8 @@
 up;%0A
-import org.jsoup.safety.Whitelist;%0Aimport java.util.HashMap;%0Aimport java.util.Arrays;%0Aimport java.lang.*;
 %0A%0A/*
@@ -944,90 +944,8 @@
 ();%0A
-  //List%3CGrid%3E reportsInGrids = new ArrayList%3CGrid%3E();%0A  double lat;%0A  double lng;
 %0A%0A  
@@ -1055,63 +1055,13 @@
 ion%7B
-%0A
     
-MarkerServlet markerServlet = new MarkerServlet();
+ 
 %0A   
@@ -1084,17 +1084,17 @@
 rkers = 
-m
+M
 arkerSer
@@ -1127,125 +1127,153 @@
     
-List%3CGrid%3E reportsInGrids = new ArrayList%3CGrid%3E();%0A    reportsInGrids = getNumOfReportsforAll(reportsInGrids, markers
+long reportsInGrids = getNumOfReportsforAll(markers, Integer.parseInt(request.getParameter(%22row%22)), Integer.parseInt(request.getParameter(%22col%22))
 );%0A 
@@ -1321,54 +1321,8 @@
 ds);
- //returns the number of reports for all grids
 %0A   
@@ -1423,1251 +1423,280 @@
 %0A  /
-/for each marker in the arraylist, get num of reports%0A  public List%3CGrid%3E getNumOfReportsforAll(List%3CGrid%3E reportsInGrids, List%3CMarker%3E markers)%7B%0A    for(Marker marker : markers)%7B%0A        //if new grid, create object and +1 reports%0A      if(uniqueInList(reportsInGrids, new Grid(marker.getLat(), marker.getLng())))%7B%0A        Grid grid = new Grid(marker.getLat(), marker.getLng());  %0A        grid.setNumOfReports(1);%0A        reportsInGrids.add(grid);%0A      %7Delse%7B%0A        Grid grid = findGridToIncrease(reportsInGrids, new Grid(marker.getLat(), marker.getLng()));%0A        grid.setNumOfReports(1); //increase reports on that grid by 1%0A      %7D%0A    %7D%0A    return reportsInGrids;%0A  %7D%0A%0A  public boolean uniqueInList(List%3CGrid%3E reportsInGrids,Grid gridToCompare)%7B%0A    if(reportsInGrids.size() == 0)%7B%0A        return true;%0A    %7D%0A    for(Grid grid : reportsInGrids)%7B%0A      if(Arrays.equals(gridToCompare.createFromLatLng(), grid.createFromLatLng()))%7B%0A        return false;%0A      %7D%0A    %7D%0A    return true;%0A  %7D%0A%0A  public Grid findGridToIncrease(List%3CGrid%3E reportsInGrids, Grid gridToFind)%7B%0A    for(Grid grid : reportsInGrids)%7B%0A      if(Arrays.equals(gridToFind.createFromLatLng(), grid.createFromLatLng()))%7B%0A        return grid;%0A      %7D%0A    %7D%0A    return null
+** Get num of reports for a grid. */%0A  public long getNumOfReports(List%3CMarker%3E markers, int gridRow, int gridCol)%7B%0A    long reports = markers.stream()%0A    .filter(marker -%3E marker.getGridRow() == gridRow && marker.getGridCol() == gridCol)%0A    .count();%0A    return reports
 ;%0A  

Modify getNumOfReports() to return just the reports of a requested grid