#ifndef RELAYSCROLLBOX_H
#define RELAYSCROLLBOX_H

#include <QWidget>
#include <vector>
#include <memory>
#include <QScrollArea>
#include <QSpacerItem>
#include <QJsonObject>
#include "itemwidget.h"
#include "../items/item.h"
#include "../items/itemstore.h"


namespace Ui
{
class RelayScrollBox;
}

class ItemScrollBox : public QWidget
{
	Q_OBJECT
private:
	struct Tab
	{
		QScrollArea* scroller;
		QWidget* content;
		QSpacerItem* spacer;
	};

	QMap<QString, Tab> tabs_;
	QMap<QString, std::vector<ItemWidget*>> widgets_;
	Ui::RelayScrollBox *ui;
	QString pendingSelectedGroup_;

signals:
	void deleteRequest(const ItemData& item);


public:
	explicit ItemScrollBox(QWidget *parent = nullptr);
	~ItemScrollBox();

	void setItemStore(ItemStore* itemStore);

	void store(QJsonObject& json) const;
	void load(const QJsonObject& json);

public slots:

	void addItem(std::weak_ptr<Item> item);
	void removeItem(const ItemData& item);
	void onItemUpdate(const ItemUpdateRequest& update);

private:
	void ensureTabExists(const QString& groupName);
	void cleanupEmptyTabs();

private:
	void addItemToTabs(std::weak_ptr<Item> item);
	void removeItemFromTabs(const ItemData& item);
};

#endif // RELAYSCROLLBOX_H


