/* 
    CPSC-1011-003
    Lab 06
    Collaborators: Owen, Angelina
*/

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

int main(void) { // Start main function

    const int numPlayers = 5; // Declare and initialize constant for 5 players
    
    int jerseyNumber[numPlayers]; // Declare integer array dependent on numPlayers to store jersey numbers
    int playerRating[numPlayers]; // Declare integer array dependent on numPlayers to store player ratings

    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

    int tempVal; // Declare integer that will be used as a temporary value in certain if/else statements
    bool error = true; // Declare and initialize boolean that validates error checks

    /*
        The method of using a boolean for error checking probably isn't ideal, but I couldn't (for the life of me) get working
        error checking conditions using else if statements. This works for now.
    */

    for (int i = 0; i < numPlayers; ++i) { // For loop that loops once for each player in numPlayers
        printf("Enter player %d's jersey number:\n", i + 1); // Print prompt for player's jersey number (plus 1 because array indexs start at 0)
        scanf("%d", &jerseyNumber[i]); // Store jersey number to array at element corresponding to player number
        printf("Enter player %d's rating:\n", i + 1); // Print prompt for player's rating (plus 1 because array indexs start at 0)
        scanf("%d", &playerRating[i]); // Store player rating to array at element corresponding to player number
        printf("\n"); // Print new line
    }
    printf("\n"); // Print new line

    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("\n"); // Print new line
        printf("Choose an option:\n"); // Print choose prompt
        scanf(" %c", &menuInput); // Store user input for menuInput
        /*
            It's probably better to output all the menu options in one line with "\n" in between each, but this makes more sense to my brain.
        */

        if (menuInput == 'o') { // If menuInput is 'o'
            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 -- Jersey number: %d, Rating: %d\n", i + 1, jerseyNumber[i], playerRating[i]); // Print information about player i (i + 1 because array indexes start at 0)
            }
            printf("\n"); // Print new line
        } else if (menuInput == 'u') { // If menuInput is 'u'
            printf("Enter a jersey number:\n"); // Print prompt for jersey number
            scanf("%d", &tempVal); // Store input as tempVal to be checked against jerseyNumber array
            printf("Enter a new rating for player:\n"); // Print prompt for the player's new rating
            for (int i = 0; i < numPlayers; ++i) { // For loop that loops once for each player in numPlayers
                if (tempVal == jerseyNumber[i]) { // If tempVal is equal to the element at index i of jerseyNumber
                    scanf("%d", &playerRating[i]); // 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)
                    scanf("%d", &tempVal); // Store the next input as tempVal, in case it's being provided as a command-line argument (otherwise the outer while() loop breaks)
                    /*
                        Realistically speaking, a user isn't going to be providing the rating value ahead of time. The test case should really be changed to reflect that, because there's no need to
                        be storing more information here.
                    */
                    printf("Player not in roster.\n"); // Print error message
                    /*
                        DON'T return -1 here, because doing so would result in the program ending.
                    */
                }
            }
            printf("\n"); // Print new line
        } else if (menuInput == 'a') { // If menuInput is 'a'
            printf("Enter a rating:\n"); // Print prompt for new rating
            scanf("%d", &tempVal); // Store input as tempVal
            printf("\n"); // Print new line
            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 < playerRating[i]) { // If tempVal is less than the element at index i of playerRating
                    printf("Player %d -- Jersey number: %d, Rating: %d\n", i + 1, jerseyNumber[i], playerRating[i]); // Print information about player i (i + 1 because array indexes start at 0)
                }
            }
            printf("\n"); // Print new line
        } else if (menuInput == 'r') { // If menuInput is 'r'
            printf("Enter a jersey number:\n"); // Print prompt for jersey number
            scanf("%d", &tempVal); // Store input as tempVal
            for (int i = 0; i < numPlayers; ++i) { // For loop that loops once for each player in numPlayers
                if (tempVal == jerseyNumber[i]) { // If tempVal is equal to the element at index i in jerseyNumber
                    printf("Enter a new jersey number:\n"); // Print prompt for new jersey number
                    scanf("%d", &jerseyNumber[i]); // Store input as element at index i of jerseyNumber
                    printf("Enter a rating for the new player:\n"); // Print prompt for new rating
                    scanf("%d", &playerRating[i]); // Store input as element 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)
                    scanf("%d", &tempVal); // Store the next input as tempVal, in case it's being provided as a command-line argument (otherwise the outer while() loop breaks)
                    /*
                        Yet again, the above line shouldn't be necessary.
                    */
                    printf("Player not in roster.\n"); // Print error message
                    /*
                        DON'T return -1 here, because doing so would result in the program ending.
                    */
                }
            }
            printf("\n"); // Print new line
        } 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)
        }

        tempVal = 0; // Set tempVal to 0
        /*
            The above line technically isn't necessary, but I figured I'd rather not have any issues because I didn't include it.
        */
        error = true; // Set bool error to true so that error conditions don't fire when they aren't supposed to on the next loop through
    }

    return 0; // Return end code

}