#include <iostream>

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

#include "linux_stuff.h"

struct TServer;

struct TUser
	: public boost::enable_shared_from_this<TUser>
{
	static size_t NextId;

	size_t Id;

	TServer& Server;

	TUserInfo UserInfo;
	boost::asio::ip::tcp::socket Socket;

	int LoginQuestion;
	int LoginQuestionResult;

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

	TUser(TServer& server);
	void Delete();

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

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

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

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

	bool IsAdmin();

	void ForceClose();

};




struct TServer
{
	TServer(boost::asio::io_service& service, const boost::asio::ip::tcp::endpoint& endpoint)
		: Service(service)
		, Acceptor(service, endpoint)
	{
		StartAccept();
	}

	void StartAccept();
	void HandleAccept(boost::shared_ptr<TUser> user, const boost::system::error_code& error);

	void BroadcastMessage(int messageType, std::vector<char> messageData);

	std::vector<char> CreateMapData();
	std::vector<char> CreateAlarmMapData();
	
	boost::asio::io_service& Service;
	boost::asio::ip::tcp::acceptor Acceptor;
	std::vector<boost::shared_ptr<TUser> > UserArr;
};