using System;
using System.Windows;
using MarcusLaw.OutlookAddin.UI.ViewModels;

namespace MarcusLaw.OutlookAddin.UI.Dialogs
{
    public partial class SaveAttachmentsDialog : Window
    {
        private SaveAttachmentsViewModel? _viewModel;

        public SaveAttachmentsDialog()
        {
            InitializeComponent();
            Loaded += (_, __) => SearchBox.Focus();
        }

        public SaveAttachmentsDialog(SaveAttachmentsViewModel viewModel) : this()
        {
            AttachViewModel(viewModel);
        }

        public void AttachViewModel(SaveAttachmentsViewModel viewModel)
        {
            if (_viewModel != null) _viewModel.RequestClose -= OnRequestClose;
            _viewModel = viewModel ?? throw new ArgumentNullException(nameof(viewModel));
            DataContext = _viewModel;
            _viewModel.RequestClose += OnRequestClose;
        }

        private void OnRequestClose(object? sender, EventArgs e)
        {
            DialogResult = _viewModel?.DialogResult;
            Close();
        }
    }
}
