#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "bool.h"
#include "time.h"
#include "appt.h"

/* ReadString
 *----------------------------------------------------------------
 * Read a string from an open file (or stdin) and discard the rest of the 
line.
 * It returns EOF if encountered. (We don't use this when reading from stdin,
 * a more robust version of this code would.)
 */
int
ReadString(char *buf, int size, FILE *f)
{
    char *sptr;
    fgets(buf, size, f);
    if(feof(f))
	return EOF;
	
    if((sptr = strchr(buf, '\n'))) {
	*sptr = '\0';
    } else {
	buf[size - 1] = '\0';
	while(!feof(f) && fgetc(f) != '\n') /*skip rest of line*/
	    ;
    }
    return feof(f);
}

/* ApptInit
 *----------------------------------------------------------------
 */
void
ApptInit(Appointment *app)
{
    app->message[0] = '\0';
}

/* Empty
 *----------------------------------------------------------------
 */
boolean
Empty(Appointment app)
{
    return app.message[0] == '\0';
}

/* SetAppt
 *----------------------------------------------------------------
 */
void
SetAppt(Appointment *app, ClockTime startTime, ClockTime endTime,
	char *message, boolean fixed)
{
    app->startTime = startTime;
    app->endTime = endTime;
    strncpy(app->message, message, MAX_MESSAGE_LEN);
    app->fixed = fixed;
}

/* SetStartTime
 *----------------------------------------------------------------
 */
void
SetStartTime(Appointment *app, ClockTime startTime)
{
    app->startTime = startTime;
}

/* SetEndTime
 *----------------------------------------------------------------
 */
void
SetEndTime(Appointment *app, ClockTime endTime)
{
    app->endTime = endTime;
}


/* GetStartTime
 *----------------------------------------------------------------
 */
ClockTime
GetStartTime(Appointment app)
{
    return app.startTime;
}

/* GetEndTime
 *----------------------------------------------------------------
 */
ClockTime
GetEndTime(Appointment app)
{
    return app.endTime;
}

/* GetMessage
 *----------------------------------------------------------------
 */
char *
GetMessage(Appointment *app)
{
    return app->message;
}

/* ClearAppt
 *----------------------------------------------------------------
 */
void
ClearAppt(Appointment *app)
{
    app->message[0] = '\0';
}
	

/* PutAppointment
 *----------------------------------------------------------------
 */
void
PutAppointment(Appointment appt)
{
    char timeStr[8];
    printf("Message: %s\n", appt.message /* maxMessageWidth + 2 ..*/);
    printf("     Start time: %s", DisplayTimeString(appt.startTime, timeStr));
    
    printf("  End time: %s", DisplayTimeString(appt.endTime, timeStr));
    printf("  ");
    if(appt.fixed)
	printf("fixed\n");
    else
	printf( "not fixed\n");
}

/* SaveAppt
 *----------------------------------------------------------------
 */
void
SaveAppt(Appointment app, FILE *savefile)
{
    char timeStr[TIME_LEN];

    if(app.message[0] != '\0') {
	fprintf(savefile, "%s\n", app.message);
	fprintf(savefile, "%s ",
		DisplayTimeString (app.startTime, timeStr));
	fprintf(savefile, "%s ",
		DisplayTimeString (app.endTime, timeStr));
	if(app.fixed)
	    fprintf(savefile, "fixed\n");
	else
	    fprintf(savefile, "not fixed\n");
    }
}


/* LoadAppt
 *----------------------------------------------------------------
 * The file must end with a newline for this to work correctly
 * (it does if SaveAppt is used).
 *
 * This code looks ugly, but all we are doing is using two pointers
 * as indices into a character array (because strchr() returns a pointer
 * rather than an index).  We use strchr to find the end of each string
 * and replace the space with a '\0' so that we can use part of the buffer
 * as a string.
 */
int
LoadAppt(Appointment *app, FILE *savefile)
{
    char buffer[LARGE_SIZE]; /*make a buffer that will be "big enough"*/
    char *sptr, *tptr;
    int res;
    
    if((res = ReadString(app->message, sizeof(app->message), savefile))== EOF)
	return EOF;
	
    /*assume startTime, endTime and fixed are all on the same line. */
    fgets(buffer, sizeof(buffer), savefile);
    if(feof(savefile))
	return EOF;
    
    /*find end of startTime string*/
    sptr = strchr(buffer, ' ');
    *sptr = '\0';
    app->startTime = MakeTimeFromString(buffer);
    
    sptr++;
    while(isspace(*sptr)) sptr++;
    tptr = strchr(sptr, ' ');
    *tptr = '\0';
    app->endTime = MakeTimeFromString(sptr);
    
    tptr++;
    while(isspace(*tptr)) tptr++;
    sptr = strchr(tptr, '\n');
    *sptr = '\0';
    app->fixed = (strncmp(tptr, "fixed", sizeof("fixed")) == 0);
    
    return !EOF;
}
