/*
 *  Original work: Copyright (c) 2014, Oculus VR, Inc.
 *  All rights reserved.
 *
 *  This source code is licensed under the BSD-style license found in the
 *  RakNet License.txt file in the licenses directory of this source tree. An additional grant 
 *  of patent rights can be found in the RakNet Patents.txt file in the same directory.
 *
 *
 *  Modified work: Copyright (c) 2016-2017, SLikeSoft UG (haftungsbeschränkt)
 *
 *  This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
 *  license found in the license.txt file in the root directory of this source tree.
 */

#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include "slikenet/GetTime.h"
#include "slikenet/peerinterface.h"
#include "slikenet/MessageIdentifiers.h"

#include "slikenet/types.h"
#include "slikenet/sleep.h"
#include "slikenet/FullyConnectedMesh2.h"
#include "slikenet/ConnectionGraph2.h"
#include <assert.h>
#include "slikenet/SocketLayer.h"
#include "slikenet/Kbhit.h"
#include "slikenet/PacketLogger.h"
#include "slikenet/Gets.h"

using namespace SLNet;

#define NUM_PEERS 8
SLNet::RakPeerInterface *rakPeer[NUM_PEERS];

int main()
{
	FullyConnectedMesh2 fcm2[NUM_PEERS];
	ConnectionGraph2 cg2[NUM_PEERS];

	for (unsigned short i=0; i < NUM_PEERS; i++)
	{
		rakPeer[i]= SLNet::RakPeerInterface::GetInstance();
		rakPeer[i]->AttachPlugin(&fcm2[i]);
		rakPeer[i]->AttachPlugin(&cg2[i]);
		fcm2[i].SetAutoparticipateConnections(true);
		SLNet::SocketDescriptor sd;
		sd.port=60000+i;
		SLNET_VERIFY(rakPeer[i]->Startup(NUM_PEERS, &sd, 1) == RAKNET_STARTED);
		rakPeer[i]->SetMaximumIncomingConnections(NUM_PEERS);
		rakPeer[i]->SetTimeoutTime(1000, SLNet::UNASSIGNED_SYSTEM_ADDRESS);
		printf("Our guid is %s\n", rakPeer[i]->GetGuidFromSystemAddress(SLNet::UNASSIGNED_SYSTEM_ADDRESS).ToString());
	}

	for (unsigned short i=0; i < NUM_PEERS; i++)
	{
		for (unsigned short j=0; j < NUM_PEERS; j++)
		{
			if (i==j)
				continue;
			SLNET_VERIFY(rakPeer[i]->Connect("127.0.0.1", 60000 + j, 0, 0) == CONNECTION_ATTEMPT_STARTED);
		}
	}

	bool quit=false;
	SLNet::Packet *packet;
	int ch;
	while (!quit)
	{
		for (int i=0; i < NUM_PEERS; i++)
		{
			for (packet = rakPeer[i]->Receive(); packet; rakPeer[i]->DeallocatePacket(packet), packet = rakPeer[i]->Receive())
			{
				switch (packet->data[0])
				{
				case ID_DISCONNECTION_NOTIFICATION:
					// Connection lost normally
					printf("%i. ID_DISCONNECTION_NOTIFICATION\n", i);
					break;
				case ID_CONNECTION_ATTEMPT_FAILED:
					printf("%i. ID_CONNECTION_ATTEMPT_FAILED\n", i);
					break;

				case ID_NEW_INCOMING_CONNECTION:
					// Somebody connected.  We have their IP now
					printf("%i. ID_NEW_INCOMING_CONNECTION from %s. guid=%s.\n", i, packet->systemAddress.ToString(true), packet->guid.ToString());
					break;

				case ID_CONNECTION_REQUEST_ACCEPTED:
					// Somebody connected.  We have their IP now
					printf("%i. ID_CONNECTION_REQUEST_ACCEPTED from %s. guid=%s.\n", i, packet->systemAddress.ToString(true), packet->guid.ToString());
					break;


				case ID_CONNECTION_LOST:
					// Couldn't deliver a reliable packet - i.e. the other system was abnormally
					// terminated
					printf("%i. ID_CONNECTION_LOST\n", i);
					break;


				case ID_FCM2_NEW_HOST:
					if (packet->systemAddress== SLNet::UNASSIGNED_SYSTEM_ADDRESS)
						printf("%i. Got new host (ourselves)\n", i);
					else
						printf("%i. Got new host %s, %s\n", i, packet->systemAddress.ToString(true), packet->guid.ToString());
					break;
				}
			}
		}

		if (_kbhit())
		{
			ch=_getch();
			if (ch==' ')
			{
				unsigned int participantList;
				RakNetGUID hostGuid;
				bool weAreHost;
				for (int i=0; i < NUM_PEERS; i++)
				{
					if (rakPeer[i]->IsActive()==false)
						continue;

					fcm2[i].GetParticipantCount(&participantList);
					weAreHost=fcm2[i].IsHostSystem();
					hostGuid=fcm2[i].GetHostSystem();
					
					if (weAreHost)
						printf("%i. %iP myGuid=%s, hostGuid=%s tcc=%i (Y)\n",i, participantList, rakPeer[i]->GetGuidFromSystemAddress(SLNet::UNASSIGNED_SYSTEM_ADDRESS).ToString(), hostGuid.ToString(), fcm2[i].GetTotalConnectionCount());
					else
						printf("%i. %iP myGuid=%s, hostGuid=%s tcc=%i (N)\n",i, participantList, rakPeer[i]->GetGuidFromSystemAddress(SLNet::UNASSIGNED_SYSTEM_ADDRESS).ToString(), hostGuid.ToString(), fcm2[i].GetTotalConnectionCount());
				}
			}
			if (ch=='d' || ch=='D')
			{
				char str[32];
				printf("Enter system index to disconnect.\n");
				Gets(str, sizeof(str));
				rakPeer[atoi(str)]->Shutdown(100);
				printf("Done.\n");
			}
			if (ch=='q' || ch=='Q')
			{
				printf("Quitting.\n");
				quit=true;
			}
		}


		RakSleep(30);
	}

	for (int i=0; i < NUM_PEERS; i++)
	{
		SLNet::RakPeerInterface::DestroyInstance(rakPeer[i]);
	}
	return 0;
}
