using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace CustomDockingPanesFactory
{
	public class RelayCommand<T> : ICommand
	{
		private readonly Action<T> execute;
		private readonly Func<T, bool> canExecute;

		public RelayCommand(Action<T> execute) : this(execute, (p) => true) { }

		public RelayCommand(Action<T> execute, Func<T, bool> canExecute)
		{
			this.execute = execute ?? throw new ArgumentNullException("execute");
			this.canExecute = canExecute ?? throw new ArgumentNullException("canExecute");
		}

		public event EventHandler CanExecuteChanged;


		public void OnCanExecuteChanged()
		{
			CanExecuteChanged?.Invoke(this, EventArgs.Empty);
		}

		public bool CanExecute(T parameter)
		{
			bool v = canExecute.Invoke(parameter);// ?? true;
			return v;
		}

		public void Execute(T parameter)
		{
			execute?.Invoke(parameter);
		}

		#region Explicit ICommand implementations
		bool ICommand.CanExecute(object parameter)
		{
			return CanExecute((T)parameter);
		}

		void ICommand.Execute(object parameter)
		{
			Execute((T)parameter);

		}
		#endregion Explicit ICommand implementations
	}
}
