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;

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

        public LogPageViewModel()
        {
            SheetMetalPlanTaskHandle.RefreshExecInfoEvent += PrintMessageToListBox;
        }

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

        /// <summary>
        /// listBox绑定日志
        /// </summary>
        /// <param name="message"></param>
        private void PrintMessageToListBox(string message)
        {
            try
            {
                listItems.Add($"{DateTime.Now.ToString("HH:mm:ss")}==>{message}");

                LogInfoListBox = listItems.OrderByDescending(x => x);
                
            }
            catch (Exception ex)
            {
                log.Error("日志数据绑定异常", ex);

            }
        }
    }
}