#pragma once

TCHAR inchar()
{
	TCHAR ch = _getche();
	if (ch == 0)
		ch = _getche();
	return ch;
}
bool isInside(const tstring& str, TCHAR c)
{
	return str.find(c) != tstring::npos;
}

bool inintesc(int32_t& in32, bool& bEnter, bool& bChar, TCHAR* szSel)
{
	tstring str(_T(""));
	TCHAR ch;
	bEnter = false; bChar = false;
	while ((ch = inchar()) != ESC) {
		ch = toupper(ch);
		if (ch == ENTER)
		{
			bEnter = true;
			break;
		}
		else if (szSel && isInside(tstring(szSel), ch))
		{
			bChar = true;
			in32 = ch;
			break;
		}
		else if (isalpha(ch))
		{
			// character alphabetic but not in list of accepted commands.
			Beep(1568, 150);
			tcout << _T("\b \b");
			/*Beep(1568, 150);
			Beep(1568, 150);
			Beep(1245, 500);
			Beep(1397, 150);
			Beep(1397, 150);
			Beep(1397, 150);
			Beep(1175, 500);*/
		}
		else
			str += ch;
	}
	// !bEnter means input aborted with ESC
	bool bESC = (!bEnter && !bChar);
	// len 0 means nothing input
	// return false if ESC or ENTER w/ no input typed
	if (bESC || (bEnter && (str.length() == 0)))
		return false;

	if (!bChar)
		tstringstream(str) >> in32;

	return true;
}
int inputint(int default)
{
	int nreturn = default;
	TCHAR input[20];
	tcin.getline(input, 20);
	if (tcslen(input) != 0) // just Enter was typed
	{
		tstring strinput(input);
		tstringstream(strinput) >> nreturn;
	}
	return nreturn;
}
float inputfloat(float default)
{
	float freturn = default;
	TCHAR input[20];
	tcin.getline(input, 20);
	if (tcslen(input) != 0) // just Enter was typed
	{
		tstring strinput(input);
		tstringstream(strinput) >> freturn;
	}
	return freturn;
}
