using AlarmInfoServerSim.Services;
using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;

namespace AlarmInfoServerSim.ViewModels;

public partial class LogBarViewModel : ObservableObject
{
    private readonly ILogService _logService;

    [ObservableProperty]
    private ObservableCollection<string> _logs;

    public LogBarViewModel(ILogService logService) 
    {
        _logService= logService;
        _logService.LogReceived += OnLogReceived;

        _logs = [];
    }

    private void OnLogReceived(object? sender, string e)
    {
        App.Current.Dispatcher.BeginInvoke(() =>
        {
            if(Logs.Count>=50)
            {
                Logs.RemoveAt(Logs.Count - 1);
            }
            Logs.Insert(0, e);
        });
    }
}
