//.............................................................................
//
// Pipe Monitor log packet declarations and representer
//

import "log.jnc"

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

enum PipeMonLogRecordCode
{
	CaptureStarted, // don't mix with PipeLogRecordCode
	CaptureStopped,
	CaptureError,
	ServerFileOpened, 
	ServerFileOpenError, 
	ClientFileOpened, 
	ClientFileOpenError, 
	FileClosed,
	ConnectionAccepted,
	ConnectionClosed,
	FileIdChanged,
}

enum PipemonLogPartCode
{
	Prefix = log.PartCodeFlags.MergeableForward | 1,
	Suffix = log.PartCodeFlags.MergeableBackward | 1,
}

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

bool representPipeMonLog  (
	log.RepresenterTarget* target,
	uint_t recordCode,
	void const* p,
	size_t size,
	uint_t foldFlags
	)
{
	switch (recordCode)
	{
	case PipeMonLogRecordCode.CaptureStarted:
		char const* nameFilter = (char const*) p;
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Listen;
		target.m_lineAttr.m_backColor = log.StdLogColor.Info;
		target.addHyperText ($"Capture started with filter \e[34m$nameFilter");
		break;

	case PipeMonLogRecordCode.CaptureStopped:
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.StopListen;
		target.m_lineAttr.m_backColor = log.StdLogColor.Info;
		target.addHyperText ("Capture stopped");
		break;

	case PipeMonLogRecordCode.CaptureError:
		std.Error const* error = (std.Error const*) p;
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Error;
		target.m_lineAttr.m_backColor = log.StdLogColor.Error;
		target.addHyperText ($"Cannot start capture: $(error.m_description)");
		break;

	case PipeMonLogRecordCode.ServerFileOpened:
		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.ServerFileOpenError:
		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.ClientFileOpened:
		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.ClientFileOpenError:
		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.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.FileIdChanged:
		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: ");
		break;

	default:
		return false;
	}

	return true;
}

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