//.............................................................................
//
// SSH Connection log packet declarations and representer
//

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

enum SshChannelLogRecordCode
{
	Resolving,
	ResolveCompleted,
	TcpConnecting,
	TcpConnectCompleted,
	SshHandshakeCompleted,
	SshAuthenticating,
	SshAuthCompleted,
	SshChannelOpened,
	SshPtyRequested,
	SshProcessStarted,
	ConnectCancelled,
	ConnectCompleted,
	ConnectError,
	Disconnected,
	Reconnecting,
}

enum SshChannelLogPartCode
{
	Connecting       = 1 | log.PartCodeFlags.MergeableForward,
	ConnectCompleted = 1 | log.PartCodeFlags.MergeableBackward,
}

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

bool representSshChannelLog (
	log.RepresenterTarget* target,
	uint_t recordCode,
	void const* p,
	size_t size,
	uint_t foldFlags
	)
{
	switch (recordCode)
	{
	case SshChannelLogRecordCode.Resolving:
		char const* addressString = (char const*) p;
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Info;
		target.m_lineAttr.m_backColor = log.StdLogColor.Info;
		target.addHyperText (SshChannelLogPartCode.Connecting, $"Resolving \e[34m$addressString\e[m...");
		break;

	case SshChannelLogRecordCode.ResolveCompleted:
		char const* addressString = ((io.SocketAddress const*) p).getString ();
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Info;
		target.m_lineAttr.m_backColor = log.StdLogColor.Info;
		target.addHyperText (SshChannelLogPartCode.ConnectCompleted, $"ok (\e[34m$addressString\e[m)");
		break;

	case SshChannelLogRecordCode.TcpConnecting:
		char const* addressString = ((io.SocketAddress const*) p).getString ();
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Info;
		target.m_lineAttr.m_backColor = log.StdLogColor.Info;
		target.addHyperText (SshChannelLogPartCode.Connecting, $"Establishing TCP connection to \e[34m$addressString\e[m...");
		break;

	case SshChannelLogRecordCode.TcpConnectCompleted:
		target.m_lineAttr.m_flags = log.LineAttrFlags.TileIcon;
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Info;
		target.m_lineAttr.m_backColor = log.StdLogColor.Info;
		target.addHyperText (SshChannelLogPartCode.ConnectCompleted, "ok");
		target.addHyperText (SshChannelLogPartCode.Connecting, "Performing SSH handshake...");
		break;

	case SshChannelLogRecordCode.SshHandshakeCompleted:
		target.m_lineAttr.m_flags = log.LineAttrFlags.TileIcon;
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Info;
		target.m_lineAttr.m_backColor = log.StdLogColor.Info;
		target.addHyperText (SshChannelLogPartCode.ConnectCompleted, "ok");
		
		// and fall through...

	case SshChannelLogRecordCode.SshAuthenticating:
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Info;
		target.m_lineAttr.m_backColor = log.StdLogColor.Info;
		target.addHyperText (SshChannelLogPartCode.Connecting, "Performing SSH authentification...");
		break;

	case SshChannelLogRecordCode.SshAuthCompleted:
		target.m_lineAttr.m_flags = log.LineAttrFlags.TileIcon;
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Info;
		target.m_lineAttr.m_backColor = log.StdLogColor.Info;
		target.addHyperText (SshChannelLogPartCode.ConnectCompleted, "ok");
		target.addHyperText (SshChannelLogPartCode.Connecting, "Opening SSH channel...");
		break;

	case SshChannelLogRecordCode.SshChannelOpened:
		target.m_lineAttr.m_flags = log.LineAttrFlags.TileIcon;
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Info;
		target.m_lineAttr.m_backColor = log.StdLogColor.Info;
		target.addHyperText (SshChannelLogPartCode.ConnectCompleted, "ok");
		target.addHyperText (SshChannelLogPartCode.Connecting, "Requesting PTY...");
		break;

	case SshChannelLogRecordCode.SshPtyRequested:
		target.m_lineAttr.m_flags = log.LineAttrFlags.TileIcon;
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Info;
		target.m_lineAttr.m_backColor = log.StdLogColor.Info;
		target.addHyperText (SshChannelLogPartCode.ConnectCompleted, "ok");
		target.addHyperText (SshChannelLogPartCode.Connecting, "Starting process...");
		break;

	case SshChannelLogRecordCode.SshProcessStarted:
		target.addHyperText (SshChannelLogPartCode.ConnectCompleted, "ok");
		break;

	case SshChannelLogRecordCode.ConnectCompleted:
		target.m_lineAttr.m_iconIdx  = log.StdLogIcon.Play;
		target.m_lineAttr.m_backColor = log.StdLogColor.Start;
		target.addHyperText ("SSH channel established");
		break;

	case SshChannelLogRecordCode.ConnectCancelled:	
		target.addHyperText (SshChannelLogPartCode.ConnectCompleted, "cancelled");
		break;

	case SshChannelLogRecordCode.ConnectError:
		target.m_lineAttr.m_flags = log.LineAttrFlags.Override;
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Error;
		target.m_lineAttr.m_backColor = log.StdLogColor.Error;

		std.Error const* Error = (std.Error const*) p;
		target.addHyperText (SshChannelLogPartCode.ConnectCompleted, $"error ($(Error.m_description))");
		break;

	case SshChannelLogRecordCode.Disconnected:
		char const* addressString = ((io.SocketAddress const*) p).getString ();
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Pause;
		target.m_lineAttr.m_backColor = log.StdLogColor.Stop;
		target.addHyperText ($"Disconnected from \e[34m$addressString");
		break;

	case SshChannelLogRecordCode.Reconnecting:
		target.m_lineAttr.m_iconIdx = log.StdLogIcon.Info;
		target.m_lineAttr.m_backColor = log.StdLogColor.Info;
		target.addHyperText (SshChannelLogPartCode.Connecting, "Reconnecting...");
		break;

	default:
		return false;
	}

	return true;
}

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