#include "Commands.h"

/*Answer::Parser*/

Answer::Parser::Parser(const String text)
{
	this->text = text;
	this->position = 0;
}

Answer::Parser::~Parser() {}

bool Answer::Parser::GetLexem(String * const result)
{
	do
	{
		if (position >= text.GetLength()) return false;
		if (text[position] == ' ') position++;
	} while (text[position] == ' ');

	String::SizeType start = position;

	while (position < text.GetLength())
	{
		if (text[position] == ' ') break; else position++;
	}

	if (result != 0) *result = text.SubString(start, position - 1);
	return true;
}

String Answer::Parser::GetString()
{
	String::SizeType start = position;
	position = text.GetLength();
	return text.SubString(start, text.GetLength() - 1);
}

/*Answer*/

Answer::Answer(const String text)
{
	Parser parser(text);
	String l;
	if (!parser.GetLexem(&l)) throw InvalidAnswer();

	if (l == "&") type = at_game; else
	if (l == "#") type = at_comment; else
	if (l == "*") type = at_event; else
	if (l == "@+" || l == "@-") type = at_system; else
	if (l == "%") type = at_who; else
		type = at_unknown;

	name = "";
	switch (type)
	{
		case at_game:
		case at_system:
		{
			parser.GetLexem(&name);
		}
		case at_who:
		{
			while (parser.GetLexem(&l))
			{
				params.PushBack(l);
			}
			break;
		}

		default:
		{
			name = parser.GetString();
			break;
		}
	}
}

