using System; using System.Collections.Generic; using System.Linq; using System.Windows; using static C4IT.Logging.cLogManager; namespace FasdDesktopUi.Basics.Services.ProtocollService { internal class F4SDProtocoll { private readonly ICollection _protocollEntries = new List(); public static F4SDProtocoll Instance { get; private set; } = new F4SDProtocoll(); private F4SDProtocoll() { } internal void Add(IProtocollEntry entry) => _protocollEntries.Add(entry); internal void Add(IEnumerable entries) { foreach (var entry in entries) { Add(entry); } } internal void Clear() => _protocollEntries.Clear(); internal T GetLatestOfType() where T : IProtocollEntry => _protocollEntries.OfType().LastOrDefault(); internal IEnumerable GetOfType(int? count = null) where T : IProtocollEntry { if(count.HasValue) return _protocollEntries.OfType().Reverse().Take(count.Value).Reverse(); return _protocollEntries.OfType(); } internal IEnumerable GetAll() => _protocollEntries; internal DataObject GetLatestOfTypeAsDataObject(bool skipHtmlFrame) where T : IProtocollEntry { IProtocollEntry entry = _protocollEntries.OfType().LastOrDefault(); if (entry is null) return new DataObject(); return GetDataObjectOf(new IProtocollEntry[] { entry }, skipHtmlFrame); } internal DataObject GetOfTypeAsDataObject(bool skipHtmlFrame, int? count = null) where T : IProtocollEntry => GetDataObjectOf(GetOfType(count).Cast(), skipHtmlFrame); internal DataObject GetAllAsDataObject(bool skipHtmlFrame) => GetDataObjectOf(_protocollEntries, skipHtmlFrame); private DataObject GetDataObjectOf(IEnumerable entries, bool skipHtmlFrame, int? count = null) { DataObject dataObject = new DataObject(); const string asciiSeparator = "\n\n\n"; const string htmlSeparator = "


"; try { string ascii = string.Empty; string html = string.Empty; if (count.HasValue) entries = entries.Reverse().Take(count.Value).Reverse(); foreach (IProtocollEntry entry in entries) { string entryAscii = entry.GetAscii(); if (!string.IsNullOrEmpty(entryAscii)) { ascii += entryAscii; ascii += asciiSeparator; } string entryHtml = entry.GetHtml(); if (!string.IsNullOrEmpty(entryHtml)) { html += entryHtml; html += htmlSeparator; } } if (ascii.EndsWith(asciiSeparator)) ascii = ascii.Remove(ascii.Length - asciiSeparator.Length); if (html.EndsWith(htmlSeparator)) html = html.Remove(html.Length - htmlSeparator.Length); dataObject.SetText(ascii); if (!string.IsNullOrEmpty(html)) { html = skipHtmlFrame ? html : cUtility.GetHtmlFrame(html); dataObject.SetText(html, TextDataFormat.Html); } } catch (Exception ex) { LogException(ex); } return dataObject; } } }