using System;
using System.Windows.Input;

namespace Valg.EVA.Skanning.UserInterfaceElements.Commands
{
    
    
    
    
    public class RelayCommand : ICommand
    {
        readonly Action<object> execute;
        readonly Predicate<object> canExecute;

        
        
        
        event EventHandler internalCanExecuteChanged;

        
        
        
        
        public RelayCommand(Action<object> execute) : this(execute, null) { }

        
        
        
        
        
        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");

            this.execute = execute;
            this.canExecute = canExecute;
        }

        
        
        
        public bool CanExecute(object parameter)
        {
            return canExecute == null || canExecute(parameter);
        }

        
        
        
        public event EventHandler CanExecuteChanged
        {
            add
            {
                internalCanExecuteChanged += value;
                CommandManager.RequerySuggested += value;
                CommandManager.InvalidateRequerySuggested();
            }
            remove
            {
                internalCanExecuteChanged -= value;
                CommandManager.RequerySuggested -= value;
            }
        }

        
        
        
        public void Execute(object parameter)
        {
            execute(parameter);
        }

        
        
        
        public void ReevaluateCanExecute()
        {
            if (internalCanExecuteChanged != null)
                internalCanExecuteChanged(this, System.EventArgs.Empty);
        }
    }
}

