#pragma once

#include "RuntimeShaderArtifact.h"
#include "RuntimeSlangShaderCompiler.h"

#include <atomic>
#include <functional>
#include <string>
#include <thread>

class RuntimeShaderBridge
{
public:
	using ArtifactCallback = std::function<void(const RuntimeShaderArtifact&)>;
	using ErrorCallback = std::function<void(const std::string&)>;

	RuntimeShaderBridge() = default;
	RuntimeShaderBridge(const RuntimeShaderBridge&) = delete;
	RuntimeShaderBridge& operator=(const RuntimeShaderBridge&) = delete;
	~RuntimeShaderBridge();

	void Start(const std::string& shaderId, ArtifactCallback onArtifactReady, ErrorCallback onError);
	void Start(const std::string& layerId, const std::string& shaderId, ArtifactCallback onArtifactReady, ErrorCallback onError);
	void RequestStop();
	void Stop();
	bool CanStopWithoutWaiting() const;

private:
	void ThreadMain();

	RuntimeSlangShaderCompiler mCompiler;
	std::thread mThread;
	std::atomic<bool> mStopping{ false };
	std::atomic<bool> mFinished{ true };
	std::string mLayerId;
	ArtifactCallback mOnArtifactReady;
	ErrorCallback mOnError;
};
