import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;

public class Frontend implements FrontendInterface {

    Backend_Placeholder backend = null;
    Graph_Placeholder graph = new Graph_Placeholder();

    public Frontend(BackendInterface backend) {
        this.backend = (Backend_Placeholder) 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 "Starting location: <input id='start' type='text'></input><br>\n"+
                "Ending location: <input id='end' type='text'></input><br><br>\n"+
                "<input type = 'button' value='Find Shortest Path'></input>";
    }

    /**
     * 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
     */
    public String generateShortestPathResponseHTML(String start, String end){
        List<String> path = backend.findLocationsOnShortestPath(start, end);

        // in case if no such path exists
        if (path.size() == 0){
            return "<p>There is no path between "+start+" and "+end+"</p>";
        }

        //time between the locations in the path
        List<Double> time = backend.findTimesOnShortestPath(start, end);

        double totalTime = 0;
        for (Double t: time){
            totalTime += t;
        }

        // string html output for the ordered list
        String listDisplay = "";
        listDisplay += "<ol>\n";
        for (String location: path){
            listDisplay += "\t<li>";
            listDisplay += location;
            listDisplay += "</li>\n";
        }
        listDisplay += "</ol>";

        //html response
        String html = "<p>Shortest path from "+start+" to "+end+":</p>\n" +
                listDisplay + "\n" +
                "<p>Total travel time: "+totalTime+" seconds</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 "Longest Location List From" 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 longest location list calculation
     */
    public String generateLongestLocationListFromPromptHTML(){
        return "Starting location: <input id='from' type='text'></input><br><br>\n" +
                "<input type='button' value='Longest Location List From'></input>";

    }

    /**
     * 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 number of locations on path Or if no such path can be found, the HTML
     * returned should instead indicate the kind of problem encountered.
     *
     * @param start is the starting location to find the longest list from
     * @return an HTML string that describes the longest list of locations along a shortest path
     * starting from the specified location
     */
    public String generateLongestLocationListFromResponseHTML(String start){
        try{
            // to get a list of locations for the longest path from starting
            List<String> location = backend.getLongestLocationListFrom(start);
            if (location.size() == 0){
                return "<p>There is no path between "+start+"</p>";
            }

            //last location in the path
            String endLocation = location.get(location.size()-1);
            int totalLocation = location.size();

            // string html output for the ordered list
            String listDisplay = "";
            listDisplay += "<ol>\n";
            for (String loc: location){
                listDisplay += "\t<li>";
                listDisplay += loc;
                listDisplay += "</li>\n";
            }
            listDisplay += "</ol>";

            //HTML response
            String htmlResponse = "<p>Longest location list from "+start+" to "+endLocation+":</p" +
                    ">\n" +
                    listDisplay + "\n" +
                    "<p>Total locations: "+totalLocation+"</p>";

            return htmlResponse;

        } catch (NoSuchElementException e){
            return "<p>Error! location '"+start+"' not found!</p>";
        }
    }
}

