#include "mnet.h"
#include <iostream> // cout

MNet::MNet()
: Net()
{
	waitforchannel = 0;
	m_mode = peer;
}

MNet::~MNet()
{
}

MNet &MNet::operator<<(ChannelOpcodes value)
{
	waitforchannel = value;
	return *this;
}

MNet &MNet::operator<<(ModeOpcodes value)
{
	m_mode = value;
	//std::cout << "[mnet] mode " << value << std::endl;
	return *this;
}

MNet &MNet::operator<<(SpecialOpcodes value)
{
	if((m_mode != peer) && ((value == begin) || (value == end) || (value == flush)))
	{
			for(it = channellist.begin(); it != channellist.end(); it++)
			{
				//std::cout << "[mnet] broadcasting special to " << (*it) << std::endl;
				Net::operator<<(channel);
				Net::operator<<((*it));
				Net::operator<<(value);
			}
	}
	else Net::operator<<(value);
	return *this;
}

MNet &MNet::operator<<(int value)
{
	if(waitforchannel)
	{
		if(waitforchannel == add)
		{
			channellist.push_back(value);
			std::cout << "[mnet] add fd " << value << std::endl;
		}
		else if(waitforchannel == remove)
		{
			/*for(it = channellist.begin(); it != channellist.end(); it++)
				if((*it) == value)
				{
					channellist.remove((*it));
					break;
				}*/
			channellist.remove(value);
			std::cout << "[mnet] remove fd " << value << std::endl;
		}
		else if(waitforchannel == clear)
		{
			channellist.clear();
			std::cout << "[mnet] clear fds " << std::endl;
		}
		waitforchannel = 0;
	}
	else
	{
		if(m_mode == peer) Net::operator<<(value);
		else
		{
			for(it = channellist.begin(); it != channellist.end(); it++)
			{
				std::cout << "[mnet] broadcasting to " << (*it) << std::endl;
				Net::operator<<(channel);
				Net::operator<<((*it));
				Net::operator<<(value);
			}
		}
	}
	return *this;
}

MNet &MNet::operator<<(const char* value)
{
	if(m_mode == peer) Net::operator<<(value);
	else
	{
		for(it = channellist.begin(); it != channellist.end(); it++)
		{
			//std::cout << "[mnet] broadcasting to " << (*it) << std::endl;
			Net::operator<<(channel);
			Net::operator<<((*it));
			Net::operator<<(value);
		}
	}
	return *this;
}


