#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h> // for EXIT_FAILURE
#include <graph.h>
#include <ctype.h>
#include <queue.h>
#include <dlist.h>


#define MAXNODENAME 40
#define BUFSIZE 400


// ===========HELP FUNCTIONS ============
/**
 * @brief Find position of first non-whitespace character.
 *
 * @param s - string to search.
 * @return int - Returns the position of the first non-whitespace character, or -1 if not found.
 */
int first_non_white_space(const char *s)
{
    int i = 0; // Start at first char.

    // Advance as long as we're loooking at white-space until we hit EOL.
    while (s[i] && isspace(s[i])) {
        i++;
    }
    // We found a non-whitespace char.
    if (s[i]) {
        // It was a proper characted. Return its position.
        return i;
    } else {
        // It was the null terminator. Return fail.
        return -1;
    }
}

/**
 * @brief Determine if the string is blank.
 *
 * @param s - string to search.
 * @return true if the line contains only whitespace chars.
 * @return false if the line contains at least one non-whitespace char.
 */

bool line_is_blank(const char *s)
{
    // Line is blank if it only contained white-space chars.
    return first_non_white_space(s) < 0;
}

/**
 * @brief Determina if the string is a comment line.
 *
 * @param s - string to search.
 * @return true if the line is a comment line.
 * @return false if the line is not a comment line.
 *
 * A comment line has a hash sign '#' as the first non-whitespace char on the line.
 */
bool line_is_comment(const char *s)
{
    int i = first_non_white_space(s);
    return (i >= 0 && s[i] == '#');
}

/**
 * @brief Extract node names from a line from the map file.
 *
 * @param buf - Input buffer.
 * @param n1 - Output buffer for the first node name. Must be at least MAXNODENAME+1 long.
 * @param n2 - Ditto for the second node name.
 * @return int - Returns the number of correctly parsed node names. If the return value is 2, both n1
 * and n2 contain node names. If the return value is less than 2, parsing of at least one node name
 * failed, in which case the content of n1 and n2 are undefined.
 */
int parse_map_line(const char *buf, char *n1, char *n2)
{
    // Create a format string the will do the work.
    char fmt[20];
    // This will generate the format string " %40s %40s" if MAXNODENAME is 40.
    snprintf(fmt, sizeof(fmt), " %%%ds %%%ds", MAXNODENAME, MAXNODENAME);

    // sscanf does all the necessary parsing.
    // Node names must be separated by whitespace.
    // Whitespace before the first node name is allowed.
    // Anything after the second node name is ignored.
    int n = sscanf(buf, fmt, n1, n2);

    // The return value from sscanf contains the number of properly parsed format codes, i.e. the
    // number of node names.
    return n;
}

/**
 * FUNCTION TO TRAVERSE THE GRAPH
 * @g: pointer to the graph
 * @src: pointer to source node
 * @dest: pointer to destination node
 */
bool find_path(graph *g, node *src, node *dest)
{
    //Mark the starting node as seen
    g = graph_node_set_seen(g,src,true);
    

    //Put it in an empty queue
    queue *q = queue_enqueue(queue_empty(NULL),src);
    while (!queue_is_empty(q))
    {
        //pick the first node from queue
        src = queue_front(q);
        q = queue_dequeue(q);

        //if node equals destinatiom node, return true
        if (nodes_are_equal(src,dest))
        {
            return true;
        }
        //get the neighbours
        dlist *neighbours = graph_neighbours(g, src);
        dlist_pos p = dlist_first(neighbours);

        while (!dlist_is_end(neighbours,p))
        {

            if (!graph_node_is_seen(g,p))
            {
                //Mark unseen node as seen and put in queue
                g = graph_node_set_seen(g,dlist_inspect(neighbours,p),true);
                q = queue_enqueue(q,dlist_inspect(neighbours,p));
            }
            p = dlist_next(neighbours,p);
        }
        
    }
    
    return false;
}

/**
 * FUNCTION THAT MAKES A GRAPH FROM FILE
 * 
 */
graph *graph_from_file()
{
    FILE *fptr = fopen("airmap1.map", "r");
    graph *g;
    if (fptr == NULL)
    {
        printf("Error reading the file\n");
        exit(EXIT_FAILURE);
    }

    char string[100];
    

}

 //MAIN
int main(void)
{

    printf("Enter origin and destination (quit to exit): ");

    return 0;
}







