using Avalonia.Controls;
using ClawdDotNet.Desktop.ViewModels;

namespace ClawdDotNet.Desktop.Views;

public partial class TasksPageView : UserControl
{
    public TasksPageView()
    {
        InitializeComponent();
    }

    protected override void OnDataContextChanged(EventArgs e)
    {
        base.OnDataContextChanged(e);
        if (DataContext is TasksPageViewModel vm)
        {
            vm.RequestAddJobDialog += OnRequestAddJobDialog;
            vm.RequestAddServiceDialog += OnRequestAddServiceDialog;
        }
    }

    private async Task<AddJobResultData?> OnRequestAddJobDialog(AddJobViewModel vm)
    {
        var topLevel = TopLevel.GetTopLevel(this);
        if (topLevel is Window window)
        {
            var dialog = new AddJobWindow { DataContext = vm };
            await dialog.ShowDialog(window);
            return vm.Result;
        }
        return null;
    }

    private async Task<AddServiceResultData?> OnRequestAddServiceDialog(AddServiceViewModel vm)
    {
        var topLevel = TopLevel.GetTopLevel(this);
        if (topLevel is Window window)
        {
            var dialog = new AddServiceWindow { DataContext = vm };
            await dialog.ShowDialog(window);
            return vm.Result;
        }
        return null;
    }
}
