#include "OSTime.h"
#include "OSMisc.h"

int main() 
{
	CHRONO chrono;
	double duration = 0;
	BOOL bStarted = FALSE;
	BOOL bSuspended = FALSE;
	char c = 0;
	int d = 0, h = 0, min = 0, s = 0;
	double cs = 0;

	INIT_DEBUG;

	printf("\nPress 's' to start/stop chrono, 'r' to suspend/resume, ' ' to exit.\n\n");

	while (c != ' ')
	{
		if ((bStarted)&&(!bSuspended))
		{
			GetTimeElapsedChrono(&chrono, &duration);
			printf("%f\n", duration);
		}
		c = GetUserInputTimeout(100);
		switch (c)
		{
		case 's':
			if (!bStarted) 
			{
				StartChrono(&chrono);
				bStarted = TRUE;
				bSuspended = FALSE;
				printf("Chrono started\n");
			}
			else 
			{
				StopChrono(&chrono, &duration);
				bStarted = FALSE;
				bSuspended = FALSE;
				printf("%f\n", duration);
				DecSec2DaysHoursMinSec(duration, &d, &h, &min, &s, &cs);
				printf("Chrono stopped at %fs i.e. %d days %02dh%02dmin%02ds %07.4f\n", duration, d, h, min, s, cs);
				printf("\nPress 's' to start/stop chrono, 'r' to suspend/resume, ' ' to exit.\n\n");
			}
			mSleep(100);
			break;
		case 'r':
			if (bStarted) 
			{
				if (bSuspended) 
				{
					ResumeChrono(&chrono);
					bSuspended = FALSE;
					printf("Chrono resumed\n");
				}
				else 
				{
					SuspendChrono(&chrono, &duration);
					bSuspended = TRUE;
					printf("%f\n", duration);
					DecSec2DaysHoursMinSec(duration, &d, &h, &min, &s, &cs);
					printf("Chrono suspended at %fs i.e. %d days %02dh%02dmin%02ds %07.4f\n", duration, d, h, min, s, cs);
					printf("\nPress 's' to start/stop chrono, 'r' to suspend/resume, ' ' to exit.\n\n");
				}
			}
			mSleep(100);
			break;
		default:
			break;
		}
	}

	return EXIT_SUCCESS;
}
