/* ---------------------------------------------------------------------

Name: Joseph Curtis
Student Number: 3952239
Unit: COMP2300
Assignment Number: 1
Name of this file: aardvark.c
Lab Group: Monday 15-17


I declare that the material I am submitting in this file is entirely
my own work.  I have not collaborated with anyone to produce it, nor
have I copied it, in part or in full, from work produced by someone else.

--------------------------------------------------------------------- */

/* Includes */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

/* Route structure:
   This is the structure in which all data about a particluar route */
struct route
{
	int flight_number;
	char airport_depart_code[4];
	char airport_arrive_code[4];
	char departure_time[6];
	char arrival_time[6];
};

/* Prototypes of all functions*/
int menu(int);
struct route flight_number(struct route);
struct route departure_code(struct route);
struct route arrival_code(struct route);
struct route departure_time(struct route);
struct route arrival_time(struct route);
void print_info(struct route);
void blank_route(struct route*);
void write_file(struct route);
int valid_time(char[5]);
int complete_route_data(struct route);
void read_code(char [], char []);
void save_route_data(struct route, char []);

/* Function for the printing and data receival of the main menu.
   This function returns a different integer value corresponding
   to the menu item chosen by the user */
int menu(int welcome_message)
{
	/* The variable welcome_message indicates if the welcome message and
	   the list of menu optins are to be displayed */

	char input[100];
	char upper_input[3];
	int i;

	if(welcome_message)
	{
		printf("\n   Welcome to Aardvark Airlines");
		printf("\nFlight and route data entry program\n");
		printf("\n  Select your input option");
	}
	do
	    {
		if(welcome_message){
			printf("\n    Qu : end data entry");
			printf("\n    Nu : flight number");
			printf("\n    Fr : departure airport code");
			printf("\n    To : arrival airport code");
			printf("\n    De : time of departure");
			printf("\n    Ar : time of arrival\n\n");
		}

		/* Receive the input command and check if it is a valid command*/
		scanf("%s", input);

		for(i=0;i<2;i++)
		{
			upper_input[i]=toupper(input[i]);
		}

		if (strncmp(upper_input, "QU", 2)==0) {
			return 1;
		}
		else if (strncmp(upper_input, "NU", 2)==0) {
			return 2;
		}
		else if (strncmp(upper_input, "FR", 2)==0) {
			return 3;
		}
		else if (strncmp(upper_input, "TO", 2)==0) {
			return 4;
		}
		else if (strncmp(upper_input, "DE", 2)==0) {
			return 5;
		}
		else if (strncmp(upper_input, "AR", 2)==0) {
			return 6;
		}
		else {
			printf("%s - is invalid input selection from", input);
			welcome_message=1;
		}
	}
	while(1);
	return 0;
}

struct route flight_number(struct route temp_route)
{
	int temp_flight_number;
	int i;
	char flight_number_string[100];
	char temp_query;
	i=0;

	do
	    {
		printf("Input flight number\n");
		scanf("%s", flight_number_string);

		if ((temp_flight_number=atoi(flight_number_string)))
		{
			if (temp_flight_number > 99 && temp_flight_number < 1000)
			{
				printf("Is the flight number %i? ([y]/n)\n", temp_flight_number);

				while (!getchar()){
				}
				temp_query=getchar();
				if(temp_query!=10){
					for(i=0;getchar()!=10;i++){
					}
				}

				if(toupper(temp_query)=='Y'||temp_query==10||temp_query==13)
				{
					temp_route.flight_number=temp_flight_number;
/*					print_info(temp_route);*/
					return temp_route;
				}
			}
			else
			    {
				printf("**Invalid flight number %i **", temp_flight_number);
				printf("\n  (Require integer from 100 - 999)\n");
			}
		}
		else
		    {
			printf("**Invalid flight number **\n");
		}
	}
	while(1==1);
	return temp_route;
}

void print_info(struct route temp)
{
	printf("\n=================================");
	printf("\n      Flight Information");
	printf("\nNumber  From   To    Dept     Arr");
	if(temp.flight_number==0)
	{
		printf("\n   ---%6s%5s%8s%8s", temp.airport_depart_code,
				temp.airport_arrive_code, temp.departure_time, temp.arrival_time);
	}
	else
	    {

		printf("\n%6i%6s%5s%8s%8s", temp.flight_number, temp.airport_depart_code,
				temp.airport_arrive_code, temp.departure_time, temp.arrival_time);
	}
	printf("\n=================================");
	printf("\n\n    Select next input option");
	printf("\n     to enter missing data\n");
}

void blank_route(struct route *blank)
{
	blank -> flight_number=0;
	strcpy(blank->airport_depart_code, "---");
	strcpy(blank->airport_arrive_code, "---");
	strcpy(blank->departure_time, "--:--");
	strcpy(blank->arrival_time, "--:--");
}

