using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Microsoft.Extensions.DependencyInjection;
using qmonlib.ui.ViewModels;
using qmonlib.ui.Views;
using System;
using System.IO.Abstractions;
using Quadarax.Foundation.Core.Logging;
using Quadarax.Foundation.Core.QMonitor;

namespace qmonlib.ui;

public partial class App : Application
{
    private IServiceProvider _services;
    public override void Initialize()
    {
        AvaloniaXamlLoader.Load(this);
    }

    public override void OnFrameworkInitializationCompleted()
    {
        ConfigureServiceProvider();

        // assign list of available services to the application resources
        this.Resources[typeof(IServiceProvider)] = _services;
     
        if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            desktop.MainWindow = new MainWindow
            {
                DataContext = new MainViewModel()
            };
        }
        else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform)
        {
            singleViewPlatform.MainView = new MainView
            {
                DataContext = new MainViewModel()
            };
        }

        base.OnFrameworkInitializationCompleted();
    }

    private void ConfigureServiceProvider()
    {
        var services = ConfigureServices();
        _services = services.BuildServiceProvider();
    }
        
    private static IServiceCollection ConfigureServices()
    {
        var services = new ServiceCollection();
        //TODO: Put service registrations here

        services.AddSingleton<QMonReceiver>(new QMonReceiver(new FileSystem(),
            QMonReceiverConfiguration.CreateDefault(), false, new ConsoleLogger()));
        
        return services;
    }
}
