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

namespace InvExplorer
{
    /// <summary>
    /// Represents a command that creates a copy of the current portfolio.
    /// </summary>
    public class CopyPortfolioCommand : ICommand
    {
        ViewModel _model;

        /// <summary>
        /// Initializes a new instance of an <see cref="CopyPortfolioCommand"/>.
        /// </summary>
        /// <param name="model"><see cref="ViewModel"/> that owns this command.</param>
        public CopyPortfolioCommand(ViewModel model)
        {
            _model = model;
            _model.PortfoliosView.CollectionChanged += (s, e) => { OnCanExecuteChanged(e); };
            _model.PortfoliosView.CurrentChanged += (s, e) => { OnCanExecuteChanged(e); };
        }

        /// <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)
        {
            return _model.PortfoliosView != null && _model.PortfoliosView.CurrentItem != null;
        }
        /// <summary>
        /// Copies the current portfolio to a new one and adds that to the collection.
        /// </summary>
        public void Execute(object parameter)
        {
            var current = _model.PortfoliosView.CurrentItem as Portfolio;
            if (current != null)
            {
                // new portfolio name
                var name = parameter as string;
                if (string.IsNullOrEmpty(name))
                {
                    name = string.Format("Copy of {0}", current.Name);
                }

                // create new portfolio
                var pf = new Portfolio();
                pf.Name = name;

                // copy content of current portfolio
                foreach (var ci in current.Items)
                {
                    var item = new PortfolioItem();
                    item.Symbol = ci.Symbol;
                    item.PurchasePrice = ci.PurchasePrice;
                    item.Units = ci.Units;
                    item.Flags = ci.Flags;
                    pf.Items.Add(item);
                }

                // add new item to portfolio collection and select it
                _model.Portfolios.Add(pf);
                _model.PortfoliosView.MoveCurrentTo(pf);
            }
        }
        /// <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);
        }
    }
}
