#include <iostream>
#include <vector>
#include "ParseBuffer.hpp"
#include "CommandValidator.hpp"

static void	printMessage(const IrcMessage &msg)
{
	std::cout << "raw=[" << msg.getRaw() << "]\n";
	std::cout << "valid=" << (msg.isValid() ? "yes" : "no") << "\n";
	if (!msg.getError().empty())
		std::cout << "error=" << msg.getError() << "\n";
	if (msg.hasPrefix())
		std::cout << "prefix=" << msg.getPrefix() << "\n";
	std::cout << "command=" << msg.getCommand() << "\n";
	for (size_t i = 0; i < msg.paramCount(); ++i)
		std::cout << "param[" << i << "]=[" << msg.param(i) << "]\n";
	std::cout << "known=" << (CommandValidator::isKnownCommand(msg.getCommand()) ? "yes" : "no") << "\n";
	std::cout << "enough_params=" << (CommandValidator::hasEnoughParams(msg) ? "yes" : "no") << "\n";
	std::cout << "-----\n";
}

int	main(void)
{
	ParseBuffer	buffer;
	std::string	chunk1 = "PASS secret\r\nNICK je";
	std::string	chunk2 = "remy\r\nUSER jeremy 0 * :Jeremy Real Name\r\n";
	std::string	chunk3 = "JOIN #42\r\nPRIVMSG #42 :salut les gars ca va ?\r\n";
	std::string	chunk4 = ":nick!user@host PRIVMSG jeremy :hello from prefixed line\r\n";

	buffer.append(chunk1.c_str(), chunk1.size());
	std::vector<IrcMessage> messages = buffer.extractMessages();
	for (size_t i = 0; i < messages.size(); ++i)
		printMessage(messages[i]);

	buffer.append(chunk2.c_str(), chunk2.size());
	messages = buffer.extractMessages();
	for (size_t i = 0; i < messages.size(); ++i)
		printMessage(messages[i]);

	buffer.append(chunk3.c_str(), chunk3.size());
	buffer.append(chunk4.c_str(), chunk4.size());
	messages = buffer.extractMessages();
	for (size_t i = 0; i < messages.size(); ++i)
		printMessage(messages[i]);

	return (0);
}
