// Console Echo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "RemotableInput.h"

CRemotableInput g_remotableInput;

int _tmain(int argc, _TCHAR* argv[])
{
	bool exitNow = false;
	int loopCounter = 0;

	int cmdLen = 0;
	char commandBuffer[1024];
	commandBuffer[0] = NULL;

	// Initialize input
	g_remotableInput.Initialize();

	// Simulate a startup delay
	Sleep(5000);
	g_remotableInput.OutputStartupMessage();

	// Main loop
	while(!exitNow)
	{
		// Sleep for some period of time
		Sleep(25);

		// Simulate server message
		printf("\n\n\n\n\n\n\n\nEcho to console %d...\n\n\n\n\n\n\n\n", loopCounter++);
		fflush(NULL);

		printf("\n\n\n\n");
		fflush(NULL);

		printf("\n\r");
		fflush(NULL);

		printf("\n");
		fflush(NULL);

		printf("test my ass and stuff");
		fflush(NULL);

		printf("...and this line gets longer as we go...");
		fflush(NULL);

		printf("...and we're not stopping here yet...");
		fflush(NULL);

		printf("...finally we maybe have this last section...");
		fflush(NULL);

		printf("...and maybe this section...");
		fflush(NULL);

		printf("...then we end, sort of\n.");
		fflush(NULL);

		// Check for input
		while(g_remotableInput.InputAvailable())
		{
			char charCode = g_remotableInput.GetChar();

			if(charCode != NULL)
			{
				if(charCode != '\n' && charCode != '\r')
				{
					commandBuffer[cmdLen++] = charCode;
					commandBuffer[cmdLen] = NULL;
				}
				else
				{
					// Process command
					if(stricmp(commandBuffer, "ping") == 0)
					{
						printf("Ping response...\n");
					}
					else if(stricmp(commandBuffer, "census") == 0)
					{
						char censusReplyStr[2048];
						int randomNum = rand();
						sprintf(censusReplyStr, "World Census {8B1B5C78-AB81-4c0f-B0EF-A380269E44C5}, %d\n", randomNum);
						printf(censusReplyStr);
					}
					else if(stricmp(commandBuffer, "saveallexit") == 0)
					{
						printf("Terminating (save and exit)...\n");
						exitNow = true;
					}
					if(stricmp(commandBuffer, "exit") == 0)
					{
						printf("Terminating (exit)...\n");
						exitNow = true;
					}

					// Reset command buffer
					cmdLen = 0;
					commandBuffer[0] = NULL;
				}
			}
		}
	}

	return 0;
}

