using CommunityToolkit.Mvvm.ComponentModel;
using log4net;
using Microsoft.IdentityModel.Logging;
using Polly;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
using Admin.Core.Tasks;
using Aucma.Core.SheetMetal.Business;
using Aucma.Core.SheetMetal.Models;
using Aucma.Core.SheetMetalTasks;

namespace Aucma.Core.SheetMetal.ViewModels
{
    public class LogPageViewModel : ObservableObject
    {
        private static readonly log4net.ILog log = LogManager.GetLogger(typeof(LogPageViewModel));
        private ObservableCollection<ListBoxDataView> listItems = new ObservableCollection<ListBoxDataView>();
        public LogPageViewModel()
        {
            AucamSheetMetalTaskService.RefreshExecInfoEvent += PrintMessageToListBox;
        }

        /// <summary>
        /// LisBox数据模板
        /// </summary>
        private IEnumerable<ListBoxDataView> logInfoListBox;
        public IEnumerable<ListBoxDataView> LogInfoListBox
        {
            get => logInfoListBox;
            set => SetProperty(ref logInfoListBox, value);
        }

        /// <summary>
        /// listBox绑定日志
        /// </summary>
        /// <param name="message"></param>
        /// <param name="color">Red  or White</param>
        private void PrintMessageToListBox(string message, string color)
        {
            try
            {
                System.Windows.Application.Current.Dispatcher.Invoke((Action)(() =>
                {
                    string content = $"{DateTime.Now.ToString("HH:mm:ss")}==>{message}";
                    ListBoxDataView view = new ListBoxDataView { Content = content, Color = color };
                    //listItems.Add(view);
                    listItems.Insert(0, view);
                    LogInfoListBox = listItems;
                }));
            }
            catch (Exception ex)
            {
                log.Error("日志数据绑定异常", ex);

            }
        }

        public ObservableCollection<ListBoxDataView> Items { get; set; }
    }
  
}