#ifndef TCPSERVER_BASE_H
#define TCPSERVER_BASE_H

#include <QTcpServer>
#include <vector>

#include "sensors/sensor.h"
#include "items/item.h"
#include "items/itemsource.h"

class Service : public ItemSource
{
	Q_OBJECT
protected:

	typedef enum
	{
		STATE_IDLE,
		STATE_RECV_JSON,
	} client_state_t;

signals:
	void gotSensor(Sensor sensor, sensor_update_type_t type = SENSOR_UPDATE_BACKEND);
	void sensorAdded(Sensor sensor, Sensor::sensor_backend_type_t backend, QJsonObject payload);

public slots:
	virtual void sensorEvent(Sensor sensor, sensor_update_type_t type);
	virtual void itemUpdated(ItemUpdateRequest update);
	virtual void refresh() override;
	virtual void addSensor(Sensor sensor, Sensor::sensor_backend_type_t backend, QJsonObject payload = {});

public:
	Service(QObject* parent = nullptr);
	void sendSensors();
	void sendItems();
	virtual void sendJson(const QJsonObject& json) = 0;
	virtual bool launch(const QHostAddress &address = QHostAddress::Any, quint16 port = 0) = 0;

protected:
	static QJsonObject createMessage(const QString& type, const QJsonArray& data);
	virtual void processIncomeingJson(const QByteArray& jsonbytes);
};

#endif // TCPSERVER_BASE_H
