#include <cstdio>
#include <cstdlib>

#include "GameBot.h"
#include "Common.h"
#include "String.h"

void onError(const String error)
{
	printf("%s\n", error.CStr());
	exit(-1);
}

int main(int argc, char *argv[])
{
	if (argc < 5) onError("Invalid arguments.");

	GameBot bot(argv[3]);
	bot.Connect(argv[1], ToUnsigned(argv[2]));

	if (argc == 5) bot.Join(); else
		if (String(argv[5]) == "-j")
		{
			if (argc == 6) onError("Game number expected.");
			bot.Join(ToUnsigned(argv[6]));
		}
		else
			if (String(argv[5]) == "-c")
			{
				if (argc == 6) onError("Players count expected.");
				bot.Create(ToUnsigned(argv[6]));
			}
			else
				onError("-j or -c argument expected.");

	FILE *script = fopen(argv[4], "r");

	switch (bot.Play(script))
	{
		case GameBot::gr_win: printf("You win.\n"); break;
		case GameBot::gr_lose: printf("You lose.\n"); break;
		case GameBot::gr_draw: printf("Draw.\n"); break;
	}

	return 0;
}
