using System;
using System.Windows.Input;

namespace TimeScheduler.Common
{
  /// <summary>Proxy-Objekt zur <see cref="ICommand"/>-Schnittstelle</summary>
  class RelayCommand : ICommand
  {
    #region Konstruktion
    private Action execute_;
    private Func<Boolean> canExecute_;

    /// <summary>Konstruktor</summary>
    /// <param name="execute">Auszuführende Funktion</param>
    /// <param name="canExecute">Prüfung zum ausführen der Funktion</param>
    public RelayCommand(Action execute, Func<bool> canExecute = null)
    {
      if (execute == null) throw new ArgumentNullException("execute");

      execute_ = execute;
      canExecute_ = canExecute;
    }
    #endregion

    #region ICommand
    /// <summary>Wirft Ereignis, dass die Oberfläche die Prüfung zum ausführen erneut ruft</summary>
    public void OnCanExecuteChanged()
    {
      var handler = CanExecuteChanged;
      if (handler != null)
        handler(this, EventArgs.Empty);
    }
    public event EventHandler CanExecuteChanged;

    /// <summary>Prüft ob das Kommando ausgeführt werden darf</summary>
    /// <param name="parameter"></param>
    /// <returns></returns>
    public bool CanExecute(object parameter) { return canExecute_ == null ? true : canExecute_(); }

    /// <summary>Wird gerufen um das Kommando ausführen</summary>
    /// <param name="parameter"></param>
    public void Execute(object parameter) { execute_(); }
    #endregion
  }
}
