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 TransferView : UserControl
{
    private readonly ViewModels.Tabs.TransferViewModel _viewModel;
    private readonly ISessionManager _sessionManager;
    private readonly Func<TenantProfile, SitePickerDialog> _sitePickerFactory;

    public TransferView(
        ViewModels.Tabs.TransferViewModel viewModel,
        ISessionManager sessionManager,
        Func<TenantProfile, SitePickerDialog> sitePickerFactory)
    {
        InitializeComponent();
        _viewModel = viewModel;
        _sessionManager = sessionManager;
        _sitePickerFactory = sitePickerFactory;
        DataContext = viewModel;

        viewModel.ShowConfirmDialog = message =>
        {
            var dlg = new ConfirmBulkOperationDialog(message) { Owner = Window.GetWindow(this) };
            dlg.ShowDialog();
            return dlg.IsConfirmed;
        };
    }

    private async void BrowseSource_Click(object sender, RoutedEventArgs e)
    {
        if (_viewModel.CurrentProfile == null) return;

        // Pick source site — SitePickerDialog returns a list; take the first selection for transfer
        var sitePicker = _sitePickerFactory(_viewModel.CurrentProfile);
        sitePicker.Owner = Window.GetWindow(this);
        if (sitePicker.ShowDialog() != true) return;

        var selectedSite = sitePicker.SelectedUrls.FirstOrDefault();
        if (selectedSite == null) return;

        _viewModel.SourceSiteUrl = selectedSite.Url;

        // Browse library/folder for the selected source site
        var profile = new TenantProfile
        {
            Name = _viewModel.CurrentProfile.Name,
            TenantUrl = selectedSite.Url,
            ClientId = _viewModel.CurrentProfile.ClientId,
        };
        var ctx = await _sessionManager.GetOrCreateContextAsync(profile, CancellationToken.None);
        var folderBrowser = new FolderBrowserDialog(ctx, allowFileSelection: true)
        {
            Owner = Window.GetWindow(this)
        };
        if (folderBrowser.ShowDialog() == true)
        {
            _viewModel.SourceLibrary = folderBrowser.SelectedLibrary;
            _viewModel.SourceFolderPath = folderBrowser.SelectedFolderPath;
            _viewModel.SetSelectedFiles(folderBrowser.SelectedFilePaths);
        }
    }

    private async void BrowseDest_Click(object sender, RoutedEventArgs e)
    {
        if (_viewModel.CurrentProfile == null) return;

        var sitePicker = _sitePickerFactory(_viewModel.CurrentProfile);
        sitePicker.Owner = Window.GetWindow(this);
        if (sitePicker.ShowDialog() != true) return;

        var selectedSite = sitePicker.SelectedUrls.FirstOrDefault();
        if (selectedSite == null) return;

        _viewModel.DestSiteUrl = selectedSite.Url;

        var profile = new TenantProfile
        {
            Name = _viewModel.CurrentProfile.Name,
            TenantUrl = selectedSite.Url,
            ClientId = _viewModel.CurrentProfile.ClientId,
        };
        var ctx = await _sessionManager.GetOrCreateContextAsync(profile, CancellationToken.None);
        var folderBrowser = new FolderBrowserDialog(ctx, allowFolderCreation: true)
        {
            Owner = Window.GetWindow(this)
        };
        if (folderBrowser.ShowDialog() == true)
        {
            _viewModel.DestLibrary = folderBrowser.SelectedLibrary;
            _viewModel.DestFolderPath = folderBrowser.SelectedFolderPath;
        }
    }

    private void ConflictCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (_viewModel != null && ConflictCombo.SelectedIndex >= 0)
        {
            _viewModel.ConflictPolicy = (ConflictPolicy)ConflictCombo.SelectedIndex;
        }
    }
}
