#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "apptcal.h"
#include "time.h"


/* BookAppt
 *----------------------------------------------------------------
 * Prompt for a message, the day, the start and end times, and whether
 * the times are fixed, then enter them in the appointment book.
 */
void
BookAppt(Calendar *Cal)
{
    char message[MAX_MESSAGE_LEN];
    char day[DAY_LEN];
    char startTime[TIME_LEN], endTime[TIME_LEN];
    char fixedStr[6];
    boolean fixed, success;
    int res1, res2;
    
    printf("Booking an appointment:\n");
    
    printf("  Enter the text to be stored with the appointment: ");
    ReadString(message, sizeof(message), stdin);

    printf("  Enter day: ");
    ReadString(day, sizeof(day), stdin);

    printf("  Enter start time: ");
    ReadString(startTime, sizeof(startTime), stdin);

    printf("  Enter end time: ");
    ReadString(endTime, sizeof(endTime), stdin);

    printf("  Enter 'true' if the time should be fixed, 'false' otherwise: ");
    ReadString(fixedStr, sizeof(fixedStr), stdin);
    
    while((res1 = (strcmp(fixedStr, "true") != 0)) &&
	  (res2 = (strcmp(fixedStr, "false") != 0))) {
	printf(" You must enter 'true' if the time should be fixed, 'false' otherwise: ");

	ReadString(fixedStr, sizeof(fixedStr), stdin);
    }
    if(res1 == 0){
	fixed = TRUE;
	assert(res2 != 0);
    } else if(res2 == 0) 
	fixed = FALSE;

    success = CalendarBook(Cal, MakeDayFromString(day),
			   MakeTimeFromString(startTime),
			   MakeTimeFromString(endTime), message, fixed);

    if(success)
	printf("  Appointment entered.\n");
    else {
	printf("  The booking was unsuccessful, probably due to another\n");
        printf("  appointment in the same time slot.\n");
    }
}


/* FitAppt
 *----------------------------------------------------------------
 * Prompt for a message and fit it into the calendar.
 */
void
FitAppt(Calendar *Cal)
{
    boolean success;
    char message[MAX_MESSAGE_LEN];
    char day[DAY_LEN];
    char totalTime[TIME_LEN];
    int res1, res2;
    char fixedStr[TIME_LEN];
    boolean fixed;
    ClockTime startTime, endTime;
    
    printf("Fitting an appointment:\n");

    printf("  Enter the text to be stored with the appointment: ");
    ReadString(message, sizeof(message), stdin);
    
    printf("Enter day to fit appointment into: ");
    ReadString(day, sizeof(day), stdin);

    printf("  Enter total time the appointment will take: ");
    ReadString(totalTime, sizeof(totalTime), stdin);

    printf("  Enter 'true' if the time should be fixed, 'false' otherwise: ");
    ReadString(fixedStr, sizeof(fixedStr), stdin);
    
    while((res1 = (strcmp(fixedStr, "true") != 0)) &&
	  (res2 = (strcmp(fixedStr, "false") != 0))) {
	printf(" You must enter 'true' if the time should be fixed, 'false' otherwise: ");
	ReadString(fixedStr, sizeof(fixedStr), stdin);
    }
    if(res1 == 0){
	fixed = TRUE;
	assert(res2 != 0);
    } else if(res2 == 0) 
	fixed = FALSE;

    success = CalendarFit(Cal, MakeDayFromString(day),
			  MakeTimeFromString(totalTime), message, fixed,
			  &startTime, &endTime);

    if(success){
        printf("  Appointment %s fit at ", message);
	DisplayTime(startTime);
	printf(" ");
	DisplayTime(endTime);
	printf("\n");
    } else {
        printf("  The fitting was unsuccessful.\n");
    }

}


/* LocateAppt
 *----------------------------------------------------------------
 * Prompt for a message and find it in the calendar.
 */
void
LocateAppt(Calendar *Cal)
{
    Workday day;
    ClockTime startTime, endTime;
    boolean success;
    char message[MAX_MESSAGE_LEN];
    
    printf("Enter message to look for: ");
    ReadString(message, sizeof(message), stdin);


    success = CalendarFind(Cal, message, &day, &startTime, &endTime);
    
    if(success){
        printf("%s ", message);
	DisplayDay(day);
	printf(" ");
        DisplayTime(startTime);
        printf(" ");
        DisplayTime(endTime);
    }  else
	printf("No appointments match '%s'\n", message);
}



/* CancelAppt
 *----------------------------------------------------------------
 * Prompt for a message, find it, and cancel it.
 */
void
CancelAppt(Calendar *Cal)
{
    char message[MAX_MESSAGE_LEN];
    printf("Enter message of appointment to cancel: ");
    ReadString(message, sizeof(message), stdin);


    CalendarCancel(Cal, message);
}


