using System;
using System.Windows;
using System.Windows.Input;
using InvExplorer.ServiceReference1;

namespace InvExplorer
{
    /// <summary>
    /// Represents a command that adds a new portfolio to the list.
    /// The name of the new portfolio should be specified by a parameter of type <see cref="string"/>.
    /// </summary>
    public class AddPortfolioCommand : ICommand
    {
        ViewModel _model;

        /// <summary>
        /// Initializes a new instance of an <see cref="AddAssetCommand"/>.
        /// </summary>
        /// <param name="model"><see cref="ViewModel"/> that owns this command.</param>
        public AddPortfolioCommand(ViewModel model)
        {
            _model = model;
        }
        /// <summary>
        /// Checks whether the command can be executed in the current model state,
        /// with the given parameter.
        /// </summary>
        /// <param name="parameter">String containing the name of the portfolio to add.</param>
        /// <returns>True if the command can be executed with the given parameter.</returns>
        public bool CanExecute(object parameter)
        {
            // need a portfolio collection
            if (_model.PortfoliosView == null)
            {
                return false;
            }

            // need a name
            var name = parameter as string;
            if (string.IsNullOrEmpty(name))
            {
                return false;
            }

            // avoid duplicates
            foreach (var pf in _model.Portfolios)
            {
                if (string.Equals(pf.Name, name, StringComparison.OrdinalIgnoreCase))
                {
                    return false;
                }
            }

            // all seems OK
            return true;
        }
        /// <summary>
        /// Add a new portfolio with the given name to the portfolio collection.
        /// </summary>
        /// <param name="parameter"><see cref="SymbolName"/> that defines the asset to add.</param>
        public void Execute(object parameter)
        {
            var name = parameter as string;
            if (_model.PortfoliosView != null &&
                !string.IsNullOrEmpty(name))
            {
                // create new portfolio
                var pf = new Portfolio();
                pf.Name = name;

                // add new item to portfolio collection and select it
                _model.Portfolios.Add(pf);
                _model.PortfoliosView.MoveCurrentTo(pf);

                // command can no longer execute...
                OnCanExecuteChanged(EventArgs.Empty);
            }
        }
        /// <summary>
        /// Event that fires when the command enabled state should change.
        /// </summary>
        public event EventHandler CanExecuteChanged;
        /// <summary>
        /// Raises the <see cref="CanExecuteChanged"/> event.
        /// </summary>
        /// <param name="e"><see cref="EventArgs"/> that contains the event data.</param>
        protected virtual void OnCanExecuteChanged(EventArgs e)
        {
            if (CanExecuteChanged != null)
                CanExecuteChanged(this, e);
        }
    }
}
