/*
   Ella Patel
   March 14, 2023
   CPSC 1011 
   Lab Section 003
   Lab Assignment 8

   In this program it will print the poem, 
   read a text file and determines how many lines are in it,
   flip the letter cases and also quit when told

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 200
#define MAX_LINE_LEN 100

/* ------------------------------------------------------------ */
/* prints a menu to the user
 parameters: none
 return: the menu choice that the user enters
*/
int printMenu() {

    int menuChoice = 0;

    do {

        printf("\nChoose from the menu:\n"); 
        printf("\t1. print the poem\n"); 
        printf("\t2. show number of lines in the poem\n"); 
        printf("\t3. convert the case\n"); 
        printf("\t4. quit\n\n"); 
        printf("\t - - > "); 
        scanf("%d", &menuChoice); 
        printf("\n");

        if (!(menuChoice >= 1 && menuChoice <= 4)) {
            menuChoice = 0;
        } else {
            break;
        }

    } while(menuChoice != 4);

    return menuChoice;
}

void printArray(char text[MAX_LINE][MAX_LINE_LEN], int size) {

    for(int i = 0; i < size; i++) {
        printf("%s", text[i]);
    }

}

void convertCase(char text[MAX_LINE][MAX_LINE_LEN], int size) {

    for (int i = 0; i < size; ++i) {
        for(int j = 0; j < strlen(text[i]); ++j){
            if(text[i][j] >= 'A' && text[i][j] <= 'Z'){
                // It's uppercase
                text[i][j] += 32;
            } else if (text[i][j] >= 'a' && text[i][j] <= 'z') {
                // It's lowercase
                text[i][j] -= 32;
            }
            // printf("%c", fileDataC[i][j]);
        }
    }

    printArray(text, size);
}

int main( void ) {
    // max is 200
    char fileSharedData[MAX_LINE][MAX_LINE_LEN];
    int lineCounter = 0;
    char tmp[MAX_LINE_LEN];
    char fileData[MAX_LINE][MAX_LINE_LEN];
    int menuChoice = -1;
    FILE* fileIn = NULL;

    do {

        menuChoice = printMenu();

        if (menuChoice != 4) {
            
            switch (menuChoice)
            {
                case 1:
                    // PRINT POEM
                    fileIn = fopen("input.txt", "r");
                    lineCounter = 0;

                    while (fgets(tmp, sizeof(tmp), fileIn) != NULL) {
                        strcpy(fileData[lineCounter], tmp);
                        ++lineCounter;
                    }

                    fclose(fileIn);

                    for (int i = 0; i < lineCounter; ++i) {
                        strcpy(fileSharedData[i], fileData[i]);
                        // fprintf(stdout, "%s", fileData[i]);
                    }

                    printArray(fileData, lineCounter);

                    break;
                case 2:
                    // PRINT LINE NUMBERS
                    fileIn = fopen("input.txt", "r");
                    lineCounter = 0;

                    while (fgets(tmp, sizeof(tmp), fileIn) != NULL) {
                        ++lineCounter;
                    }

                    fclose(fileIn);

                    printf("The poem has %d lines.\n\n", lineCounter);
                    break;
                case 3:

                    fileIn = fopen("input.txt", "r");
                    lineCounter = 0;

                    while (fgets(tmp, sizeof(tmp), fileIn) != NULL) {
                        strcpy(fileData[lineCounter], tmp);
                        ++lineCounter;
                    }

                    fclose(fileIn);

                    convertCase(fileData, lineCounter);

                    

                    break;
            }

        }

    } while (menuChoice != 4);

    return 0;
}