using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Notifications;
using AvaloniaSideBarEx.Services;
using Microsoft.Extensions.DependencyInjection;

namespace AvaloniaSideBarEx.Views;

public partial class MainWindow : Window
{
    private WindowNotificationManager? _manager;
    public MainWindow()
    {
        InitializeComponent();
        this.Opened += MainWindow_Opened;
    }
    private void MainWindow_Opened(object? sender, EventArgs e)
    {
        // 1. 取得視窗的頂層容器
        var topLevel = TopLevel.GetTopLevel(this);
        
        if (topLevel != null)
        {
            // 2. 建立通知管理器
            _manager = new WindowNotificationManager(topLevel)
            {
                Position = NotificationPosition.TopRight,
                MaxItems = 3
            };
            // A. 從 App 取得 DI 容器
            var app = (App)Application.Current!;
            
            // B. 從容器中取出 NotificationService
            var service = app.Services?.GetRequiredService<INotificationService>();

            // C. 進行「配對」
            // (我們需要轉型成 NotificationService 才能設定 Manager 屬性)
            if (service is NotificationService concreteService)
            {
                concreteService.Manager = _manager;
            }
        }
    }
}