using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace EsriMapSamples
{
    public class RelayCommand : ICommand
    {
        //private readonly Action _handler;
        //private bool _isEnabled;

        //public RelayCommand(Action handler)
        //{
        //    _handler = handler;
        //}

        //public bool IsEnabled
        //{
        //    get { return _isEnabled; }
        //    set
        //    {
        //        if (value != _isEnabled)
        //        {
        //            _isEnabled = value;
        //            if (CanExecuteChanged != null)
        //            {
        //                CanExecuteChanged(this, EventArgs.Empty);
        //            }
        //        }
        //    }
        //}

        //public bool CanExecute(object parameter)
        //{
        //    return IsEnabled;
        //}

        //public event EventHandler CanExecuteChanged;

        //public void Execute(object parameter)
        //{
        //    _handler();
        //}

        public event EventHandler CanExecuteChanged;

        Predicate<Object> _canExecute = null;
        Action<Object> _executeAction = null;

        public RelayCommand(Predicate<Object> canExecute, Action<object> executeAction)
        {
            _canExecute = canExecute;
            _executeAction = executeAction;
        }
        public bool CanExecute(object parameter)
        {
            if (_canExecute != null)
                return _canExecute(parameter);
            return true;
        }

        public void UpdateCanExecuteState()
        {
            if (CanExecuteChanged != null)
                CanExecuteChanged(this, new EventArgs());
        }

        public void Execute(object parameter)
        {
            if (_executeAction != null)
                _executeAction(parameter);
            UpdateCanExecuteState();
        }
    }
}
