//..............................................................................
//
// $(pluginName) log representer
//

import "log_Representation.jnc"
import "log_RepresentDynamicLayout.jnc"
import "$(protocolFile)"

//..............................................................................

bool $(logRepresenterFunc)(
	log.Representation* representation,
	uint64_t recordCode,
	void const* p,
	size_t size,
	uint_t foldFlags
) {
	switch (recordCode) {
	case $(protocol)LogRecordCode.ParseError:
		std.Error const* error = (std.Error const*)p;
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Error;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Warning;
		representation.addHyperText($"\e[1m$(protocol):\e[m parse error: %1"(error.m_description));
		break;

	case $(protocol)LogRecordCode.TxPacket:
	case $(protocol)LogRecordCode.RxPacket:
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.TxPacket + recordCode - $(protocol)LogRecordCode.TxPacket;

		jnc.DynamicLayout layout(jnc.DynamicLayoutMode.Stream | jnc.DynamicLayoutMode.Save, p, size);
		jnc.Promise* promise = layout$(protocol)(layout);
		if (layout.m_isIncomplete) {
			representation.m_lineAttr.m_backColor = ui.StdColor.PastelYellow;
			representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Warning;
			representation.addHyperText($"Incomplete $(protocol) packet: %1 of %2 bytes"(layout.m_bufferSize, layout.m_size));
		} else if (promise.m_error) {
			representation.m_lineAttr.m_backColor = ui.StdColor.PastelOrange;
			representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Warning;
			representation.addHyperText($"$(protocol) parse error: %1"(promise.m_error.m_description));
		} else
			log.representDynamicLayout(representation, layout, foldFlags);

		break;

	default:
		return false;
	}

	return true;
}

//..............................................................................
