#include <iostream>
#include <conio.h>
#include <string>
#include "common.h"
using namespace std;

string passwordPrompt(char echoCharacter, unsigned int maxLength)
{
	string pass="";
	char key=NULL;

	do {
		key=_getch();
		
		if ( key == -32 )
		{
			// if the user hit a control key, get the next char to clear the buffer and ignore it
			key=_getch();			
		} else if ( key == 8 ) {
			// user hit a backspace, don't add a backspace char, instead remove the last char
			if ( pass.length() > 0 ) 
			{
				pass=pass.substr(0,pass.length()-1);
				if ( echoCharacter != NULL ) { cout << key << " " << key; }
			}
		} else if ( key != 13 && key != 9 ) {
			if ( maxLength == 0 || pass.length() < maxLength ) 
			{
				// add the key to the password
				pass.append(1,key);
				cout << echoCharacter;
			}
		}
	} while ( key != 13 );
	
	cout << endl;

	return pass;
}

string standardPrompt(int foreColor, int backColor, unsigned int maxLength)
{
	string text="";
	char key=NULL;

	setColor(foreColor,backColor);

	do {
		key=_getch();
		
		if ( key == -32 )
		{
			// if the user hit a control key, get the next char to clear the buffer and ignore it
			key=_getch();			
		} else if ( key == 8 ) {
			// user hit a backspace, don't add a backspace char, instead remove the last char
			if ( text.length() > 0 ) 
			{
				text=text.substr(0,text.length()-1);
				cout << key << " " << key; 
			}
		} else if ( key != 13 && key != 9 ) {
			if ( maxLength == 0 || text.length() < maxLength ) 
			{
				// add the key to the password
				text.append(1,key);
				cout << key;
			}
		}
	} while ( key != 13 );
	
	//cout << endl;

	return text;
}