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

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

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

        public ComposeFromCaseDialog(ComposeFromCaseViewModel viewModel) : this()
        {
            AttachViewModel(viewModel);
        }

        public void AttachViewModel(ComposeFromCaseViewModel 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();
        }
    }
}