/* ViewDay
 *----------------------------------------------------------------
 * Print the current day's appointments.
 */
void
ViewDay(Calendar *Cal)
{
    char res[2];
    char day[DAY_LEN];
    
    printf("   Do you want to view today's appointments (y/n)? ");
    ReadString(res, sizeof(res), stdin);
    if(res[0]== 'y') {
	CalendarViewToday(Cal);
    } else {
	printf("  Which day? ");
	ReadString(day, sizeof(day), stdin);
	CalendarViewDay(Cal, MakeDayFromString(day));
    }
}


/* NextDay
 *----------------------------------------------------------------
 * Move on to the next day; clear today's appointments.
 */
void
NextDay(Calendar *Cal)
{
    CalendarNextDay(Cal);
}


/* ViewWeek
 *----------------------------------------------------------------
 * Print the whole week's appointments.
 */
void
ViewWeek(Calendar *Cal)
{
    CalendarViewWeek(Cal);
}


/* ClearDay
 *----------------------------------------------------------------
 * Cancel today's appointments.
 */
void
ClearDay(Calendar *Cal)
{
    char res[2];
    char day[DAY_LEN];
    
    printf("  Do you want to delete today's appointments (y/n)? ");
    ReadString(res, sizeof(res), stdin);
    if(res[0]== 'y') {
	CalendarClearDay(Cal, Cal->currentDay);
    } else {
	printf("  Which day? ");
	ReadString(day, sizeof(day), stdin);
	CalendarClearDay(Cal, MakeDayFromString(day));
    }
}

/* ClearWeek
 *----------------------------------------------------------------
 * Cancel all appointments.
 */
void
ClearWeek(Calendar *Cal)
{
    char res[2];
    
    printf("  Are you sure you want to delete all appointments (y/n)? ");
    ReadString(res, sizeof(res), stdin);
    
    if(res[0] == 'y')
        CalendarClearWeek(Cal);
}


/* SaveAppts
 *----------------------------------------------------------------
 * Save appointments to file.
 */
void
SaveAppts(Calendar *Cal)
{
    CalendarSave(Cal);
}


/* LoadAppts
 *----------------------------------------------------------------
 * Load appointments from file.
 */
void
LoadAppts(Calendar *Cal)
{
    CalendarLoad(Cal);
}


/*----------------------------------------------------------------
 * Begin main program.
 *----------------------------------------------------------------
 * Continually get commands from the user and process it.
 */

void
main()
{
    char command[3];
    char res[2];
    Calendar Cal;
    
    ApptCalInit(&Cal);
    
    printf("Appointment Calendar program\n");
    printf("Available commands:\n");
    printf("  Book appointment (ba)\n");
    printf("  Fit appointment (fa)\n");
    printf("  Locate appointment (la)\n");
    printf("  Cancel appointment (ca)\n");
    printf("  View day (vd)\n");
    printf("  Next day (vn)\n");
    printf("  View week (vw)\n");
    printf("  Clear day (cd)\n");
    printf("  Clear week (cw)\n");
    printf("  Save (s)\n");
    printf("  Load (l)\n");
    printf("  Quit (q)\n\n");

    /* Read and handle each command.*/
    for(;;) {
        printf("Command: ");
	ReadString(command, sizeof(command), stdin);

        printf("\n");

	if(strncmp(command, "ba", 2) == 0)
	    BookAppt(&Cal);
	else if(strncmp(command, "fa", 2) == 0)
	    FitAppt(&Cal);
	else if(strncmp(command, "la", 2) == 0)
	    LocateAppt(&Cal);
	else if(strncmp(command, "ca", 2) == 0)
	    CancelAppt(&Cal);
        else if(strncmp(command, "vd", 2) == 0)
	    ViewDay(&Cal);
        else if(strncmp(command, "nd", 2) == 0)
	    NextDay(&Cal);
        else if(strncmp(command, "vw", 2) == 0)
	    ViewWeek(&Cal);
        else if(strncmp(command, "cd", 2) == 0)
	    ClearDay(&Cal);
        else if(strncmp(command, "cw", 2) == 0)
	    ClearWeek(&Cal);
        else if(strncmp(command, "s", 1) == 0)
	    SaveAppts(&Cal);
        else if(strncmp(command, "l", 1) == 0)
	    LoadAppts(&Cal);
        else if(strncmp(command, "q", 1) == 0) {
	    
	    printf("Do you want to save the current appointments?");
	    ReadString(res, sizeof(res), stdin);
	    if(res[0] == 'y')
		SaveAppts(&Cal);
	    break;
	    
	} else 
	    printf("Whoops, incomprehensible command: %s", command);
        
        printf("\n");
    }
    printf("Program ended.\n");
    exit(0);
}