void read_code(char type[], char code_target[])
{
	char temp_code[100];
	char temp_query;
	int i;

	do
	    {
		printf("Input 3 character code for %s airport\n", type);
		scanf("%s", temp_code);
		strlcpy(temp_code,temp_code,4);

		/* check for characters */
		temp_code[0]=toupper(temp_code[0]);
		temp_code[1]=toupper(temp_code[1]);
		temp_code[2]=toupper(temp_code[2]);

		if(temp_code[0]>64&&temp_code[0]<91&&
			    temp_code[1]>64&&temp_code[1]<91&&
			    temp_code[2]>64&&temp_code[2]<91)
		{
			printf("Is the %3s airport code %s? ([y]/n)\n", type, temp_code);

			while (!getchar()){
			}
			temp_query=getchar();
			if(temp_query!=10){
				for(i=0;getchar()!=10;i++){
				}
			}
			if(toupper(temp_query)=='Y'||temp_query==10||temp_query==13)
			{
				/* Do this if it is a valid code */

				code_target[0]=temp_code[0];
				code_target[1]=temp_code[1];
				code_target[2]=temp_code[2];
				code_target[3]='\0';
				return;
			}
		}
		else
		    {
			/* invalid code */
			printf("**Invalid Airport Code %s **", temp_code);
			printf("\n  (Require three characters\n");
		}
	}
	while(1);
}

struct route departure_code(struct route temp)
{
	char temp_code[4];
	read_code("departure", temp_code);
	strcpy(temp.airport_depart_code, temp_code);
	/*print_info(temp);*/
	return temp;
}

struct route arrival_code(struct route temp)
{
	char temp_code[4];
	read_code("arrival", temp_code);
	strcpy(temp.airport_arrive_code, temp_code);
	return temp;
}

int valid_time(char temp[6])
{
	/* Returns 0 if invalid time, 1 if valid format but invalid time (e.g. 24:60),
	returns 2 if completely valid time */

	char hours[3];
	char minutes[3];

	hours[0]=temp[0];
	hours[1]=temp[1];
	hours[2]=0;
	minutes[0]=temp[3];
	minutes[1]=temp[4];
	minutes[2]=0;

	if(isdigit(temp[0])&&isdigit(temp[1])&&(temp[2]==':')&&isdigit(temp[3])&&isdigit(temp[4]))
	{
		if((atoi(hours)<24)&&(atoi(minutes)<60))
		{
			return 2;
		}
		else
		    {
			return 1;
		}
	}
	return 0;
}

void read_time(char type[], char time_target[])
{
	char temp_time[100];
	char temp_query;
	int i;

	do
	    {
		printf("Input %s time as 09:02\n", type);
		scanf("%s", temp_time);
		/*strlcpy(temp_time, temp_time, 6);*/
		temp_time[5]='\0';
		/*printf("\n\n%s\n\n"temp_time*/
		switch (valid_time(temp_time))
		{
		case 0:
			printf("\n**Invalid Time %s **\n", temp_time);
			printf("  (Require format 23:34)\n");
			break;
		case 1:
			printf("Invalid time - enter again\n");
			break;
		case 2:

			printf("Is the %s time %s? ([y]/n)\n", type, temp_time);

			while (!getchar()){
			}
			temp_query=getchar();
			if(temp_query!=10){
				for(i=0;getchar()!=10;i++){
				}

				/*      for(i=0;i<3;i++){getchar();}*/
			}
			if(toupper(temp_query)=='Y'||temp_query==10||temp_query==13)
			{
				/* Do this if it is a valid time */
				strlcpy(time_target, temp_time, 6);
/*				print_info(temp);*/
				return;
			}


			break;
		default:
			printf("Error in switch statement in function departure_time!!\n\n");
			break;
		}

	}
	while(1);
}


struct route departure_time(struct route temp)
{
	char temp_time[6];
	read_time("departure", temp_time);
	strcpy(temp.departure_time, temp_time);
	/*print_info(temp);*/
	return temp;
}

struct route arrival_time(struct route temp)
{
	char temp_time[6];
	read_time("arrival", temp_time);
	strcpy(temp.arrival_time, temp_time);
	/*print_info(temp);*/
	return temp;
}


int complete_route_data(struct route temp)
{
	if((temp.flight_number>0)&&
		    (temp.airport_depart_code[0]!='-')&&
		    (temp.airport_arrive_code[0]!='-')&&
		    (temp.departure_time[0]!='-')&&
		    (temp.arrival_time[0]!='-'))
	{
		return 1;
	}
	else
    {
		return 0;
	}

}

void save_route_data(struct route temp, char name[])
{
	FILE *fileout;
	fileout = fopen(name, a);

	fclose(fileout);
}

int main()
{
	int welcome_message;
	struct route current_route;
	int quit;

	welcome_message=1;
	quit=0;

	blank_route(&current_route);

	do
	    {

		switch(menu(welcome_message))
		{
		case 0:
			/* Error message here */
			break;
		case 1:
			/* Quit */
			printf("Terminating data entry");
			printf("\nEnd of Program\n");
			quit=1;
			break;
		case 2:
			/* flight number */
			current_route=flight_number(current_route);
			print_info(current_route);
			welcome_message=0;
			break;
		case 3:
			/* departure airport code */
			current_route=departure_code(current_route);
			print_info(current_route);
			welcome_message=0;
			break;
		case 4:
			/* arrival airport code */
			current_route=arrival_code(current_route);
			print_info(current_route);
			welcome_message=0;
			break;
		case 5:
			/* time of departure */
			current_route=departure_time(current_route);
			print_info(current_route);
			welcome_message=0;
			break;
		case 6:
			/* time of arrival */
			current_route=arrival_time(current_route);
			print_info(current_route);
			welcome_message=0;
			break;
		default:
			printf("There is an error in this code!\n\nMain manu return value");
			break;
		}

		if(complete_route_data(current_route))
		{
			save_route_data(current_route, "AARDVARK.ROUTES");
		}

	}
	while (!quit);
	return 0;
}

