//..............................................................................
//
// Pipe listener log representer
//

import "log_Representation.jnc"
import "PipeServerLogRecordCode.jnc"

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

bool representPipeServerLog  (
	log.Representation* representation,
	uint64_t recordCode,
	void const* p,
	size_t size,
	uint_t foldFlags
	)
{
	switch (recordCode)
	{
	case PipeServerLogRecordCode.Listening:
		char const* name = (char const*)p;
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Listen;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Info;
		representation.addHyperText($"Listening on \e[34m$name");
		break;

	case PipeServerLogRecordCode.ListenError:
		std.Error const* error = (std.Error const*)p;
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Error;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Error;
		representation.addHyperText($"Cannot listen: $(error.m_description)");
		break;

	case PipeServerLogRecordCode.ListenStopped:
		char const* name = (char const*)p;
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.StopListen;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Info;
		representation.addHyperText($"Stopped listening on \e[34m$name");
		break;

	case PipeServerLogRecordCode.ClientConnected_0:
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Connect;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Connect;
		representation.addHyperText("Client connected");
		break;

	case PipeServerLogRecordCode.ClientDisconnected_0:
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Disconnect;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Disconnect;
		representation.addHyperText("Client disconnected");
		break;

	case PipeServerLogRecordCode.ClientRejected:
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Warning;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Warning;
		representation.addHyperText("Client connected and rejected ");
		break;

	case PipeServerLogRecordCode.ClientConnected:
		uint64_t id = *(uint64_t const*)p;
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Connect;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Connect;
		representation.addHyperText($"Client ID \e[34m0x%llX\e[m connected"(id));
		break;

	case PipeServerLogRecordCode.ClientDisconnected:
		PipeDisconnectLogRecordParams const* params = (PipeDisconnectLogRecordParams const*)p;
		char const* initiator = (params.m_flags & PipeDisconnectLogRecordFlags.Remote) ? "remote" : "local";
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Disconnect;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Disconnect;

		representation.addHyperText(
			$"Client ID \e[34m0x%llX\e[m diconnected (closed by %2 node)"(
			params.m_id,
			initiator
			));
		break;

	case PipeServerLogRecordCode.ClientChanged:
		uint64_t id = *(uint64_t const*)p;
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Info;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Info;
		representation.addHyperText($"Client ID \e[34m0x%llX\e[m:"(id));
		break;

	default:
		return false;
	}

	return true;
}

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