// Ashton Krol
// Lab 8 
// CPSC 1010 002
// 3/14/23
//
//


#include <stdio.h>
#include <ctype.h>

#define MAX_LINE 1000
#define MAX_LINE_LEN 1000

int printMenu() {
    int menuChoice;
    while (1) {
        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) {
            return menuChoice;
        } else {
            printf("Invalid choice. Please enter a number from 1 to 4.\n");
        }
    }
}

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]) {
    for (int i = 0; text[i][0] != '\0'; i++) {
        for (int j = 0; text[i][j] != '\0'; j++) {
            if (islower(text[i][j])) {
                text[i][j] = toupper(text[i][j]);
            } else if (isupper(text[i][j])) {
                text[i][j] = tolower(text[i][j]);
            }
        }
    }
}

int main() {
    char text[MAX_LINE][MAX_LINE_LEN];
    char oppositeCase[MAX_LINE][MAX_LINE_LEN];
    int line_number = 0;

    FILE *inFile, *outFile;

    inFile = fopen("input.txt", "r");
    outFile = fopen("poe-raven.txt", "w");

    while (fgets(text[line_number], MAX_LINE_LEN, inFile)) {
        line_number++;
        if (line_number == MAX_LINE) break;
    }

    fclose(inFile);

    for (int i = 0; i < line_number; i++) {
        for (int j = 0; j < MAX_LINE_LEN; j++) {
            oppositeCase[i][j] = text[i][j];
        }
    }

    int menuChoice;

    while (1) {
        menuChoice = printMenu();
        if (menuChoice == 1) {
            printArray(text, line_number);
        } else if (menuChoice == 2) {
            printf("This poem has %d lines.\n", line_number);
        } else if (menuChoice == 3) {
            convertCase(oppositeCase);
            printArray(oppositeCase, line_number);
        } else if (menuChoice == 4) {
            break;
        }
    }

    fclose(outFile);

    return 0;
}

