#include "RuntimeServices.h"

#include "RuntimeStore.h"

RuntimeServices::RuntimeServices(RuntimeEventDispatcher& runtimeEventDispatcher) :
	mControlServices(std::make_unique<ControlServices>(runtimeEventDispatcher))
{
}

RuntimeServices::~RuntimeServices()
{
	Stop();
}

bool RuntimeServices::Start(OpenGLComposite& composite, RuntimeStore& runtimeStore, std::string& error)
{
	return mControlServices && mControlServices->Start(composite, runtimeStore, error);
}

void RuntimeServices::BeginPolling(RuntimeCoordinator& runtimeCoordinator)
{
	if (mControlServices)
		mControlServices->BeginPolling(runtimeCoordinator);
}

void RuntimeServices::Stop()
{
	if (mControlServices)
		mControlServices->Stop();
}

void RuntimeServices::BroadcastState()
{
	if (mControlServices)
		mControlServices->BroadcastState();
}

void RuntimeServices::RequestBroadcastState()
{
	if (mControlServices)
		mControlServices->RequestBroadcastState();
}

bool RuntimeServices::QueueOscUpdate(const std::string& layerKey, const std::string& parameterKey, const std::string& valueJson, std::string& error)
{
	return mControlServices && mControlServices->QueueOscUpdate(layerKey, parameterKey, valueJson, error);
}

bool RuntimeServices::ApplyPendingOscUpdates(std::vector<AppliedOscUpdate>& appliedUpdates, std::string& error)
{
	if (!mControlServices)
	{
		appliedUpdates.clear();
		return true;
	}

	return mControlServices->ApplyPendingOscUpdates(appliedUpdates, error);
}

bool RuntimeServices::QueueOscCommit(const std::string& routeKey, const std::string& layerKey, const std::string& parameterKey, const JsonValue& value, uint64_t generation, std::string& error)
{
	return mControlServices && mControlServices->QueueOscCommit(routeKey, layerKey, parameterKey, value, generation, error);
}

void RuntimeServices::ClearOscState()
{
	if (mControlServices)
		mControlServices->ClearOscState();
}

void RuntimeServices::ClearOscStateForLayerKey(const std::string& layerKey)
{
	if (mControlServices)
		mControlServices->ClearOscStateForLayerKey(layerKey);
}

void RuntimeServices::ConsumeCompletedOscCommits(std::vector<CompletedOscCommit>& completedCommits)
{
	if (!mControlServices)
	{
		completedCommits.clear();
		return;
	}

	mControlServices->ConsumeCompletedOscCommits(completedCommits);
}
