//..............................................................................
//
// Pipe Monitor log representer
//

import "log_Representer.jnc"
import "PipeMonLogRecordCode.jnc"

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

bool representPipeMonLog  (
	log.RepresenterTarget* target,
	uint64_t recordCode,
	void const* p,
	size_t size,
	uint_t foldFlags
	)
{
	switch (recordCode)
	{
	case PipeMonLogRecordCode.ServerFileOpened_old:
		uint32_t fileId = *(uint32_t const*) p;
		char const* name = (char const*) (p + sizeof(fileId));
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Connect;
		target.m_lineAttr.m_backColor = log.StdLogColor.Connect;
		target.addHyperText($"File \e[34m#$fileId\e[m: Server file opened: \e[34m$name");
		break;

	case PipeMonLogRecordCode.ServerFileOpened:
		uint32_t fileId = *(uint32_t const*) p;
		uint32_t pid = *((uint32_t const*) p + 1);
		char const* name = (char const*) (p + sizeof(fileId) + sizeof(pid));
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Connect;
		target.m_lineAttr.m_backColor = log.StdLogColor.Connect;
		target.addHyperText($"PID \e[34m#$pid\e[m file \e[34m#$fileId\e[m: Server file opened: \e[34m$name");
		break;

	case PipeMonLogRecordCode.ServerFileOpenError_old:
		std.Error const* error = (std.Error const*) p;
		char const* name = (char const*) (error + 1);
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Error;
		target.m_lineAttr.m_backColor = log.StdLogColor.Error;
		target.addHyperText($"Cannot open server file \e[34m$name\e[m$: $(error.m_description)");
		break;

	case PipeMonLogRecordCode.ServerFileOpenError:
		uint32_t pid = *(uint32_t const*) p;
		std.Error const* error = (std.Error const*) (p + sizeof(pid));
		char const* name = (char const*) (error + 1);
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Error;
		target.m_lineAttr.m_backColor = log.StdLogColor.Error;
		target.addHyperText($"PID \e[34m#$pid\e[m: Cannot open server file \e[34m$name\e[m$: $(error.m_description)");
		break;

	case PipeMonLogRecordCode.ClientFileOpened_old:
		uint32_t fileId = *(uint32_t const*) p;
		char const* name = (char const*) (p + sizeof(fileId));
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Connect;
		target.m_lineAttr.m_backColor = log.StdLogColor.Connect;
		target.addHyperText($"File \e[34m#$fileId\e[m: Client file opened: \e[34m$name");
		break;

	case PipeMonLogRecordCode.ClientFileOpened:
		uint32_t fileId = *(uint32_t const*) p;
		uint32_t pid = *((uint32_t const*) p + 1);
		char const* name = (char const*) (p + sizeof(fileId) + sizeof(pid));
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Connect;
		target.m_lineAttr.m_backColor = log.StdLogColor.Connect;
		target.addHyperText($"PID \e[34m#$pid\e[m file \e[34m#$fileId\e[m: Client file opened: \e[34m$name");
		break;

	case PipeMonLogRecordCode.ClientFileOpenError_old:
		std.Error const* error = (std.Error const*) p;
		char const* name = (char const*) (error + 1);
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Error;
		target.m_lineAttr.m_backColor = log.StdLogColor.Error;
		target.addHyperText($"Cannot open client file \e[34m$name\e[m: $(error.m_description)");
		break;

	case PipeMonLogRecordCode.ClientFileOpenError:
		uint32_t pid = *(uint32_t const*) p;
		std.Error const* error = (std.Error const*) (p + sizeof(pid));
		char const* name = (char const*) (error + 1);
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Error;
		target.m_lineAttr.m_backColor = log.StdLogColor.Error;
		target.addHyperText($"PID \e[34m#$pid\e[m: Cannot open client file \e[34m$name\e[m: $(error.m_description)");
		break;

	case PipeMonLogRecordCode.FileClosed:
		target.m_lineAttr.m_flags = log.LineAttrFlags.Override;
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Disconnect;
		target.m_lineAttr.m_backColor = log.StdLogColor.Disconnect;
		target.addHyperText(PipeMonLogPartCode.Suffix, "File closed");
		break;

	case PipeMonLogRecordCode.ConnectionAccepted:
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Info;
		target.m_lineAttr.m_backColor = log.StdLogColor.Info;
		target.addHyperText(PipeMonLogPartCode.Suffix, "Connection accepted");
		break;

	case PipeMonLogRecordCode.ConnectionClosed:
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Info;
		target.m_lineAttr.m_backColor = log.StdLogColor.Info;
		target.addHyperText(PipeMonLogPartCode.Suffix, "Connection closed");
		break;

	case PipeMonLogRecordCode.Transaction:
		PipeMonLogTransactionParams const* params = (PipeMonLogTransactionParams const*) p;
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.TxRx;
		target.m_lineAttr.m_backColor = gui.StdColor.PastelGray;
		target.addHyperText(PipeMonLogPartCode.Suffix, $"Pipe transaction: TX: \e[34m$(params.m_txDataSize)\e[m B; RX: \e[34m$(params.m_rxDataSize)\e[m B");
		break;

	case PipeMonLogRecordCode.FileIdChanged_old:
		uint32_t fileId = *(uint32_t const*) p;
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Info;
		target.m_lineAttr.m_backColor = log.StdLogColor.Info;
		target.addHyperText(PipeMonLogPartCode.Prefix, $"File \e[34m#$fileId\e[m: ");
		break;

	case PipeMonLogRecordCode.FileIdChanged:
		uint32_t fileId = *(uint32_t const*) p;
		uint32_t pid = *((uint32_t const*) p + 1);
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Info;
		target.m_lineAttr.m_backColor = log.StdLogColor.Info;
		target.addHyperText(PipeMonLogPartCode.Prefix, $"PID \e[34m#$pid\e[m file \e[34m#$fileId\e[m: ");
		break;

	default:
		return false;
	}

	return true;
}

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