#include "sensor.h"

#include <QDebug>
#include <QJsonArray>

SensorStore globalSensors;

SensorStore::SensorStore(QObject *parent): QObject(parent)
{
}

void SensorStore::store(QJsonObject& json)
{
	QJsonArray sensorsArray;
	for(const Sensor& sensor : sensors_)
	{
		QJsonObject sensorObject;
		sensor.store(sensorObject);
		sensorsArray.append(sensorObject);
	}
	json["Sensors"] = sensorsArray;
}

void SensorStore::load(const QJsonObject& json)
{
	knownSensors_.clear();
	QJsonArray sensorsArray = json["Sensors"].toArray();
	for(const QJsonValue& value : sensorsArray)
	{
		knownSensors_.push_back(Sensor(value.toObject()));
	}
}

std::vector<QString> SensorStore::allGroups() const
{
	std::vector<QString> groups;
	for(const Sensor& sensor : sensors_)
	{
		if(!sensor.groupName.isEmpty())
		{
			bool found = false;
			for(const QString& group : groups)
			{
				if(group == sensor.groupName)
				{
					found = true;
					break;
				}
			}
			if(!found)
				groups.push_back(sensor.groupName);
		}
	}
	return groups;
}

void SensorStore::sensorGotState(const Sensor& sensor, sensor_update_type_t type)
{
	bool inSensors = false;

	qDebug()<<"Sensor update for id"<<sensor.id<<"type"<<sensor.type<<"update type"<<type<<"for"<<sensor.name<<"with group"<<sensor.groupName;

	for(unsigned i = 0; i < sensors_.size(); ++i)
	{
		if(sensor.type == sensors_[i].type && sensor.id == sensors_[i].id)
		{
			sensors_[i].updateSeen();
			bool needsUpdate = false;

			if(type == SENSOR_UPDATE_USER || type == SENSOR_UPDATE_REMOTE)
			{
				if(sensors_[i].name != sensor.name || sensors_[i].hidden != sensor.hidden || sensors_[i].groupName != sensor.groupName)
				{
					sensors_[i].name = sensor.name;
					sensors_[i].hidden = sensor.hidden;
					sensors_[i].groupName = sensor.groupName;
					for(Sensor& known : knownSensors_)
					{
						if(sensor.type == known.type && sensor.id == known.id)
						{
							known.name = sensor.name;
							known.hidden = sensor.hidden;
							known.groupName = sensor.groupName;
							break;
						}
					}
					needsUpdate = true;
				}

				if(sensors_[i].field != sensor.field && type == SENSOR_UPDATE_REMOTE)
				{
					needsUpdate = true;
					sensors_[i].field = sensor.field;
				}
			}
			else if(sensors_[i].field != sensor.field)
			{
				needsUpdate = true;
				sensors_[i].field = sensor.field;
			}

			if(needsUpdate)
			{
				sensorChangedState(sensors_[i], type);
				stateChenged(sensors_);
			}

			inSensors = true;
			break;
		}
	}
	if(!inSensors)
	{
		Sensor newSensor = sensor;
		for(const Sensor& known : knownSensors_)
		{
			if(sensor.type == known.type && sensor.id == known.id)
			{
				newSensor.name = known.name;
				newSensor.hidden = known.hidden;
				newSensor.groupName = known.groupName;
				break;
			}
		}
		sensors_.push_back(newSensor);
		sensorChangedState(newSensor, type);
		stateChenged(sensors_);
	}

	for(unsigned i = 0; i < sensors_.size(); ++i)
	{
		if(sensors_[i].type > 0 && sensors_[i].type < 128 && QDateTime::currentDateTime() > sensors_[i].lastSeen.addSecs(1800))
		{
			sensorDeleted(sensors_[i]);
			stateChenged(sensors_);
			sensors_.erase(sensors_.begin()+i);
		}
	}
}


