import java.util.List;


public class Frontend implements FrontendInterface {

  private BackendInterface backend;

  /**
   * The Frontend constructor that instantiates a Backend object
   *
   * @param backend is used for shortest path computations
   */
  public Frontend(BackendInterface backend) {
    this.backend = backend;
  }

  /**
   * Returns an HTML fragment that can be embedded within the body of a larger html page.  This HTML
   * output should include: - a text input field with the id="start", for the start location - a
   * text input field with the id="end", for the destination - a button labelled "Find Shortest
   * Path" to request this computation Ensure that these text fields are clearly labelled, so that
   * the user can understand how to use them.
   *
   * @return an HTML string that contains input controls that the user can make use of to request a
   * shortest path computation
   */
  @Override
  public String generateShortestPathPromptHTML() {
    return "<label>Start Location:</label>" + "<input id=\"start\"/>" + "<label>Destination:</label>" + "<input id=\"end\"/>" + "<button>Find Shortest Path</button>";
  }

  /**
   * Returns an HTML fragment that can be embedded within the body of a larger html page.  This HTML
   * output should include: - a paragraph (p) that describes the path's start and end locations - an
   * ordered list (ol) of locations along that shortest path - a paragraph (p) that includes the
   * total travel time along this path Or if there is no such path, the HTML returned should instead
   * indicate the kind of problem encountered.
   *
   * @param start is the starting location to find a shortest path from
   * @param end   is the destination that this shortest path should end at
   * @return an HTML string that describes the shortest path between these two locations
   */
  @Override
  public String generateShortestPathResponseHTML(String start, String end) {
    // Check if the start and end input is valid
    if (start == null || end == null) {
      return "<p>Please enter non-null values for start and end</p>";
    }
    // Get the location list generated by the Backend
    List<String> locations = backend.findLocationsOnShortestPath(start, end);
    // Check if there is a valid path between start and end
    if (locations == null || locations.isEmpty()) {
      return "<p>There are no possible paths between " + start + " and " + end + "</p>";
    }

    // Create the first line of the HTML string
    String HTML =
        "<p>The shortest path between " + start + " and " + end + " goes through the following locations: </p>";
    // Start an ordered list
    HTML += "<ol>";
    for (String location : locations) {
      // Add each location as a list item
      HTML += "<li>" + location + "</li>";
    }
    HTML += "</ol>";

    // Get the time list generated by the Backend
    List<Double> times = backend.findTimesOnShortestPath(start, end);
    Double totalTime = 0.0;
    for (Double time : times) {
      // Calculate the total time by adding each element
      totalTime += time;
    }
    HTML += "<p>The total travel time along this path is " + totalTime + "</p>";
    return HTML;
  }

  /**
   * Returns an HTML fragment that can be embedded within the body of a larger html page.  This HTML
   * output should include: - a text input field with the id="from", for the start location - a
   * button labelled "Ten Closest Destinations" to submit this request Ensure that this text field
   * is clearly labelled, so that the user can understand how to use it.
   *
   * @return an HTML string that contains input controls that the user can make use of to request a
   * ten closest destinations calculation
   */
  @Override
  public String generateTenClosestDestinationsPromptHTML() {
    return "<label >Start Location:</label>" + "<input id=\"from\" placeholder=\"Start Location\" />" + "<button>Ten Closest Destinations</button>";
  }

  /**
   * Returns an HTML fragment that can be embedded within the body of a larger html page.  This HTML
   * output should include: - a paragraph (p) that describes the start location that travel time to
   * the closest destinations are being measured from - an unordered list (ul) of the ten locations
   * that are closest to start Or if no such destinations can be found, the HTML returned should
   * instead indicate the kind of problem encountered.
   *
   * @param start is the starting location to find close destinations from
   * @return an HTML string that describes the closest destinations from the specified start
   * location.
   */
  @Override
  public String generateTenClosestDestinationsResponseHTML(String start) {
    // Check if the start and end input is valid
    if (start == null) {
      return "<p>Please enter non-null values for start</p>";
    }
    // Get the location list generated by the Backend
    List<String> locations = backend.getTenClosestDestinations(start);
    if (locations == null || locations.isEmpty()) {
      return "<p>There are no possible paths that start with " + start + "</p>";
    }
    // Create the first line of the HTML string
    String HTML = "<p>These are the ten closest destinations from " + start + ": </p>";
    // Start an unordered list
    HTML += "<ul>";
    for (String location : locations) {
      // Add each location as a list item
      HTML += "<li>" + location + "</li>";
    }
    HTML += "</ul>";
    return HTML;
  }
}
