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

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

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

bool $(logRepresenterFunc)(
	log.Representation* representation,
	uint64_t recordCode,
	void const* p,
	size_t size,
	uint_t foldFlags
	)
{
	$(protocol)Hdr const* hdr = ($(protocol)Hdr const*)p;
	if (recordCode != $(logRecordCodeEnum).Packet || size < sizeof($(protocol)Hdr) || size < hdr.m_size)
		return false;

	switch (hdr.m_code) {
	case $(protocol)Code.Connect:
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Connect;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Connect;
		representation.addHyperText($"\e[1m$(protocol):\e[m Connect");
		break;

	case $(protocol)Code.Disconnect:
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Disconnect;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Disconnect;
		representation.addHyperText($"\e[1m$(protocol):\e[m Disconnect");
		break;

	case $(protocol)Code.Read:
		if (size < sizeof($(protocol)Read))
			return false;

		$(protocol)Read const* read = ($(protocol)Read const*)p;
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.RxPacket;
		representation.m_lineAttr.m_backColor = ui.StdColor.PastelGray;
		representation.addHyperText($"\e[1m$(protocol):\e[m Read @%04X (%2 byte(s))"(read.m_address, read.m_dataSize));
		break;

	case $(protocol)Code.Write:
		if (size < sizeof($(protocol)Write))
			return false;

		$(protocol)Write const* write = ($(protocol)Write const*)p;
		size_t dataSize = (size - sizeof($(protocol)Write));

		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.RxPacket;
		representation.m_lineAttr.m_backColor = ui.StdColor.PastelGray;
		representation.addHyperText($"\e[1m$(protocol):\e[m Write @%04X (%2 byte(s))"(write.m_address, dataSize));

		representation.m_lineAttr.m_backColor = ui.ColorFlags.Undefined;
		representation.addBin(write + 1, dataSize);
		break;

	case $(protocol)Code.Reply:
		if (size < sizeof($(protocol)Reply))
			return false;

		$(protocol)Reply const* reply = ($(protocol)Reply const*)p;
		if (reply.m_status) {
			std.Error error;
			error.m_size = sizeof(error);
			error.m_guid = std.g_errnoGuid;
			error.m_code = reply.m_status;

			representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Error;
			representation.m_lineAttr.m_backColor = log.StdLogColor.Error;
			representation.addHyperText($"\e[1mMyProto:\e[m \e[31mERROR\e[m: %1"(error.m_description));
		} else {
			representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Info;
			representation.m_lineAttr.m_backColor = log.StdLogColor.Info;
			representation.addHyperText($"\e[1mMyProto:\e[m \e[32mOK\e[m");

			if (size > sizeof($(protocol)Reply)) {
				representation.m_lineAttr.m_backColor = ui.ColorFlags.Undefined;
				representation.addBin(reply + 1, size - sizeof($(protocol)Reply));
			}
		}

		break;

	default:
		return false;
	}

	return true;
}

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