#include <yuvChannelStimulusBuffer.h>
#include <stdexcept>

namespace smo {
namespace stim_buff {
namespace lcamera_buff {

namespace {

constexpr const char *qualeIfaceColourYuvY = "colour-yuv-y";
constexpr const char *qualeIfaceColourYuvU = "colour-yuv-u";
constexpr const char *qualeIfaceColourYuvV = "colour-yuv-v";

} // namespace

YuvChannelKind YuvChannelStimulusBuffer::yuvChannelKindFromQualeIfaceApi(
	const std::string& qualeIfaceApi)
{
	if (qualeIfaceApi == qualeIfaceColourYuvY) {
		return YuvChannelKind::Y;
	}

	if (qualeIfaceApi == qualeIfaceColourYuvU) {
		return YuvChannelKind::U;
	}

	if (qualeIfaceApi == qualeIfaceColourYuvV) {
		return YuvChannelKind::V;
	}

	throw std::runtime_error(
		"lcameraBuff: unsupported qualeIfaceApi '" + qualeIfaceApi
		+ "'; expected colour-yuv-y, colour-yuv-u, or colour-yuv-v");
}

StagingBuffer::IOEngineConstraints yuvChannelStagingInputConstraints(
	size_t channelByteSize)
{
	const size_t pageSize = static_cast<size_t>(sysconf(_SC_PAGE_SIZE));

	return StagingBuffer::IOEngineConstraints(
		1,
		channelByteSize,
		pageSize,
		pageSize);
}

YuvChannelStimulusBuffer::YuvChannelStimulusBuffer(
	StimulusProducer& parent,
	const std::shared_ptr<device::DeviceAttachmentSpec>& deviceAttachmentSpec,
	int histbuffMs,
	YuvChannelKind _channelKind,
	size_t _channelByteSize,
	const SmoCallbacks& callbacks,
	cl_mem_flags flags)
:	StimulusBuffer(
		parent,
		deviceAttachmentSpec,
		histbuffMs,
		yuvChannelStagingInputConstraints(_channelByteSize),
		yuvChannelStagingInputConstraints(_channelByteSize),
		callbacks,
		flags),
channelKind(_channelKind),
channelByteSize(_channelByteSize)
{}

} // namespace lcamera_buff
} // namespace stim_buff
} // namespace smo
