/**************************
 * Owen Sullivan
 * CPSC 2310 Fall 22
 * UserName: opsulli
 * Instructor:  Dr. Yvon Feaster
*************************/

#include "functions.h"

node_t* createList(FILE *inFile, node_t **head) {

    // Loop until the end of the input file has been reached
    while (!feof(inFile)) {

        // Create a new node with the contents of the return value
        // of readNodeInfo
        node_t *node = readNodeInfo(inFile);

        // Add the newly created node to the linked list
        add(&node, head);

    }

    // Return a reference to the start of the linked list
    return *head;

}

void add(node_t **node, node_t **head) {

    // Check if the linked list is empty
    if (*head == NULL) {

        // Set the head node to the node being added
        *head = *node;

        // Set head->next to NULL so that the head node is now
        // functionally just a regular node
        (*head)->next = NULL;

      // If linked list is not empty
    } else {

        // Create a temp node
        node_t *temp = malloc(sizeof(node_t));
        // Create a node that will become the tail of the current linked list,
        // starting at the head node before looping below
        node_t *tail = *head;

        // Set temp to the node being added
        temp = *node;
        // Set temp->next to NULL so that the temp node is now
        // functionally just a regular node
        temp->next = NULL;

        // Loop through every node in the linked list
        while (tail->next != NULL) {

            // Set the tail node to the next node,
            // by the end of the loop it will be the last node
            // in the linked list
            tail = tail->next;

        }

        // Add the populated temp node after the tail node
        tail->next = temp;

    }

}

node_t* readNodeInfo(FILE *inFile) {

    // Allocate memory for a new node
    node_t *node = malloc(sizeof(node_t));

    // Locally-scoped arrays to hold birthday values, allowing for
    // scansets for what would otherwise be scanned as integers
    char month[3];
    char day[3];
    char year[5];

    // Read strings of data from the input file using scansets to stop
    // at commas (or new lines for end of line)
    fscanf(inFile, "%[^,]%*c", node->lastName);
    fscanf(inFile, "%[^,]%*c", node->firstName);
    fscanf(inFile, "%[^,]%*c", month);
    fscanf(inFile, "%[^,]%*c", day);
    fscanf(inFile, "%[^,]%*c", year);
    fscanf(inFile, "%[^,]%*c", node->favoriteSong);
    fscanf(inFile, "%[^,]%*c", node->favoritePastime);
    fscanf(inFile, "%[^\n]%*c", node->whyCS);

    // Store birthday values in node as converted integers from locally-scoped
    // char arrays
    node->birthday.month = atoi(month);
    node->birthday.day = atoi(day);
    node->birthday.year = atoi(year);

    // Return the newly populated node
    return node;

}

void printList(FILE *outFile, node_t *head) {

    // Print the top border
    printBorder(outFile);

    // Set the node to be printed to the head node
    node_t *node = head;

    // Assert that the linked list is not empty
    assert(node != NULL);

    // Start printing
    fprintf(outFile, "List Info: \n");
    // Loop until there are no nodes left
    while (node != NULL) {

        // Print node info
        fprintf(outFile, "Name: %s %s\n", node->firstName, node->lastName);
        char month[10];
        // Convert month to string of month name
        switch (node->birthday.month) {

            case 1:
                strcpy(month, "January");
                break;
            
            case 2:
                strcpy(month, "February");
                break;
            
            case 3:
                strcpy(month, "March");
                break;
            
            case 4:
                strcpy(month, "April");
                break;
            
            case 5:
                strcpy(month, "May");
                break;
            
            case 6:
                strcpy(month, "June");
                break;
            
            case 7:
                strcpy(month, "July");
                break;
            
            case 8:
                strcpy(month, "August");
                break;
            
            case 9:
                strcpy(month, "September");
                break;
            
            case 10:
                strcpy(month, "October");
                break;
            
            case 11:
                strcpy(month, "November");
                break;
            
            case 12:
                strcpy(month, "December");
                break;

        }
        fprintf(outFile, "Date of Birth: %s %d, %d\n", month,
                                                        node->birthday.day,
                                                        node->birthday.year);
        fprintf(outFile, "Favorite Song: %s\n", node->favoriteSong);
        fprintf(outFile, "Favorite Pastime: %s\n", node->favoritePastime);
        fprintf(outFile, "Why I Chose CS: %s\n", node->whyCS);
        // Print a new line for spacing between nodes
        fprintf(outFile, "\n");

        // Move on to the next node
        node = node->next;

    }

    // Print bottom border
    printBorder(outFile);

}

void printBorder(FILE *outFile) {

    // Print 80 asterisks
    for (int i = 0; i < 80; i++) {

        fprintf(outFile, "*");

    }

    // Print a couple of new lines for spacing
    fprintf(outFile, "\n\n");

}

void deleteList(node_t **head) {

    // Create a temp node
    node_t *node;

    // Loop until the linked list is empty
    while (*head != NULL) {

        // Set the temp node to the current node
        node = *head;
        // Set the current node to the next node in the linked list
        *head = (*head)->next;

        // Free the current node
        free(node);

    }

}