using System.Windows;
using System.Windows.Controls;
using SharepointToolbox.Core.Models;
using SharepointToolbox.Services;
using SharepointToolbox.Views.Dialogs;

namespace SharepointToolbox.Views.Tabs;

public partial class VersionCleanupView : UserControl
{
    private readonly ViewModels.Tabs.VersionCleanupViewModel _viewModel;
    private readonly ISessionManager _sessionManager;
    private readonly IVersionCleanupService _versionService;

    public VersionCleanupView(
        ViewModels.Tabs.VersionCleanupViewModel viewModel,
        ISessionManager sessionManager,
        IVersionCleanupService versionService)
    {
        InitializeComponent();
        _viewModel = viewModel;
        _sessionManager = sessionManager;
        _versionService = versionService;
        DataContext = viewModel;

        viewModel.PickLibrariesAsync = async (siteUrl, preselected) =>
        {
            if (viewModel.CurrentProfile == null) return null;
            var profile = new TenantProfile
            {
                TenantUrl = siteUrl,
                ClientId = viewModel.CurrentProfile.ClientId,
                Name = viewModel.CurrentProfile.Name,
            };
            var ctx = await _sessionManager.GetOrCreateContextAsync(profile, CancellationToken.None);
            var dlg = new LibraryPickerDialog(ctx, _versionService, preselected)
            {
                Owner = Window.GetWindow(this)
            };
            if (dlg.ShowDialog() != true) return null;
            return dlg.SelectedLibraryTitles;
        };

        viewModel.ConfirmAction = msg =>
        {
            var result = MessageBox.Show(
                Window.GetWindow(this), msg,
                Localization.TranslationSource.Instance["versions.tab"],
                MessageBoxButton.OKCancel,
                MessageBoxImage.Warning);
            return result == MessageBoxResult.OK;
        };
    }
}
