/* 
    CPSC-1011-003
    Lab 09
    Owen Sullivan
*/

#include <stdio.h> // Include stdio.h for printf() and scanf()
#include <stdbool.h> // Include stdbool.h for boolean variables

typedef struct PlayerInfo_struct { // Declare PlayerInfo struct
    char firstName[20]; // Declare firstName of type char
    char lastName[20]; // Declare lastName of type char
    int jerseyNumber; // Declare jerseyNumber of type int
    int rating; // Declare rating of type int
} PlayerInfo;

void GetPlayerInfo(PlayerInfo playersArray[], int numPlayers); // Function prototype for GetPlayerInfo
void PrintRoster(PlayerInfo playersArray[], int numPlayers); // Function prototype for PrintRoster
void UpdateRating(PlayerInfo playersArray[], int numPlayers); // Function prototype for UpdateRating
void FindAbove(PlayerInfo playersArray[], int numPlayers); // Function prototype for FindAbove
void ReplacePlayer(PlayerInfo playersArray[], int numPlayers); // Function prototype for ReplacePlayer

int main(void) { // Start main function

    int numPlayers; // Declare numPlayers

    printf("How many players to enter: \n"); // Prompt for numPlayers
    scanf("%d", &numPlayers); // Store user input as numPlayers

    PlayerInfo playersArray[numPlayers]; // Declare playersArray array of type struct PlayerInfo dependent on numPlayers

    char menuInput; // Declare single-character variable to be updated by user that validates if/else statements in while() loop
    bool menuReady = true; // Declare and initialize boolean that validates while() loop's continuation

    GetPlayerInfo(playersArray, numPlayers); // Call GetPlayerInfo with arguments playersArray and numPlayers

    while (menuReady) { // While loop that serves menu functions until bool menuReady is set to false by the 'q' menu option
        printf("MENU\n"); // Print "MENU" header
        printf("o - Output roster\n"); // Print 'o' option
        printf("u - Update player rating\n"); // Print 'u' option
        printf("a - Output players above a rating\n"); // Print 'a' option
        printf("r - Replace player\n"); // Print 'r' option
        printf("q - Quit\n"); // Print 'q' option
        printf("Choose an option:\n"); // Print choose prompt
        scanf(" %c", &menuInput); // Store user input for menuInput

        if (menuInput == 'o') { // If menuInput is 'o'
            PrintRoster(playersArray, numPlayers);
        } else if (menuInput == 'u') { // If menuInput is 'u'
            UpdateRating(playersArray, numPlayers);
        } else if (menuInput == 'a') { // If menuInput is 'a'
            FindAbove(playersArray, numPlayers);
        } else if (menuInput == 'r') { // If menuInput is 'r'
            ReplacePlayer(playersArray, numPlayers);
        } else if (menuInput == 'q') { // If menuInput is 'q'
            menuReady = false; // Set bool menuReady to false (which causes outer while() loop not to iterate next time)
        }
    }

    return 0; // Return end code

}

void GetPlayerInfo(PlayerInfo playersArray[], int numPlayers) { // Function to store player info, dependent on playersArray and numPlayers
    for (int i = 0; i < numPlayers; ++i) { // For loop that loops once for each player in numPlayers
        printf("Enter player %d first name: \n", i + 1); // Prompt for first name
        scanf("%s", playersArray[i].firstName); // Store first name
        printf("Enter player %d last name: \n", i + 1); // Prompt for last name
        scanf("%s", playersArray[i].lastName); // Store last name
        printf("Enter player %d jersey number: \n", i + 1); // Prompt for jersey number
        scanf("%d", &playersArray[i].jerseyNumber); // Store jersey number
        printf("Enter player %d rating: \n", i + 1); // Prompt for rating
        scanf("%d", &playersArray[i].rating); // Store rating
    }
}

