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

namespace InvExplorer
{
    /// <summary>
    /// Represents a command that adds an asset to the current portfolio.
    /// The asset should be specified by a parameter of type <see cref="SymbolName"/>.
    /// </summary>
    public class AddAssetCommand : 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 AddAssetCommand(ViewModel model)
        {
            _model = model;
            _model.PortfoliosView.CurrentChanged += (s, e) =>
            {
                OnCanExecuteChanged(EventArgs.Empty);
            };
        }
        /// <summary>
        /// Checks whether the command can be executed in the current model state,
        /// with the given parameter.
        /// </summary>
        /// <param name="parameter"><see cref="SymbolName"/> that represents the asset to add.</param>
        /// <returns>True if the command can be executed with the given parameter.</returns>
        public bool CanExecute(object parameter)
        {
            // need a current portfolio
            if (_model.PortfoliosView == null ||
                _model.PortfoliosView.CurrentItem == null)
            {
                return false;
            }

            // need a symbol to add to the portfolio 
            var sn = parameter as SymbolName;
            if (sn != null)
            {
                // check that symbol is not in the current portfolio
                var pf = _model.PortfoliosView.CurrentItem as Portfolio;
                foreach (PortfolioItem item in pf.Items)
                {
                    if (item.Symbol == sn.Symbol)
                    {
                        return false;
                    }
                }

                // all seems OK
                return true;
            }

            // no symbol? can't add...
            return false;
        }
        /// <summary>
        /// Add a new item with the given symbol to the current portfolio.
        /// </summary>
        /// <param name="parameter"><see cref="SymbolName"/> that defines the asset to add.</param>
        public void Execute(object parameter)
        {
            var sn = parameter as SymbolName;
            if (_model.PortfoliosView != null &&
                _model.PortfoliosView.CurrentItem != null &&
                sn != null)
            {
                // create new portfolio item
                var item = new PortfolioItem();
                item.Symbol = sn.Symbol;
                item.ShowChart = true;

                // add new item to current portfolio and select it
                var pf = _model.PortfoliosView.CurrentItem as Portfolio;
                pf.Items.Add(item);
                pf.ItemsView.MoveCurrentTo(item);

                // 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);
        }
    }
}
