/* 
Rebecca Han 
03/27/2023
CPSC 1011 Section 003
Lab 9 
arrayProccesing.c

This file contains three functions necessary to compile and run 
Lab 9, which are: initArray, printArray, and getWorkout. 
They will read in the file, scan in user input to get workout data, 
and then print a workout.
*/


#include "defs.h"


// initializes the array of exercises
// inputs:  the size of the array, the array, and file pointer
// output:  none
void initArray(int arraySize, exercise workout[], FILE *inFile) {
	int i;
	for (i =0; i < arraySize; i++) {
		fscanf(inFile, "%19s %39s %d %d %d %d", workout[i].name, workout[i].muscles, 
		&workout[i].weight, &workout[i].time, &workout[i].sets, &workout[i].reps);
}
}

// prints the array
// inputs:  the size of the array and the array
// output:  none
void printArray(int arraySize, exercise workout[]) {
	int i;
	printf("\n%-25s %-36s %5s %5s %5s %5s\n", 
	       "EXERCISE", "MUSCLES", "WEIGHT",
			 "TIME", "SETS", "REPS");
	for (i = 0; i < arraySize; i++) {
		printf("%2d. %-21s %-35s %5d %5d %5d %5d\n", 
				 i+1,
				 workout[i].name,
				 workout[i].muscles,
				 workout[i].weight,
				 workout[i].time,
				 workout[i].sets,
				 workout[i].reps);
	}
	printf("\n");
}


// fills in the array with the values from the user to create
//		their workout plan
// note the call to freopen which sets the stdin back to the terminal
// inputs:  the size of the array and the array
// output:  none

void getWorkout(int arraySize, exercise workout[]) {
	// In a loop, let the user enter values for each item in the workout plan
	int notused = 0;
	for (int i = 0; i < arraySize; i++) {
		printf("%s for %s:\n", workout[i].name, workout[i].muscles);
		if (strcmp(workout[i].name, "hip_thrust") == 0 || 
			strcmp(workout[i].name, "russian_twists") == 0) {
			printf("Weight:\n");
			scanf("%i", &workout[i].weight);
			printf("Time:\n");
			scanf("%i", &workout[i].time);
			printf("Sets:\n");
			scanf("%i", &workout[i].sets);
			printf("Reps:\n");
			scanf("%i", &workout[i].reps);
			printf("\n");
		} else if (strcmp(workout[i].name, "leg_raises") == 0 || 
			strcmp(workout[i].name, "situps/crunches") == 0) {
			printf("Time:\n");
			scanf("%i", &workout[i].time);
			printf("Sets:\n");
			scanf("%i", &workout[i].sets);
			printf("Reps:\n");
			scanf("%i", &workout[i].reps);
			printf("\n");
			workout[i].weight = notused;
		} else {
			printf("Weight:\n");
			scanf("%i", &workout[i].weight);
			printf("Sets:\n");
			scanf("%i", &workout[i].sets);
			printf("Reps:\n");
			scanf("%i", &workout[i].reps);
			printf("\n");
			workout[i].time = notused;
		}
	}
}

