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

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

        public FileToCaseDialog()
        {
            InitializeComponent();
            Loaded += OnLoaded;
        }

        public FileToCaseDialog(FileToCaseViewModel viewModel) : this()
        {
            AttachViewModel(viewModel);
        }

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

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            SearchBox.Focus();
        }

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