#ifndef __NETWORK
#define __NETWORK

#include "gameinitexit.h"

enum{
	kMessageTypeSynch=1,
	kMessageTypeSynchStart=2,
	kMessageTypeID=3,
	kMessageTypeCarID=4,
	kMessageTypeStart=5,
	kMessageTypeEndGame=6,
	kMessageTypeEndReplay=7,
	kMessageTypeGameInfo=8,
	kMessageTypePhysics=9,
	kMessageTypeChat=10,
	kMessageTypePing=11,
	kMessageTypePong=12,
	kMessageTypeJoinDenied=13,
	kMessageTypeVersionConfirmed=14,
	kMessageTypeVersionDenied=15,
	kMessageTypePause=16,
	kMessageTypeResume=17,
	kMessageTypePlayerLeft=18,
	kMessageTypePlayerJoined=19,
	kMessageTypeGameTerminated=20,
	kMessageTypeVersion=21,
	kMessageTypeKicked=22,
	kMessageTypeBye=23,
	kMessageTypeFinishTime=24,
};

enum{
	kJoinDeniedInProgress,
	kJoinDeniedVersion
};

enum{
	kMessagePriorityLow,
	kMessagePriorityNormal,
	kMessagePriorityHigh
};

enum{
	kMessageNoRecipients=-1,
	kMessageSendToAll=-2,
	kMessageSendToAllButSelf=-3,
	kMessageSendToHostOnly=-4
};

typedef struct{
	UInt8 playerID;
	UInt8 color;
	UInt64 license;
	UInt32 licenseCopies;
	char carName[kMaxFileNameLength];
	char playerName[256];
//	UInt32 checksum;
}tCarIDMessage;

enum{
	kChatFlagAlert=1<<0,
	kChatFlagSystem=1<<1,
	kChatFlagSilent=1<<2
};

typedef struct{
	UInt8 flags;
	float recvTime;
	char str[256];
} tChatMessage;

typedef struct{
	int player;
	int time;
}tFinishTimeMessage;

typedef struct{
	void *opaqueRef;
	void *data;
	long size;
	int from;
	char what;
} tNetworkPacket;

enum{
	kNetworkDisconnectBan,
	kNetworkDisconnectLicenseCopies,
	kNetworkDisconnectPirate,
	kNetworkDisconnectNoDemo,
};

void NetworkInit();
void NetworkExit();
int JoinNetGame(char *address,char *errorString);
void HostNetGame(int maxPlayers,char *password);
int NetworkSynch(int numPlayers);
void ExitNetGame();
int NetworkGetLocalNetID();
void NetworkSendPacket(char messageType,void *message,long size,int priority,int to);
void NetworkSendPacket(char messageType,void *message,long size,int priority,int to,int notTo);
void NetworkDisconnectPlayer(int netID,UInt8 type);
void NetworkIdle();
int NetworkReceivePacket(tNetworkPacket* packet);
void NetworkDisposePacket(tNetworkPacket* packet);
void NetworkQueuePacket(tNetworkPacket *insert);
void NetworkClearPacketQueue();
void NetworkGetStatusString(char *str);
void NetworkGetBandwidth(float *rcv,float *snd);
void NetworkPreSession();
float NetworkGetPlayerPing(int netID);
void NetworkLockOutNewPlayers();
void NetworkUnlockOutNewPlayers();
void NetworkChangePassword(char *pass);

#define S64Swap(value) ((value)=EndianS64_BtoN(value))
#define U64Swap(value) ((value)=EndianU64_BtoN(value))
#define S32Swap(value) ((value)=EndianS32_BtoN(value))
#define U32Swap(value) ((value)=EndianU32_BtoN(value))
#define S16Swap(value) ((value)=EndianS16_BtoN(value))
#define U16Swap(value) ((value)=EndianU16_BtoN(value))
#define F32Swap(value) ((*((UInt32*)(&(value))))=EndianU32_BtoN(*((UInt32*)(&(value)))))
#define F64Swap(value) ((*((UInt64*)(&(value))))=EndianU64_BtoN(*((UInt64*)(&(value)))))

extern int gInternetAvailable;
#endif