import java.util.List;
import java.util.NoSuchElementException;
/**
 * This class handles user inputs and outputs for finding the shortest paths between locations on
 * campus
 */
public class Frontend implements FrontendInterface {
	private BackendInterface backend;
	
	/**
	 * Contructor that assigns the parameter backend to a data field
	 * @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 includes the input fields for a start and end location and a button
     * 
     * @return an HTML string that contains input controls that the user can
     *         make use of to request a shortest path computation
	 */
	public String generateShortestPathPromptHTML() {
		String s = "<input id=\"start\" placeholder=\"Start Location\"/>";
		s += "<input id=\"end\" placeholder=\"Destination\"/>";
		s += "<button>Find Shortest Path</button>";
		
		return s;
	}

	/**
     * Returns an HTML fragment that can be embedded within the body of a larger html page.
     * This HTML output includes the output describing the start and end locations,
     * the locations along that shortest path, and the total travel time
     * 
     * Or if there is no such path, the HTML returned instead indicates 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
     */
	public String generateShortestPathResponseHTML(String start, String end) {
		String s;
		List<String> locationsOnPath = backend.findLocationsOnShortestPath(start, end);
		// If there is no path found, let the user know
		if (locationsOnPath.isEmpty()) {
			s = "<p>There is no path between " + start
			    + " and " + end + "</p>";
		}
		else {
			// Paragraph with end locations
			s = "<p>This is the shortest path starting at " + start + " and ending at "
			    + end + ":</p><ol>";
			
			// Ordered list of each location along path
			for (String loc: locationsOnPath) {
				s += "<li>" + loc + "</li>";
			}
			
			// Calculate the total time in the shortest path
			List<Double> timesOnPath = backend.findTimesOnShortestPath(start, end);
			double totalTime = 0;
			for (Double time: timesOnPath)
				totalTime += time;
			
			// Paragraph with total time
			s += "</ol><p>This path has an estimated walking time of "
					+ totalTime + " seconds</p>";
		}
		return s;
	}

	/**
     * Returns an HTML fragment that can be embedded within the body of a larger html page.
     * This HTML output includes the input field for the start location and a button
     * 
     * @return an HTML string that contains input controls that the user can
     *         make use of to request a ten closest destinations calculation
     */
	public String generateTenClosestDestinationsPromptHTML() {
		String s = "\n<input id=\"from\" placeholder=\"Start location\"/>";
		s += "/n<button>Ten Closest Destinations</button>";
		
		return s;
	}

    /**
     * Returns an HTML fragment that can be embedded within the body of a larger html page.
     * This HTML output includes the output describing the start location and the ten closest
     * locations from that starting location, based on travel time
     * 
     * @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.
     */
	public String generateTenClosestDestinationsResponseHTML(String start) {
		// Paragraph describing start location
		String s = "<p>These are the ten closest destinations based on walking time from "
		    + start + ":</p><ul>";
		
		// Get list of closest locations
		List<String> closestLoc;
		try {
		closestLoc = backend.getTenClosestDestinations(start);
		}
		catch (NoSuchElementException e) {
			return "<p>There were no destinations to be found</p>";
		}
		// Have a bullet for each destination
		for (String loc: closestLoc) {
			s += "<li>" + loc + "</li>";
		}
		
		return s + "</ul>";
	}
}
