#ifndef SERVER_BASE_H
#define SERVER_BASE_H

#include <QTcpServer>
#include <QWebSocket>
#include <vector>

#include "service.h"

class Server : public Service
{
	Q_OBJECT

protected:
	typedef enum
	{
		STATE_IDLE,
		STATE_RECV_JSON,
	} client_state_t;

	struct Client
	{
		union {
			QTcpSocket* tcpSocket;
			QWebSocket* webSocket;
		} socket;
		QByteArray buffer;
		client_state_t state = STATE_IDLE;
		long long recievebytes = 0;
	};

	std::vector<Client> clients;

public slots:
	virtual void itemUpdated(ItemUpdateRequest update) override;

public:
	Server(QObject* parent = nullptr);
	virtual ~Server();

protected:
	virtual void processIncomeingJson(const QByteArray& jsonbytes) override;
	void handleSocketError();
	void handleSocketDisconnect();
	void removeClient(QTcpSocket* socket);
	void removeClient(QWebSocket* socket);

signals:
	void sigRequestSave();
};

#endif // SERVER_BASE_H
