//..............................................................................
//
// Pcap log representer
//

import "io_base.jncx"
import "io_SocketAddress.jnc"
import "log_Representation.jnc"
import "PcapLogRecordCode.jnc"

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

bool representPcapLog(
	log.Representation* representation,
	uint64_t recordCode,
	void const* p,
	size_t size,
	uint_t foldFlags
) {
	switch (recordCode) {
	case PcapLogRecordCode.StartingCapture:
		char const* filter = p;
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Info;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Info;

		representation.addHyperText(
			PcapLogPartCode.StartingCapture,
			size && *filter ?
				$"Starting capture (capture filter '\e[34m$filter\e[m')..." :
				"Starting capture (no capture filter)..."
			);

		break;

	case PcapLogRecordCode.CaptureStarted:
		representation.m_lineAttr.m_flags = log.LineAttrFlags.Override;
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Listen;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Info;
		representation.addPlainText(PcapLogPartCode.CaptureCompleted, "ok");
		break;

	case PcapLogRecordCode.CaptureError:
		std.Error const* error = (std.Error const*) p;
		representation.m_lineAttr.m_flags = log.LineAttrFlags.Override;
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Error;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Error;
		representation.addPlainText(PcapLogPartCode.CaptureCompleted, $"error: $(error.m_description)");
		break;

	case PcapLogRecordCode.CaptureStopped:
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.StopListen;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Info;
		representation.addPlainText("Capture stopped");
		break;

	case PcapLogRecordCode.ChangingCaptureFilter:
		char const* filter = (char const*) p;
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Info;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Info;
		representation.addHyperText(PcapLogPartCode.ChangingCaptureFilter, $"Changing capture filter to \e[34m$filter\e[m...");
		break;

	case PcapLogRecordCode.CaptureFilterChanged:
		representation.addPlainText(PcapLogPartCode.CaptureFilterCompleted, "ok");
		break;

	case PcapLogRecordCode.CaptureFilterError:
		std.Error const* error = (std.Error const*) p;
		representation.m_lineAttr.m_flags = log.LineAttrFlags.Override;
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Error;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Error;
		representation.addPlainText(PcapLogPartCode.CaptureFilterCompleted, $"error: $(error.m_description)");
		break;

	case PcapLogRecordCode.SetCaptureFilter:
		char const* filter = (char const*) p;
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Info;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Info;
		representation.addHyperText(
			size && *filter ?
				$"Set capture filter \e[34m$filter" :
				"Cleared capture filter"
			);
		break;

	case PcapLogRecordCode.SetAddressFilter:
		io.SocketAddress const* address = (io.SocketAddress const*)p;
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Info;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Info;
		representation.addHyperText(
			address.m_family ?
				$"Set capture address filter \e[34m$(address.getString())" :
				"Cleared capture address filter"
			);

		break;


	default:
		return false;
	}

	return true;
}

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