#ifndef CLIENT_SOCKET_H_INCLUDED
#define CLIENT_SOCKET_H_INCLUDED

#include "UserInfo.h"
#include "misc.h"
#include "MessageSender.h"


struct TClientSocket
	: public boost::enable_shared_from_this<TClientSocket>
{
	boost::asio::ip::tcp::socket Socket;
	boost::asio::io_service& IoService;
    
    boost::shared_ptr<boost::asio::deadline_timer> ConnectionTimeoutTimer;

	int AutorizationCode;
	int AutorizationCodeResponce;
	std::string AutorizationCodeResult;
    float BaseLon;
    float BaseLat;
    int BaseZoom;

	int LastSignal;
	int ReadDataLenLong;
	std::vector<char> ReadData;

	TUserInfo UserInfo;
    
    bool BlockCloseOnError;
    bool BlockForceCloseMessage;

	TClientSocket(boost::asio::io_service& io_service);

	void Open(std::string address, std::string port);
    
    void ConnectTimeout(const boost::system::error_code& e);
    void OnAddressNotResolved();

	void HandleConnect(const boost::system::error_code& error);

	void HandleReadAutorizationCode(const boost::system::error_code& error);
	void HandleReadAutorizationResult(const boost::system::error_code& error);

	void HandleReadSignal(const boost::system::error_code& error);
	void HandleReadDataLen(const boost::system::error_code& error);
	void HandleReadData(const boost::system::error_code& error);

	void HandleWrite(const boost::system::error_code& error);

    void CloseImmediate();


	void SendMessageImmediate(std::string msg);
	void SendUserinfoUpdateImmediate();
	void SendHeartbeatImmediate();
	void SendMapQueryImmediate();
	void SendAlarmMapQueryImmediate();
    void SendKickPlayerImmediate(std::string lastName, std::string firstName, std::string middleName);

    void CloseThreaded();

	void SendMessageThreaded(std::string msg);
	void SendUserinfoUpdateThreaded();
	void SendHeartbeatThreaded();
	void SendMapQueryThreaded();
	void SendAlarmMapQueryThreaded();
    void SendKickPlayerThreaded(std::string lastName, std::string firstName, std::string middleName);

	boost::signal<void (TUserInfo userInfo, std::string message)> ReceiveMessageSignal;
	boost::signal<void (std::vector<TUserInfo> userInfoArr)> ReceiveMapSignal;
	boost::signal<void (std::vector<TUserInfo> userInfoArr)> ReceiveAlarmMapSignal;
	boost::signal<void ()> ReceiveForceCloseSignal;
    boost::signal<void ()> ReceiveAutorizationSentSignal;
};




#endif
