using Avalonia;
using Avalonia.Markup.Xaml;
using Avalonia.ReactiveUI;
using detect.gui.Drivers;
using detect.gui.Services;
using detect.gui.Services.Detect;
using detect.gui.ViewModels;
using detect.gui.Views;
using ReactiveUI;
using Splat;

namespace detect.gui;

public class App : Application
{
    public override void Initialize() => AvaloniaXamlLoader.Load(this);
        
    public override void OnFrameworkInitializationCompleted()
    {
        var suspension = new AutoSuspendHelper(ApplicationLifetime!);
        RxApp.SuspensionHost.CreateNewAppState = () => new MainViewModel();
        RxApp.SuspensionHost.SetupDefaultSuspendResume(new NewtonsoftJsonSuspensionDriver("app.state.json"));
        suspension.OnFrameworkInitializationCompleted();
        
        Locator.CurrentMutable.RegisterConstant<IScreen>(RxApp.SuspensionHost.GetAppState<MainViewModel>());
        Locator.CurrentMutable.Register(() => new LoginView(), typeof(IViewFor<LoginViewModel>));
        Locator.CurrentMutable.Register(() => new HomeView(), typeof(IViewFor<HomeViewModel>));
        Locator.CurrentMutable.Register(() => new DetectTaskView(), typeof(IViewFor<DetectTaskViewModel>));
        Locator.CurrentMutable.Register(() => new DeviceView(), typeof(IViewFor<DeviceViewModel>));
        Locator.CurrentMutable.Register(() => new LogView(), typeof(IViewFor<LogViewModel>));
        Locator.CurrentMutable.Register(() => new UserView(), typeof(IViewFor<UserViewModel>));
        
        Locator.CurrentMutable.Register(() => new DetectUserService(), typeof(DetectUserService));
        Locator.CurrentMutable.Register(() => new DetectAuthorityService(), typeof(DetectAuthorityService));
        Locator.CurrentMutable.Register(() => new DetectDeviceService(), typeof(DetectDeviceService));
        Locator.CurrentMutable.Register(() => new DetectTaskService(), typeof(DetectTaskService));
        Locator.CurrentMutable.Register(() => new DetectTaskLogService(), typeof(DetectTaskLogService));
        Locator.CurrentMutable.Register(() => new DetectTaskProgressService(), typeof(DetectTaskProgressService));
        Locator.CurrentMutable.Register(() => new DetectLogService(), typeof(DetectLogService));
        
        Locator.CurrentMutable.Register(() => new DeviceClientService(), typeof(DeviceClientService));
        Locator.Current.GetService<DeviceClientService>();
        
        new MainView { DataContext = Locator.Current.GetService<IScreen>() }.Show();

        base.OnFrameworkInitializationCompleted();
    }
}