#pragma once

#include <QTimer>

#include "actor.h"
#include "sensors/sensor.h"

class Regulator : public Actor
{
	Q_OBJECT

private:
	Sensor sensor_;
	float setPoint_ = 0;
	float band_ = 1;
	bool invert_ = false;
	int timeout_ = 1800;
	int safeValue_ = 0;
	QTimer timer;

	bool first = true;

private slots:

	void timeout();

public slots:

	void sensorEvent(Sensor sensor, sensor_update_type_t type);

	void setSensor(const Sensor sensor);
	void setPoint(float setPoint );
	void setBand (float band );
	void setInvert(bool invert );
	void setSafeValue(int value);
	void setTimeout(int value);
	virtual void makeInactive() override;

public:

	float getBand()
	{
		return  band_;
	}
	float getSetPoint()
	{
		return  setPoint_;
	}
	int getSafeValue()
	{
		return safeValue_;
	}
	int getTimeout()
	{
		return timeout_;
	}
	Regulator(const Sensor sensor, QObject* parent = nullptr);
	Regulator(QObject* parent = nullptr);
	Sensor getSensor()
	{
		return sensor_;
	}
	virtual QString getName() const;
	virtual ~Regulator() {}

	virtual void store(QJsonObject& json);
	virtual void load(const QJsonObject& json, bool preserve);
};
