using GalaSoft.MvvmLight.CommandWpf;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;

namespace Demo1.ViewModel
{
    public class ReaderEditNameViewModel : ViewModelBase
    {
        public ReaderEditNameViewModel()
        {
            CloseCommand = new RelayCommand<Window>(CloseCommandExecute);
            SaveCommand = new RelayCommand<Window>(SaveCommandExecute, CanSaveCommand);

            Messenger.Default.Register<string>(this, (name) =>
            {
                ReaderName = name;
                Messenger.Default.Unregister<string>(this);
            });
        }

        #region Close

        public RelayCommand<Window> CloseCommand { get; private set; }

        private void CloseCommandExecute(Window window)
        {
            window.DialogResult = false;
            window.Close();
        }

        #endregion Close

        #region Save

        public RelayCommand<Window> SaveCommand { get; private set; }

        private void SaveCommandExecute(Window window)
        {
            window.DialogResult = false;
            window.Close();

            Messenger.Default.Send(ReaderName);
        }

        public bool CanSaveCommand(Window window)
        {
            return ReaderName.Trim().Length > 0;
        }

        #endregion Save

        private string _readerName = "";

        public string ReaderName
        {
            get => _readerName;
            set => Set(ref _readerName, value);
        }
    }
}