#pragma once
#include <vector>
#include <memory>
#include "item.h"
#include "itemsource.h"

#include <QJsonObject>

class ItemStore: public QObject
{
	Q_OBJECT
private:
	std::vector<std::shared_ptr<Item> > items_;

public:

	ItemStore(QObject *parent = nullptr);
	virtual ~ItemStore() {}

	inline std::vector< std::shared_ptr<Item> >* getItems()
	{
		return &items_;
	}

	std::shared_ptr<Item> getItem(uint32_t id);

	void registerItemSource(ItemSource* source);
	void store(QJsonObject &json);

	void clear();

signals:

	void itemDeleted(ItemData item);
	void itemAdded(std::weak_ptr<Item> Item);
	void itemUpdated(ItemUpdateRequest update);
	void sigRefresh();

public slots:

	void removeItem(const ItemData& item);
	void addItem(const ItemAddRequest& item);
	void addItems(const std::vector<ItemAddRequest>& itemsIn);
	void replaceItems(const std::vector<std::shared_ptr<Item>>& items);
	void updateItems(const std::vector<ItemUpdateRequest>& updates);
	void updateItem(const ItemUpdateRequest& update);
	void refresh();

private slots:
	void itemUpdateSlot(ItemUpdateRequest update);
};

extern ItemStore globalItems;
