using System;
using System.Windows.Input;
using DatabaseEditor.BL.Enums;

namespace DatabaseEditor.Commands
{
    public class SortTableCommand : ICommand
    {
        public delegate void SortAction(string columnName, SortDirection direction);
        private readonly SortAction _action;
        private readonly SortDirection _direction;
        private readonly Func<bool> _canExecute;

        public SortTableCommand(SortAction action, SortDirection direction, Func<bool> canExecute = null)
        {
            _action = action;
            _direction = direction;
            _canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            if (_canExecute == null) return true;
            return _canExecute();
        }

        public void Execute(object parameter)
        {
            var columnName = parameter as string;
            if (columnName != null)
            {
                _action(columnName, _direction);
            }
        }
        
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    }
}
