/*
 *  ScreenPrint.cpp
 *  AudioUIdemo
 *
 *	Code and Sound Design by Peter "pdx" Drescher.
 *  Copyright 2010 twittering machine. All rights reserved.
 *
 *	This class produces the keyword responses used for screen printing mode.
 *	It keeps track of what the user typed in, and checks to see if a keyword is present when the user hits "return".
 *	If so, it formats the appropriate response string:
 * 		"search"					- do a google search, and return the labels
 *		"hello"						- pick a canned response at random
 *		"goodbye"					- pick a canned response at random
 *		"clear"						- clear the screen
 *		"you know what i meant"		- this 'macro' was programmed into the first IBM mainframe I ever worked on,
 *									  as a reminder that computers take everything literally.
 *		"fingerprint"				- a CSI-style fingerprint matching routine, to be implemented in the next version
 *									  (when I figure out how to hack the FBI database :)
 *		"?"							- a "magic 8 ball" routine (random canned response to a question)
 *		"list"						- prints out the list of keywords
 *		"ascii"						- prints out the ascii character set (useful for listening to sound sets and algorithms)
 *	response strings are ended with '/r' (carriage return), which tells the AudioUImockup module to end screen printing mode.
 */

#include "ScreenPrint.h"
#include "GetSearchString.h"

#define MAX_KEYWORDS 9

GetSearchString getSearchString;

void ScreenPrint::init()
{
	srand ( time(NULL) );
	typedIn = "";	
}

// keep track of what the user typed on the keyboard
void ScreenPrint::appendCharToTypedIn(char charTypedIn)
{
	if (charTypedIn == -1)									//backspace key pressed
	{
		if (typedIn.size() > 0)
		{
			typedIn = typedIn.erase (typedIn.size() - 1,1); // erase the last character of string
		}
	}
	else
	{
		typedIn += charTypedIn;								// append character to end of string
	}
}

string ScreenPrint::checkForKeyword ()
{
	static const string keyWords[] =
		{"search",
		 "hello",
		 "goodbye",
		 "clear",
		 "you know what i meant",
		 "fingerprint",
		 "?",
		 "list",
		 "ascii"};

	keyWordResponse = "";
	// set string to lowercase
	for (int i = 0; typedIn[i] != '\0'; i++)			
		typedIn[i] = (char)tolower(typedIn[i]);				

	for (int ctr = 0; ctr < MAX_KEYWORDS; ctr++)
	{
		if (typedIn.find(keyWords[ctr], 0) != string::npos)
		{
			switch (ctr)
			{
				case 0: //Search
					keyWordResponse = ScreenPrint::keyWordRespondSearch();
					return (keyWordResponse += '\r');
					break;
					
				case 1: //Hello
					keyWordResponse = ScreenPrint::keyWordRespondHello();
					return (keyWordResponse += '\r');
					break;
					
				case 2: //Goodbye
					keyWordResponse = ScreenPrint::keyWordRespondGoodbye();
					return (keyWordResponse += '\r');
					break;
					
				case 3: //Clear
					keyWordResponse = "clear";
					return (keyWordResponse += '\r');
					break;
					
				case 4: //Meaning
					keyWordResponse = "I cannot do what you mean -- I can only do what you say";
					return (keyWordResponse += '\r');
					break;
					
				case 5: //Fingerprint
					keyWordResponse = "FBI fingerprint database currently unavailable";
					return (keyWordResponse += '\r');
					break;
					
				case 6: //Question mark
					keyWordResponse = ScreenPrint::keyWordRespondQuestion();
					return (keyWordResponse += '\r');
					break;
					
				case 7: //List
					for (int x = 0; x < MAX_KEYWORDS; x++) {
						keyWordResponse += keyWords[x];
						if (x < (MAX_KEYWORDS - 1)) {
							keyWordResponse += '\f'; //will be processed as a "line feed" character
						} 
					}
					return (keyWordResponse += '\r');
					break;
					
				case 8: //ASCII
					for (int x = 32; x < 127; x++) {
						keyWordResponse += x;
					}
					return (keyWordResponse += '\r');
					break;
					
				default:
					break;
			}
		}
		else
		{
			//std::cout << "no match \n";
		}
	}
	//std::cout << "no keyword found \n";
	return (keyWordResponse = "");
}

string ScreenPrint::keyWordRespondHello()
{
	static const string HelloResponse[] =
	{	"Hi There",
		"Well, hello there!",
		"What up, dude?",
		"Hi!",
		"Hmm, what did you expect me to say 'hello' too? How cliche!",
		"Bonjour! (I'm feeling kinda French today)",
		"Bugger off ... I'm not interested",
		"Why helloooooooo!! I'm so glad you dropped by! Is there anything I can do for you?",
		"What do YOU want!?",
		"You say Hello and I say Goodbye (I love the Beatles!)"
	};
	
	return HelloResponse[(rand() % 10)];
}

string ScreenPrint::keyWordRespondGoodbye()
{
	static const string GoodbyeResponse[] =
	{	"Later, bitches!",
		"So Long",
		"Buh-Bye!",
		"Sorry to see you go, but love to watch you leave!",
		"See ya later, alligator",
		"Au Revoir! (I'm feeling kinda French today)",
		"Frak off, don't bother me ...",
		"Aww, do you have to leave so soon?",
		"OK, so, whaddya waitin' for, get outa here!",
		"You say Goodbye and I say Hello (I still love the Beatles!)"
	};
	
	return GoodbyeResponse[(rand() % 10)];
}

// scan for characters enclosed by quotes, delineate words with plus sign, then google search it
string ScreenPrint::keyWordRespondSearch()
{
	string searchString = "";
	string formattedLabels = "";
	bool foundQuoteMark = false;
	for (int i = 0; typedIn[i] != '\0'; i++)
		if (typedIn[i] == '\"')
		{
			if (foundQuoteMark)
			{
				formattedLabels = getSearchString.fetchHTMLfromGoogle(searchString);
				return formattedLabels;
			}
			else
			{
				foundQuoteMark = true;
			}
		}
		else
		{
			if (foundQuoteMark)
			{
				if (typedIn[i] == ' ')
				{
					searchString += "+";
				}
				else
				{
					searchString += typedIn[i];
				}
			}
			else
			{
				//std::cout << "ignoring " << typedIn[i] << " (outside of quotes) \n";
			}
		}
	
	searchString = "incorrectly formatted search parameters:";
	searchString += '\f'; //will be processed as a "line feed" character
	searchString += "put what you want to look for in quotes.";
	return searchString;
}

string ScreenPrint::keyWordRespondQuestion()
{
	static const string QuestionResponse[] =
	{	"What, you think I know?",
		"I know the answer to that question, but I'm not telling YOU!",
		"Signs point to 'yes' ...",
		"Ask again later",
		"Hmm, lemme think about that one for a minute ...",
		"Mais oui!! (I'm still feeling French today)",
		"Waddya askin' me for? I ain't no damn crystal ball, ya know!!",
		"If the day of the month is even, and the time of day is odd, then the  answer is most likely NO ... BUT if the day is odd AND the time is odd,then the answer is Definitely YES! *except* when the moon is full, thenall answers are always MAYBE ... ",
		"42",
		"Got No Clue."
	};
	
	return QuestionResponse[(rand() % 10)];
}