void PrintRoster(PlayerInfo playersArray[], int numPlayers) { // Function to print the current roster, dependent on playersArray and numPlayers
    printf("ROSTER\n"); // Print "ROSTER" header
    for (int i = 0; i < numPlayers; ++i) { // For loop that loops once for each player in numPlayers
        printf("Player %d -- Name: %s %s, Jersey number: %d, Rating: %d\n", i + 1, playersArray[i].firstName, playersArray[i].lastName, playersArray[i].jerseyNumber, playersArray[i].rating); // Print information about player i (i + 1 because array indexes start at 0)
    }
}

void UpdateRating(PlayerInfo playersArray[], int numPlayers) { // Function to update a player's rating, dependent on playersArray and numPlayers
    printf("Enter a jersey number:\n"); // Print prompt for jersey number
    int tempVal; // Declare tempVal of type int
    scanf("%d", &tempVal); // Store input as tempVal to be checked against jerseyNumber array
    bool error = true; // Declare and initialize boolean that validates error checks
    for (int i = 0; i < numPlayers; ++i) { // For loop that loops once for each player in numPlayers
        if (tempVal == playersArray[i].jerseyNumber) { // If tempVal is equal to the element at index i of jerseyNumber
            printf("Enter a new rating for player:\n"); // Print prompt for the player's new rating
            scanf("%d", &playersArray[i].rating); // Store new value at index i of playerRating
            error = false; // Set bool error to "false" to indicate that a value has been found (thus not triggering the error condition)
        } else if (error && i == numPlayers - 1) { // If bool error is still "true" when i equals numPlayers - 1 (- 1 because array indexes start at 0)
            printf("Player not in roster.\n"); // Print error message
        }
    }
}

void FindAbove(PlayerInfo playersArray[], int numPlayers) { // Function to find players with ratings above an input value, dependent on playersArray and numPlayers
    printf("Enter a rating:\n"); // Print prompt for new rating
    int tempVal; // Declare tempVal of type int
    scanf("%d", &tempVal); // Store input as tempVal
    printf("ABOVE %d\n", tempVal); // Print "ABOVE" header
    for (int i = 0; i < numPlayers; ++i) { // For loop that loops once for each player in numPlayers
        if (tempVal < playersArray[i].rating) { // If tempVal is less than the element at index i of playerRating
            printf("Player %d -- Name: %s %s, Jersey number: %d, Rating: %d\n", i + 1, playersArray[i].firstName, playersArray[i].lastName, playersArray[i].jerseyNumber, playersArray[i].rating); // Print information about player i (i + 1 because array indexes start at 0)
        }
    }
}

void ReplacePlayer(PlayerInfo playersArray[], int numPlayers) { // Function to replace a player in the roster, dependent on playersArray and numPlayers
    printf("Enter a jersey number to replace:\n"); // Print prompt for jersey number
    int tempVal; // Declare tempVal of type int
    scanf("%d", &tempVal); // Store input as tempVal
    bool error = true; // Declare and initialize boolean that validates error checks
    for (int i = 0; i < numPlayers; ++i) { // For loop that loops once for each player in numPlayers
        if (tempVal == playersArray[i].jerseyNumber) { // If tempVal is equal to the element at index i in jerseyNumber
            printf("Enter a first name for the new player:\n"); // Prompt for first name
            scanf("%s", playersArray[i].firstName); // Store first name
            printf("Enter a last name for the new player:\n"); // Prompt for last name
            scanf("%s", playersArray[i].lastName); // Store last name
            printf("Enter a jersey number for the new player:\n"); // Prompt for jersey number
            scanf("%d", &playersArray[i].jerseyNumber); // Store jersey number
            printf("Enter a rating for the new player:\n"); // Prompt for rating
            scanf("%d", &playersArray[i].rating); // Store rating
            error = false; // Set bool error to "false" to indicate that a value has been found (thus not triggering the error condition)
        } else if (error && i == numPlayers - 1) { // If bool error is still "true" when i equals numPlayers - 1 (- 1 because array indexes start at 0)
            printf("Player not in roster.\n"); // Print error message
        }
    }
}