using LogoForceTestApp.Modules.MainModule.Models;
using Prism.Events;
using System;
using System.Windows.Controls;
using System.Windows.Threading;

namespace LogoForceTestApp.Modules.MainModule.Views
{
    /// <summary>
    /// MainPage.xaml 的交互逻辑
    /// </summary>
    public partial class MainPage : UserControl
    {
        private readonly IEventAggregator _eventAggregator;

        public MainPage(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
            _eventAggregator.GetEvent<LogEvent>().Subscribe(WriteLog);

            InitializeComponent();
           
        }

        private void WriteLog(Tuple<LogType, string> log)
        {

            ListBoxMessage?.Dispatcher?.BeginInvoke(DispatcherPriority.Render, new Action(() =>
            {
                if (ListBoxMessage.Items.Count > 100)//24
                {
                    ListBoxMessage.Items.RemoveAt(0);
                }
                var (type, info) = log;
                var listBoxItem = new ListBoxItem
                {
                    Content = info
                };
                if (log.Item1 == LogType.Error)
                    listBoxItem.Foreground = System.Windows.Media.Brushes.Red;
                else if (log.Item1 == LogType.Warning)
                    listBoxItem.Foreground = System.Windows.Media.Brushes.Yellow;
                else
                    listBoxItem.Foreground = System.Windows.Media.Brushes.DarkOliveGreen;
                ListBoxMessage.Items.Add(listBoxItem);
                ListBoxMessage.ScrollIntoView(listBoxItem);
            }));
        }

    }
}