using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;

namespace InvExplorer
{
    /// <summary>
    /// Represents a command that loads a portfolio definition from a file.
    /// </summary>
    public class LoadPortfolioCommand : ICommand
    {
        ViewModel _model;

        /// <summary>
        /// Initializes a new instance of a <see cref="LoadPortfolioCommand"/>.
        /// </summary>
        /// <param name="model"><see cref="ViewModel"/> that owns this command.</param>
        public LoadPortfolioCommand(ViewModel model)
        {
            _model = model;
        }
        /// <summary>
        /// Checks whether the command can be executed in the current model state,
        /// with the given parameter.
        /// </summary>
        public bool CanExecute(object parameter)
        {
            return true;
        }
        /// <summary>
        /// Event that fires when the command enabled state should change.
        /// </summary>
        public event EventHandler CanExecuteChanged;
        /// <summary>
        /// Loads a portfolio definition from a file.
        /// </summary>
        public void Execute(object parameter)
        {
            var dlg = new OpenFileDialog();
            dlg.Filter = "Portfolio Definitions (*.pdx)|*.pdx";
            if (dlg.ShowDialog().Value)
            {
                using (var s = dlg.File.OpenRead())
                {
                    _model.Load(s);
                }
            }
        }
    }
}
