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

namespace AutoCat.MVVM
{
    class RelayCommand1<T> : ICommand where T : new()
    {
        private Action<T> action;
        private Func<T, bool> canExecuteAction;

        public RelayCommand1(Action<T> action)
        {
            this.action = action;
            this.canExecuteAction = null;
        }

        public RelayCommand1(Action<T> action, Func<T, bool> canExecute)
        {
            this.action = action;
            this.canExecuteAction = canExecute;
        }

        #region ICommand Members

        public bool CanExecute(object parameter)
        {
            if (canExecuteAction != null)
            {
                T param = parameter != null ? (T)parameter : new T();
                return canExecuteAction(param);
            }
            else
            {
                return true;
            }
        }

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

        public void Execute(object parameter)
        {
            if (parameter != null)
            {
                action((T)parameter);
            }
            else
            {
                action(new T());
            }
        }

        #endregion
    }
}
