
using System;
using System.Windows.Input;

namespace ReAct_PME.WPF_FrameWork
{
    public class SignalCommand : ICommand
    {
        private event EventHandler<EventArgs<object>>? _executed;
        public event EventHandler<EventArgs<object>>? Executed
        {
            add
            {
                _executed += value;
            }
            remove
            {
                _executed -= value;
            }
        }

        protected void OnExecuted(object parameter)
        {
            if (_executed != null)
            {
                EventArgs<object> e = new EventArgs<object>(parameter);
                _executed(this, e);
            }
        }

        private event EventHandler<CancelEventArgs<object>>? _canExecuteChecking;
        public event EventHandler<CancelEventArgs<object>>? CanExecuteChecking
        {
            add
            {
                _canExecuteChecking += value;
            }
            remove
            {
                _canExecuteChecking -= value;
            }
        }

        protected bool OnCanExecuteChecking(object parameter)
        {
            bool canExecute = true;
            if (_canExecuteChecking != null)
            {
                CancelEventArgs<object> e = new CancelEventArgs<object>(parameter);
                _canExecuteChecking(this, e);
                canExecute = !e.Cancel;
            }
            return canExecute;
        }

        #region ICommand Members
        public bool CanExecute(object? parameter)
        {
            return OnCanExecuteChecking(parameter!);
        }

        public event EventHandler? CanExecuteChanged
        {
            add
            {
                CommandManager.RequerySuggested += value;
            }
            remove
            {
                CommandManager.RequerySuggested -= value;
            }
        }

        public void Execute(object? parameter)
        {
            OnExecuted(parameter!);
        }
        #endregion
    }
}
