//..............................................................................
//
// Generic file log representer
//

import "io_base.jncx"
import "io_FileStream.jnc"
import "log_Representation.jnc"

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

bool representFileLog(
	log.Representation* representation,
	uint64_t recordCode,
	void const* p,
	size_t size,
	uint_t foldFlags
) {
	switch (recordCode) {
	case FileLogRecordCode.Opened_0:
		io.FileKind const* kind = (io.FileKind const*)p;
		string_t fileName = (char const*)(kind + 1);
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Connect;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Connect;
		representation.addHyperText($"Opened file \e[34m$fileName\e[m ($(io.getFileStreamKindString(*kind)))");
		break;

	case FileLogRecordCode.Opened:
		FileLogOpenParams const* params = (FileLogOpenParams const*)p;
		string_t fileName = (char const*)(params + 1);
		string_t kindString = io.getFileStreamKindString(params.m_kind);
		string_t modeString =
			(params.m_openFlags & io.FileOpenFlags.ReadOnly) ? ", read-only" :
			(params.m_openFlags & io.FileOpenFlags.WriteOnly) ? ", write-only" :
			null;

		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Connect;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Connect;
		representation.addHyperText($"Opened file \e[34m$fileName\e[m ($kindString$modeString)");
		break;

	case FileLogRecordCode.FileOpened:
		string_t fileName = (char const*)p;
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Connect;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Connect;
		representation.addHyperText($"Opened file \e[34m$fileName");
		break;

	case FileLogRecordCode.OpenError:
		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 open file: $(error.m_description)");
		break;

	case FileLogRecordCode.Closed:
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Disconnect;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Disconnect;
		representation.addHyperText($"File closed");
		break;

	case FileLogRecordCode.Eof:
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Info;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Info;
		representation.addHyperText("End-of-file reached");
		break;

	case FileLogRecordCode.Cleared:
		representation.m_lineAttr.m_iconIdx = log.StdLogIcon.Info;
		representation.m_lineAttr.m_backColor = log.StdLogColor.Info;
		representation.addHyperText($"File cleared");
		break;

	default:
		return false;
	}

	return true;
}

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