#ifndef SH_ui_sync_logic_H
#define SH_ui_sync_logic_H

#include <QObject>
#include <QMap>
#include <pimap.h>
#include <pibytearray.h>
#include <piqt.h>

class UiSync {
	public:

	typedef PIMap<uint, QVariant> ObjectProperties;
	typedef PIMap<uint, ObjectProperties> ObjectPropertiesMap;

	void setSyncProperties(const PIMap<QString, QVector<QByteArray>> & names);
	const QMap<uint, QString> & propertiesByClassname(const QString & cn);

	static uint getObjectHash(const QObject * o);
	static uint createObjectHash(const QObject * o);
	static void writeObjectHash(QObject * o);
	static uint stringHash(const QString & s);

	static QByteArray serializeDiff(const ObjectPropertiesMap & diff);
	static ObjectPropertiesMap deserializeDiff(const QByteArray & diff);

	void addObjectProperty(ObjectProperties & ret, const QObject * o, const char * pname);
	ObjectProperties makeObjectProperties(const QObject * o);
	ObjectPropertiesMap makeObjectListProperties(QObjectList list);

	void overrideObjectProperties(ObjectProperties & dst, const ObjectProperties & income);
	void overrideObjectListProperties(ObjectPropertiesMap & dst, const ObjectPropertiesMap & income);

	ObjectProperties objectPropertiesDiff(const ObjectProperties & src, const ObjectProperties & income);
	ObjectPropertiesMap objectListPropertiesDiff(const ObjectPropertiesMap & src, const ObjectPropertiesMap & income);

	void applyObjectListProperties(ObjectPropertiesMap & income);
	void applyObjectProperties(const ObjectProperties & income);

private:

	PIMap<QString, QVector<QByteArray>> property_names;
	PIMap<QString, QMap<uint, QString> > properties_by_classname;

};

inline PICout operator<<(PICout s, const QVariant & v) {s << Q2PIVariant(v); return s;}

#endif
