#ifndef SERVER_CLIENT_H
#define SERVER_CLIENT_H

#include <QMainWindow>
#include <QMessageBox>
#include <QBoxLayout>
#include <QTimer>
#include <QDir>
#include <QQueue>
#include <pithread.h>
#include <pidiagnostics.h>
#include "SH_server_base.h"
#include "SH_network_types.h"
#include "SH_multicast.h"
#include "SH_stream_tcp.h"
#include "SH_types.h"

class ServerCore;

class ServerClient: public PIThread {
	PIOBJECT_SUBCLASS(ServerClient, PIThread)
public:
	ServerClient(ServerCore * s, SHNetworkTypes::ChannelType c);

	enum Parameter {
		pLogIO = 1
	};

	void init(PIIODevice * e);
	void close();
	void destroy();
	bool isConnected();
	void queueSend(const PIByteArray & d, bool gr_force = false);
	bool isQueueSending();
	void setQueueSending(bool yes);
	void run() override;
	PIString hostType();
	void updateInfo();
	void applyAdminParameter(const PacketParameter & p);
	void setRole(int index);
	void newRoleDone();

	EVENT_HANDLER1(void, packetRec, PIByteArray, ba);
	EVENT_HANDLER1(void, sendRequest, PIByteArray, data);
	EVENT1(postGraphicsRequest, PIStringList, gl);
	EVENT_HANDLER1(void, recGraphicsRequest, PIStringList, gl);
	//EVENT2(packetReceived, PIByteArray, data, PIEthernet*, from)

	ServerCore * server = nullptr;
	PIIODevice * device = nullptr;
	PIDiagnostics * diag = nullptr;
	SHStreamTCP * tcp = nullptr;
	ServerClientInfo info;
	PIDeque<PIByteArray> send_queue, new_gr_queue;
	PIMutex queue_mutex, queue_sending_mutex;
	QStringList graphics;
	QMap<QString, ullong> graphics_last_sended;
	PIDeque<LogEntry> admin_io_log;
	//PITimeMeasurer ka_tm;
	SHNetworkTypes::ChannelType channel_type;
	uint id = 0;
	int net_version = 0, sync_version = 0, user_role = 0, new_user_role = 0;
	bool active = false, queue_sending = false, is_graphics_sended = false, role_changed = false;
	bool upload_ui = true, upload_rcc = true, closing = false, log_sended = false, admin_log_io = false;

private:
	void sendGraphicHistoty(const QString & vname);

};


#endif // SERVER_CLIENT_H
