using BOB.ViewModels;
using System.Windows.Controls;

namespace BOB.Views
{
    public partial class LogArea : UserControl
    {
        public LogArea()
        {
            InitializeComponent();

            // 绑定 DataContext 后，订阅日志集合变化
            this.Loaded += (s, e) =>
            {
                if (DataContext is LogAreaViewModel vm)
                {
                    vm.Logs.CollectionChanged += Logs_CollectionChanged;
                }
            };
        }

        private void Logs_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            // 每次新增日志，滚动到最后一条
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                if (this.LogListView.Items.Count > 0)
                {
                    this.LogListView.ScrollIntoView(this.LogListView.Items[^1]);
                }
            }
        }
    }
}
