#ifndef _NETWORKING_H
#define _NETWORKING_H

#include <DyConnect.h>

#define CLIENT_NAME      "NAME"
#define CLIENT_VERSION   "CVER"
#define QUERY_LOBBY_DATA "QLOB"
#define QUERY_MOTD       "MOTD"
#define CHAT             "CHAT"
#define GAME_UPDATE      "GUPD"
#define JOINING_GAME     "JOIN"
#define BACK_IN_LOBBY    "INLB"

#define SERVER_LOBBY_DATA "LOBD"
#define SERVER_MOTD       "MOTD"
#define SERVER_CHAT       "TEXT"
#define SERVER_NEWNAME    "NAME"
#define SERVER_WANTS_NAME    "QNAM"
#define SERVER_WANTS_VERSION "QVER"
#define PORT_ERROR        "EPRT"

enum netids
{
	NET_FIRST = 0,
	NET_LOBBY,
	NET_HOST,
	NET_HOST_CONNECT,
	NET_CLIENT,
	NET_PLAY,
	NET_QUIT,

	NET_PLAYERTINT,
	NET_PLAYERTEAM,
	NET_ASK_PLAYERDATA,
	NET_FUNDSPERBUILDING,
	NET_SHOWDAMAGE,
	NET_TIMELIMIT,
	NET_MAPCHANGE,
	NET_MAPDATA,
	NET_MAPLOADED,
	NET_CLIENTREADY,
	NET_CHARACTER,
	NET_STARTINGCASH,
	NET_TECHLEVEL,
	NET_CASHTURNS,
	NET_UNREADYALL,
	NET_GO,
	NET_CLIENT_TINTCYCLE,
	NET_DISCONNECT,

	NET_UPDATINGTILES,
	NET_UNITHEALTH,
	NET_FINISHEDSYNC,
	NET_INIDATA,
	NET_PLAYERGONE,
	NET_NEWTURN,
	NET_NEWUNIT,
	NET_PLAYERCASH,
	NET_MOVEUNIT,
	NET_STANDBY,
	NET_DEPLOYPORTAL,
	NET_MECHANIC_CAPTURE,
	NET_UNLOAD,
	NET_TELEPORT,
	NET_BATTLEDATA,
	//#define NET_ATTACK           35
	//#define NET_COUNTERATTACK    36
	//#define NET_TREEBURN         37
	//#define NET_BATTLEOVER       38
	NET_OHNOYOUDONT,

	NET_CLIENT_ENDTURN,
	NET_CLIENT_NEWUNIT,
	NET_CLIENT_MOVEUNIT,
	NET_CLIENT_STANDBY,
	NET_CLIENT_DEPLOY,
	NET_CLIENT_MECHCAP,
	NET_CLIENT_UNLOAD,
	NET_CLIENT_TELEPORT,
	NET_CLIENT_ATTACK,
	NET_CLIENT_SURRENDER,

	NET_STATS,
	NET_PLAYERDATA,
	NET_MENUDATA,
	NET_PLAYER_UPDATE,
	NET_PLAYER_SURRENDER,
	NET_RESUMEGAME,
	NET_NEWCONTROLLER
};


#define gameid 0

#define MAX_CONNECTIONS 6

class netengine : public DasyneEngine
{
public:
	void broadcastTCP(DyBytes &data)
	{
		unsigned int i = 0;
		while (i < getMaxConnections())
		{
			if (isConnected(i))
			{
				sendTCP(i, data);
			}
			i++;
		}
	}
};

typedef struct _netstuff
{
	bool server;
	int lobbythread, thread, chatthread, clientthread[3], client_pnum, hostnum;
	string err;
	string address[4];
	netengine *engine;  //for the main stuff: user and movement data
	netengine *chatengine;  //for chatting between players
	netengine *lobbyengine;  //for lobby stuff: players, available games, lobby chat, etc.
	
	_netstuff();
	~_netstuff();
	
	bool init(bool is_server);
	void unload();
} _netstuff;

extern _netstuff net;

extern void create_sidebars();
extern void description(_character *c, int x, int y, bool centeredtext);

bool init_networking();
void stop_networking();

bool get_client_message(int client, DyBytes &data);
bool get_host_message(DyBytes &data);

void load_netplayer(int number, DyBytes *data);

DyBytes mapdata();
void send_mapdata(int thread);
void broadcast_mapdata();
void load_mapdata(DyBytes *data);

void addID(DyBytes &data, const char id[5]);
void prependID(DyBytes &data, const char id[5]);
string getID(DyBytes &data);

bool clientconnected(int client);
int network();
int net_serverconnect();
int net_lobby();
int net_connect_to_host();
int net_joingame();
int net_hostgame();

bool do_hostchat(_listbox *chatbox, string str);
bool do_clientchat(_listbox *chatbox, string str);
void broadcast_chat_message(string mesg);

#endif
