﻿using System;
using System.Diagnostics;
using System.Windows.Input;

namespace InOutLog.Core
{
    public class RelayCommand : ICommand
    {
        readonly Action<object> _execute = null;
        readonly Predicate<object> _canExecute = null;

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

        }

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

            _execute = execute; 
            _canExecute = canExecute;
        }

        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            _execute(parameter);
            OnCanExecuteChanged();
        }

        public event EventHandler CanExecuteChanged;
        public virtual void OnCanExecuteChanged()
        {
            if (CanExecuteChanged != null)
            {
                CanExecuteChanged(this, new EventArgs());
            }
        }

        
    }
}