package com.google.sps.agents;

// Imports the Google Cloud client library
import com.google.gson.Gson;
import com.google.protobuf.Value;
import com.google.sps.data.Location;
import com.google.sps.data.Place;
import com.google.sps.data.Output;
import com.google.sps.agents.Agent;
import com.google.sps.utils.LocationUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/** Maps Agent */
public class Maps implements Agent {
    
  private final String intentName;
  private String fulfillment = null;
  private String display = null;
  private String redirect = null;
  private ArrayList<String> locationWords;
  private String locationFormatted;
  private String locationDisplayed;
  private Location location;

  public Maps(String intentName, Map<String, Value> parameters) {
    this.intentName = intentName;
    setParameters(parameters);
  }

  @Override 
  public void setParameters(Map<String, Value> parameters) {
    locationFormatted = LocationUtils.getFormattedAddress("location", parameters);
    locationWords = LocationUtils.getLocationParameters("location", parameters);
    if(intentName.contains("search")) {
        mapsSearch(parameters);
    } else if (intentName.contains("find")) {
        mapsFind(parameters);
    }
  }

  @Override
  public String getOutput() {
    return fulfillment;
  }

  @Override
  public String getDisplay() {
    return display;
  }

  @Override
  public String getRedirect() {
    return redirect;
  }

  private void mapsSearch(Map<String, Value> parameters) {
    location = new Location(locationFormatted); 
    fulfillment = "Here is the map for: " + locationFormatted;

    Place place = new Place(location.getLng(), location.getLat());
    display = place.toString();
  }

  private void mapsFind(Map<String, Value> parameters) {
    String attraction = parameters.get("place-attraction").getStringValue();
    location = new Location(locationFormatted);
    Place place;
    String limitDisplay = "";
    int limit = (int) parameters.get("number").getNumberValue();
    if (limit > 0) {
      limitDisplay = String.valueOf(limit) + " ";
      place = new Place(attraction, location.getLng(), location.getLat(), limit);
    } else {
      place = new Place(attraction, location.getLng(), location.getLat());
    }

    fulfillment =
        "Here are the top "
            + limitDisplay
            + "results for "
            + attraction
            + " in "
            + locationDisplayed
            + ".";
    display = place.toString();
  }
  
}

@@ -90,15 +90,30 @@
 gle.
-gson.Gs
+maps.errors.ApiExcepti
 on;%0A
@@ -221,79 +221,8 @@
 ce;%0A
-import com.google.sps.data.Output;%0Aimport com.google.sps.agents.Agent;%0A
 impo
@@ -320,31 +320,8 @@
 st;%0A
-import java.util.List;%0A
 impo
@@ -394,20 +394,16 @@
 Agent %7B%0A
-    
 %0A  priva
@@ -738,24 +738,150 @@
  parameters)
+%0A      throws IllegalStateException, IOException, ApiException, InterruptedException,%0A          ArrayIndexOutOfBoundsException
  %7B%0A    this.
@@ -952,17 +952,16 @@
 Override
- 
 %0A  publi
@@ -1003,32 +1003,158 @@
 lue%3E parameters)
+%0A      throws IllegalStateException, IOException, ApiException, InterruptedException,%0A          ArrayIndexOutOfBoundsException
  %7B%0A    locationF
@@ -1310,16 +1310,17 @@
 ;%0A    if
+ 
 (intentN
@@ -1341,26 +1341,24 @@
 search%22)) %7B%0A
-  
       mapsSe
@@ -1423,26 +1423,24 @@
 %22)) %7B%0A      
-  
 mapsFind(par
@@ -1721,24 +1721,150 @@
  parameters)
+%0A      throws IllegalStateException, IOException, ApiException, InterruptedException,%0A          ArrayIndexOutOfBoundsException
  %7B%0A    locat
@@ -1865,36 +1865,49 @@
  location = 
-new 
+LocationUtils.get
 Location
 (locationFor
@@ -1886,32 +1886,38 @@
 tils.getLocation
+Object
 (locationFormatt
@@ -1920,17 +1920,16 @@
 matted);
- 
 %0A    ful
@@ -2143,16 +2143,142 @@
 ameters)
+%0A      throws IllegalStateException, IOException, ApiException, InterruptedException,%0A          ArrayIndexOutOfBoundsException
  %7B%0A    S
@@ -2363,20 +2363,16 @@
 cation =
- new
  Locatio
@@ -2372,16 +2372,39 @@
 Location
+Utils.getLocationObject
 (locatio
@@ -3015,9 +3015,6 @@
   %7D%0A
-  %0A
 %7D%0A

Add exception handling and modify Location creation to continue to work