using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Windows; using System.Windows.Input; using DesktopBot.Engine; using DesktopBot.Models; namespace DesktopBot.ViewModels { /// /// ViewModel per il Live Log /// public class LiveLogViewModel : BaseViewModel { private ObservableCollection _logs = new ObservableCollection(); public ObservableCollection Logs { get => _logs; set => SetProperty(ref _logs, value); } public ICommand ClearLogsCommand { get; } public ICommand CopyLogsCommand { get; } public LiveLogViewModel(AutomatedBotEngine botEngine) { ClearLogsCommand = new RelayCommand(_ => Logs.Clear()); CopyLogsCommand = new RelayCommand(_ => { if (Logs.Count == 0) return; var sb = new StringBuilder(); // I log sono inseriti in testa (più recente prima): li copiamo in ordine cronologico foreach (var entry in Logs.Reverse()) sb.AppendLine(string.Format("[{0:HH:mm:ss dd/MM}] [{1}] {2}", entry.Timestamp, entry.Level, entry.Message)); Clipboard.SetText(sb.ToString()); }); AddLog(new BotLogEntry(LogLevel.Info, "Sistema avviato - in attesa di configurazione")); } /// /// Aggiunge un log entry in cima alla lista, sul UI thread (limiti parametrizzati) /// public void AddLog(BotLogEntry logEntry) { Application.Current?.Dispatcher.Invoke(() => { Logs.Insert(0, logEntry); // Limite massimo per il live log (default 10000) int maxLiveLogEntries = 10000; while (Logs.Count > maxLiveLogEntries) Logs.RemoveAt(Logs.Count - 1); }); } } }