#ifndef sGui_h
#define sGui_h
#include "sSocket.h"
#include "misc.h"

typedef enum {
	DISCONNECT 	=  0,
	CONNECTING	=  1,
	CONNECT		=  2
	} eGui_status;

#define GUI_LIST	for (sGui *akt = sGui::first_gui; akt != NULL; akt = akt->next_gui)

/* ed2k_gui class */
class sGui : public sSocket {
public:
static	unsigned	 count;			/* total number of current files */
static  class sGui	*first_gui;
	class sGui	*next_gui;
	time_t		 ul_timeout;
	time_t		 dl_timeout;
	eGui_status	 status;
	unsigned int  	version;
private:
	char 		*name;
	unsigned int	 idx;
	int		 last_code;
	uint32_t	 search_len;
	char		*search_tree;
	int		 result_type;	// TCP(1) UDP(2)
	uint32_t	 result_len;
	char		*result_tree;
	unsigned short	 port;
public:	bool		 ul_status;
	bool		 dl_status;
	bool 			announced_server_connect;
	bool		 auth;
	char		 padding1;
	char		 padding2;
	char		 padding3;
	int		 GUI_serverlist      (void                   ); // (0xAA)
	bool  		 GUI_res_part_status (tHash  &hash           ); // (0xAE)
	int		 GUI_core_status     (void                   ); // (0xAF)
public:	int  		 GUI_statusMessage   (const char *txt        ); // (0xB4)
private: int		 GUI_errorMessage    (const char *txt        ); // (0xB5)
public:	int  		 GUI_connected_to    (const char* name       ); // (0xB6)
	int  		 GUI_disconnected    (void                   ); // (0xB7)
	int		 GUI_result_tcp      (void                   ); // (0xBB)	
	int   		 GUI_new_download    (const class sFile *file); // (0xBC 188)
public:	int		 GUI_new_upload      (const class sFile *file); // (0xBE 190)
	int		 GUI_removeDownload  (tHash &hash            ); // (0xBD)
	int 		 GUI_shareFiles      (void                   ); // (0xad)
private:int		 GUI_dl_status       (void                   ); // (0xC5 197)
	int		 GUI_ul_status       (void                   ); // (0xC6 218)
public: int		 GUI_txt_g 			 (void);
	int		 GUI_send_option     (void		     ); // (0xC7 219)
	int		 GUI_share_dirs      (void                   ); // (0xD7)
	int		 GUI_login           (class sPacket *packet); // (0x64)
	int		 GUI_shutdown        (class sPacket *packet); // (0x65)
	int		 GUI_txt_command     (class sPacket *packet); // (0x66)
	int		 GUI_connect         (class sPacket *packet); // (0xC8)
	int		 GUI_disconnect      (class sPacket *packet); // (0xC9)
	int		 GUI_search          (class sPacket *packet); // (0xCA)
	int		 GUI_search_ext      (class sPacket *packet); // (0xCB)
	int 		 req_download 	     (class sPacket *packet); // (0xCF)
	int		 GUI_dlPause         (class sPacket *packet); // (0xD0)
	int		 GUI_dlResume        (class sPacket *packet); // (0xD1)
	int		 GUI_cancelDownload  (class sPacket *packet); // (0xD2)
	int		 GUI_download_prio   (class sPacket *packet); // (0xD3)
	int		 GUI_get_serverlist  (class sPacket *packet); // (0xD5) // Gui request for server List
	int		 GUI_req_shared_dir  (class sPacket *packet); // (0xD7)
	int		 GUI_dlStatus_start  (class sPacket *packet); // (0xD9)
	int		 GUI_dlStatus_stop   (class sPacket *packet); // (0xDA)
	int		 GUI_ulStatus_start  (class sPacket *packet); // (0xDB)
	int		 GUI_ulStatus_stop   (class sPacket *packet); // (0xDC)
	int		 GUI_serverDelete    (class sPacket *packet); // (0xDD)
	int		 GUI_serverAdd       (class sPacket *packet); // (0xDE)
	int		 GUI_serverPrio      (class sPacket *packet); // (0xDF)
	int		 GUI_get_shareFiles  (class sPacket *packet); // (0xE0)
	int		 GUI_get_option      (class sPacket *packet); // (0xE1)
	int		 GUI_add_download    (class sPacket *packet); // (0xE2)
	int		 GUI_req_part_status (class sPacket *packet); // (0xE3)
	int		 GUI_core_status_get (class sPacket *      ); // (0xe4)
	
	int		 Donkey_gui 	     (class sPacket *packet);
	public:
	void		 readWork(void);
	void		 TCP_Response	(void) { result_type = 1; }
	void		 UDP_Response	(void) { result_type = 2; }
	void		 Code		(int i) { last_code = i; }
virtual	void		 Work		(void );
virtual			~sGui	();
	unsigned int	 IDX	   	(void ) const { return idx   ; }
	unsigned short	 Port		(void ) const { return port  ; }
	eGui_status	 Status		(void ) const { return status; }
const	char		*resultTree	(void ) const { return result_tree; }
const	char		*searchTree	(void ) const { return search_tree; }
	uint32_t	 resultLen	(void ) const { return result_len; }
	uint32_t	 searchLen	(void ) const { return search_len; }
	void		 resultTree	(char *tree,uint32_t len);
	void		 searchTree	(char *tree,uint32_t len);
	void		 Port		(unsigned short p) { port = p; }
	void		 Status		(eGui_status s) { status = s; }
	void          	 IDX	   	(unsigned int id) { idx = id; }
	public:
	const char 	*Name 		(void) const { return name; }
	void 		 Name 		(const char *name);
			 sGui 		(class cSocket *fd, struct in_addr);
	};
int         GUI_result_tcp      (sGui    *source                    );
int         GUI_statusMessage   (sGui    *source, const char *txt   );
int         GUI_connected_to    (sGui    *source, const char* name  );
int Donkey_gui (sGui *gui, sPacket *packet);
#endif
