using BOB.Singleton;
using DeviceCommand.Device;
using Logger;
using Prism.Mvvm;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;

namespace BOB.ViewModels
{
    public class LogAreaViewModel : BindableBase
    {
        // 日志集合
        private ObservableCollection<LogItem> _logs = new();
        public ObservableCollection<LogItem> Logs
        {
            get => _logs;
            set => SetProperty(ref _logs, value);
        }
        public ICommand ClearLogCommand { get; set; }
        public Devices devices { get; set; }
        public LogAreaViewModel(IContainerProvider containerProvider)
        {
            devices=containerProvider.Resolve<Devices>();
            ClearLogCommand = new DelegateCommand(ClearLog);
            LoggerHelper.Progress = new System.Progress<(string message, string color,int depth)>(
               log =>
               {
                   var brush = (Brush)new BrushConverter().ConvertFromString(log.color);
                   Logs.Add(new LogItem(log.message, brush, log.depth));
               });
        }



        private void ClearLog()
        {
            Logs.Clear();
        }
    }

    // 日志条目类
    public class LogItem
    {
        public string Message { get; set; }
        public Brush Color { get; set; } = Brushes.Black;
        public int Depth { get; set; } 
        public LogItem(string message, Brush color, int depth = 0)
        {
            Message = new string(' ', depth * 20) + message;
            Color = color;
            Depth = depth;
        }
    }
}
