import java.util.List;
import java.util.NoSuchElementException;

/**
 * Frontend Implementation that will generate HTML fragments for shortest path and closest
 * locations in the webapp.
 */
public class Frontend implements FrontendInterface {

    private BackendInterface backend;
    
    /**
    * Constructs a Frontend object and connects it to specified backend
    * @param backend 
    */
    public Frontend(BackendInterface backend) {
        this.backend = backend;
    }
    
    /**
    * Generates an HTML fragment containing the input form for shortest path query
    * Includes text fields for start/end and submission button
    * @return String of HTML containing labels, inputs, and a button
    */
    @Override
    public String generateShortestPathPromptHTML() {
        return "<div>" +
                " <label for=\"start\">Start Location:</label> " +
                " <input type=\"text\" id=\"start\" name=\"start\" placeholder=\"Enter City...\" /> " +
                " <br />" +
                " <label for=\"end\">End Location:</label> " +
                " <input type=\"text\" id=\"end\" placeholder=\"Enter City...\" /> " +
                " <br />" +
                " <button id=\"findShortestPath\">Find Shortest Path</button>" +
                "</div>";
    }

    /**
    * Generates an HTML fragment to display the result of a shortest path computation
    * If successful, displays path as ordered list with travel time, unsuccessfull, returns error message
    * @param start the starting location provided by user
    * @param end the end destination location provided by user
    * @return a string of HTML representing either path results or error
    */
    @Override
    public String generateShortestPathResponseHTML(String start, String end) {
       List<String> path = null;
       List<Double> times = null;
       // Attempt to retrieve path and segment times 
       try {

            path = backend.findLocationsOnShortestPath(start, end);
            times = backend.findTimesOnShortestPath(start, end);

        } catch (NoSuchElementException e) {
            return "<div class=\"error\"><p>Error: No path found between '" + start + "' and '" + end + "'. or locations do not exist..</p></div>";
        }
	
	if (path == null || times == null) {
	    return "<div class=\"error\"><p>Error: The backend returned no data for this path.</p></div>";
	}
	
	// Calculate total time (sum all segments)
        double totalTime = 0.0;
        for (Double time : times) {
            totalTime += time;
        }
        // Assemble HTML fragments
        StringBuilder html = new StringBuilder();
        html.append("<div>");
        html.append("<p>Path from <strong>").append(start).append("</strong> to <strong>").append(end).append("</strong>:</p>");
        html.append("<ol>");
        for (String location : path) {
            html.append("<li>").append(location).append("</li>");
        }
        // Ordered list to show progression of the journey
        html.append("</ol>");
        // Disply total time in minutes
        html.append("<p>Total Travel Time: ").append(totalTime).append(" minutes</p>");
        html.append("</div>");
        return html.toString();

    }
    /**
    * Generates an HTML fragment containing the input form to find ten closest locations
    * @return a String of HTML containing a single city input and a button
    */
    @Override
    public String generateTenClosestLocationsPromptHTML() {
        return "<div>" +
                " <label for=\"from\">Start Location:</label> " +
                " <input type=\"text\" id=\"from\" name=\"from\" placeholder=\"Enter City...\" /> " +
                " <button id=\"getTenClosest\">Ten Closest Locations</button>" +
                "</div>";
    }
    
    /**
    * Generates an HTML fragment displaying a list of the ten locations closest to a starting point
    * @param staart the reference location to find closest neighbours for
    * @return a String of HTML representing the list of nearby cities or an error
    */
    @Override
    public String generateTenClosestLocationsResponseHTML(String start) {

	// Query backend for the nearest ten locations
        try {
            List<String> closest = backend.getTenClosestLocations(start);
	    
	    // Build the unordered list for user display
            StringBuilder html = new StringBuilder();
            html.append("<div>");
            html.append("<p>Locations closest to <strong>").append(start).append("</strong>:</p>");
            html.append("<ul>");
            for (String location : closest) {
                html.append("<li>").append(location).append("</li>");
            }
            html.append("</ul>");
            html.append("</div>");

            return html.toString();
        } catch (NoSuchElementException e) {
            return "<div class=\"error\"><p>Error: Could not find locations near '" + start + "'.</p></div>";
        }
    }


}
