// DWBS ------------------------------------------------------------------ DWBS
//
// PLATFORM		: ALL
// PRODUCT		: BIT_DEMON
// VISIBILITY	: PUBLIC
//
// DWBS ------------------------------------------------------------------ DWBS

#ifndef SIMULATION_H
#define SIMULATION_H

#include <bdExample/bdExampleCommonLobby/bdExampleCommonLobby.h>
#include <bdLobby/bdChannelChat/bdGetChannelsResult.h>
#include <bdLobby/bdChannelChat/bdGetChannelMembersResult.h>

#define MAX_NUM_DISPLAY_CHANNELS (10u)
#define MAX_NUM_DISPLAY_MEMBERS (10u)

class Simulation : public bdDemo
{
	public:

		BD_DECLARE_NEW_AND_DELETE_OPERATORS

		Simulation();

		~Simulation();

		virtual bdBool init(bdInt argc,
							bdNChar8* argv[]);

		virtual bdBool step(const bdFloat32 time);
		
		virtual void quit();

	protected:

		enum Task
		{
			START_NETWORK,			
			CHECK_DNS,
			CREATE_ACCOUNT,			
			AUTHENTICATE_ACCOUNT,
			LOBBY_CONNECT,
			GET_CHANNEL_LIST,
			SUBSCRIBE_TO_CHANNEL,
			SEND_TO_CHANNEL,
			GET_CHANNEL_MEMBERS,
			WHISPER_TO_MEMBER,
			UNSUBSCRIBE_FROM_CHANNEL,
			LOBBY_DISCONNECT,
			STOP_NETWORK
		};

		void startTask();

		void pumpTask();

		void displayTaskResult();

		// Network startup

		void startNetworkDescription(bdString& description);

		void startNetwork();

		void pumpStartNetwork();

		// DNS lookup

		void checkDNSDescription(bdString& description);

		void startCheckDNS();

		void pumpCheckDNS();

		// Authentication service 	

		bdAuthService* getAuthService();

		const bdAuthInfo *getAuthInfo();

		void checkAuthServiceError();

		void pumpAuthServiceTask();

		// Create account

		void createAccountDescription(bdString& description);

		void startCreateAccount();

		void pumpCreateAccount();

		// Authenticate account

		void authenticateAccountDescription(bdString& description);

		void startAuthenticateAccount();

		void pumpAuthenticateAccount();

		// Lobby service connect

		void lobbyServiceConnectDescription(bdString& description);

		void startLobbyServiceConnect();

		void pumpLobbyServiceConnect();

		// Channel Chat

		void checkChannelChatError();

		// Get Channel List

		void getChannelListDescription(bdString& description);

		void startGetChannelList();

		void pumpGetChannelList();

		// Subscribe to Channel

		void subscribeToChannelDescription(bdString& description);

		void startSubscribeToChannel();

		void pumpSubscribeToChannel();

		// Send to Channel

		void sendToChannelDescription(bdString& description);

		void startSendToChannel();

		void pumpSendToChannel();

		// Get Channel Members

		void getChannelMembersDescription(bdString& description);

		void startGetChannelMembers();

		void pumpGetChannelMembers();

		// Whisper to channel member

		void whisperToMemberDescription(bdString& description);

		void startWhisperToMember();

		void pumpWhisperToMember();

		// Whisper to channel member

		void unsubscribeFromChannelDescription(bdString& description);

		void startUnsubscribeFromChannel();

		void pumpUnsubscribeFromChannel();

		// Lobby disconnect

		void lobbyServiceDisconnectDescription(bdString& description);

		void startLobbyServiceDisconnect();

		void pumpLobbyServiceDisconnect();

		// Network stop

		void stopNetworkDescription(bdString& description);

		void stopNetwork();

		// A generic pump method, for all channel chat tasks that do not return results.
		void pumpGenericChannelChatTask();
		
		// A method to consume and display any messages that have arrived in the
		// message store.
		void checkMessageStore();

	protected:

		// receives log messages
		bdLogSubscriber m_logSubscriber;

		TaskStatus m_taskStatus;
		Task m_task;
		bdString m_lastTaskDescription;

		bdBool m_changedScreen;
		bdString m_screenText;

		bdInetAddr m_authAddr;
		bdGetHostByName *m_authGetHostByName;

		bdAuthService *m_authService;

		bdInetAddr m_lsgAddr;
		bdGetHostByName *m_lsgGetHostByName;

		bdRemoteTask *m_remoteTask;

		PrintableList<bdChannelInfo, MAX_NUM_DISPLAY_CHANNELS> m_channelList;

		bdUInt64 m_subscribingChannelID;
		bdFastArray<bdUInt64> m_subscribedChannels;
		
		PrintableList<bdGetChannelMembersResult::bdChannelMember, MAX_NUM_DISPLAY_MEMBERS> m_memberList;

		bdString m_messageText;
		bdStopwatch m_messageTimer;

		bdBool m_pumpLobbyConnection;
		bdLobbyErrorCode m_lobbyErrorCode;
};


#endif // SIMULATION_H
