import java.util.List;

public class Frontend implements FrontendInterface{

    BackendInterface backend;

    public Frontend(BackendInterface backend){ // creates a constructor when the user inputs their own backend
        this.backend = backend; // user backend
    }
    public Frontend(){ // creates a constructor when the user doesn't input a backend
        backend = new Backend_Placeholder(new Graph_Placeholder()); // uses backend and graph placeholders
    }

    /**
     * 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 for = "start"> Start Location: </label>
        <input id = "start"> </input>
        <label for = "end"> End Location: </label>
        <input id = "end"> </input>
        <button> Find Shortest Path </button> 
        <br>
        """; // creates an HTML fragment in the form of a java string
    }

    /**
     * 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) {
        StringBuilder string = new StringBuilder(); // creates a string builder to help build the HTML fragment
        string.append("<p> Start Location: " + start + "</p>\n"); // adds the user inputed start location with a message
        string.append("<p> End Location: " + end + "</p>\n"); // adds the user inputed end location with a message
        List<String> stringList = backend.findLocationsOnShortestPath(start, end); // gets the locations from start to end
        if (stringList.size() == 0) { // if there is no locations in the list then it outputs that there was none in the webite
            string.append("<p> There Were No Paths Found </p>\n");
        }
        string.append("<ol>\n"); // starts a list
        for (int i = 0; i < stringList.size(); i++) {
            string.append("<li>" + stringList.get(i) + "</li>\n"); // for each of the locations in the list object it adds to the HTML list
        }
        string.append("</ol>\n"); // ends a list
        Double result = 0.0; // creates the result of the added locations distances
        List<Double> numberList = backend.findTimesOnShortestPath(start, end); // gets list of distances
        for (int i = 0; i < numberList.size(); i++) {
            result += numberList.get(i); // adds each distance to the result
        }
        string.append("<p> Total Time Travelled Along This Path: " + result + "</p>\n"); // output the result to the user
        string.append("<br>\n");
        return string.toString(); // returns the string from the string builder
    }

    /**
     * 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 for = "start"> Start Location: </label>
        <input id = "from"> </input>
        <button> Ten Closest Destinations </button> 
        <br>
        """; // creates an HTML fragment in the form of a java string
    }

    /**
     * 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) {
        StringBuilder string = new StringBuilder(); // creates a string builder to help build the HTML fragment
        string.append("<p> Start Location: " + start + "</p>\n"); // ouputs to the user their intended start location
        List<String> stringList = backend.getTenClosestDestinations(start); // gets a list of the ten closest locations from the start location
        if (stringList.size() == 0) {
            string.append("<p> There Were No Destinations </p>\n"); // if there is no locations in the list the website shows that
        }
        string.append("<ul>\n"); // starts a list in HTML
        for (int i = 0; i < stringList.size(); i++) {
            string.append("<li>" + stringList.get(i) + "</li>\n"); // for each of the locations in the object list they are added to the HTML list
        }
        string.append("</ul>\n"); // ends the list in HTML
        string.append("<br>\n");
        return string.toString(); // returns the string from the string builder
    }
    
}
