//..............................................................................
//
// TCP Listener log packet declarations and representer
//

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

enum TcpListenerLogRecordCode
{
	Listening,
	ListenError,
	ListenStopped,
	ClientConnected,
	ClientDisconnected,
	ClientRejected,
}

bitflag enum TcpDisconnectLogRecordFlags
{
	Remote,
	Reset,
}

struct TcpDisconnectLogRecordParams
{
	io.SocketAddress m_address;
	uint_t m_flags;
}

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

bool representTcpListenerLog (
	log.RepresenterTarget* target,
	uint_t recordCode,
	void const* p,
	size_t size,
	uint_t foldFlags
	)
{
	switch (recordCode)
	{
	case TcpListenerLogRecordCode.Listening:
		char const* addressString = ((io.SocketAddress const*) p).getString ();
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Listen;
		target.m_lineAttr.m_backColor = log.StdLogColor.Info;
		target.addHyperText ($"Listening on \e[34m$addressString");
		break;

	case TcpListenerLogRecordCode.ListenError:
		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 listen: $(error.m_description)");
		break;

	case TcpListenerLogRecordCode.ListenStopped:
		char const* addressString = ((io.SocketAddress const*) p).getString ();
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.StopListen;
		target.m_lineAttr.m_backColor = log.StdLogColor.Info;
		target.addHyperText ($"Stopped listening on \e[34m$addressString");
		break;

	case TcpListenerLogRecordCode.ClientConnected:
		char const* addressString = ((io.SocketAddress const*) p).getString ();
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Connect;
		target.m_lineAttr.m_backColor = log.StdLogColor.Connect;
		target.addHyperText ($"Client connected from \e[34m$addressString");
		break;

	case TcpListenerLogRecordCode.ClientDisconnected:
		TcpDisconnectLogRecordParams const* params = (TcpDisconnectLogRecordParams const*) p;
		char const* addressString = params->m_address.getString ();
		char const* closeKind = (params.m_flags & TcpDisconnectLogRecordFlags.Reset) ? "reset" : "closed";
		char const* initiator = (params.m_flags & TcpDisconnectLogRecordFlags.Remote) ? "remote" : "local";

		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Disconnect;
		target.m_lineAttr.m_backColor = log.StdLogColor.Disconnect;
		target.addHyperText ($"Client \e[34m$addressString\e[m disconnected ($closeKind by $initiator node)");
		break;

	case TcpListenerLogRecordCode.ClientRejected:
		char const* addressString = ((io.SocketAddress const*) p).getString ();
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Warning;
		target.m_lineAttr.m_backColor = log.StdLogColor.Warning;
		target.addHyperText ($"Client \e[34m$addressString\e[m is rejected ");
		break;

	default:
		return false;
	}

	return true;
}

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