#pragma once

#include <pipewire/context.h>
#include <pipewire/core.h>
#include <pipewire/pipewire.h>
#include <QObject>
#include <string>
#include <cstdint>

class PipeWireHandler: public QObject
{
	Q_OBJECT

public:
	struct PwNode
	{
		uint32_t id;
		std::string name;
	};

private:

	struct pw_thread_loop* loop;
	struct pw_context* context;
	struct pw_core* core;
	struct pw_registry* registry;
	struct spa_hook registryListener;

	void write(const char * const buffer, const size_t length);
	void write(const QByteArray& buffer);

	void decode(QByteArray buffer);
	void decodeMaster(const QByteArray& buffer);

public:

	static void registryEventHandler(void *data, uint32_t id,
									 uint32_t permissions, const char *type, uint32_t version,
									 const struct spa_dict *props);

signals:

	void nodeAdded(PwNode node);
	void nodeRemoved(PwNode node);

public:

	PipeWireHandler();
	PipeWireHandler(const PipeWireHandler&) = delete;
	PipeWireHandler operator=(const PipeWireHandler&) = delete;
	bool startLoop();
	struct pw_registry* getRegistry();

	~PipeWireHandler();
};

