using System.Diagnostics;
using EnvDTE;
using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RecipeFramework;

namespace Microsoft.Practices.RecipeFramework.Extensions.ValueProviders.VisualStudio
{
    /// <summary>
    /// ValueProvider that returns the first selected project
    /// in the solution explorer
    /// </summary>
    [ServiceDependency(typeof(DTE))]
    public class FirstSelectedProjectProvider : ValueProvider
    {
        /// <summary>
        /// Sets the newValue to the first selected project
        /// </summary>
        /// <param name="currentValue"></param>
        /// <param name="newValue"></param>
        /// <returns></returns>
        public override bool OnBeginRecipe(object currentValue, out object newValue)
        {
            newValue = currentValue;
            DTE vs = (DTE)GetService(typeof(DTE));
            object[] activeProjects = (object[])vs.ActiveSolutionProjects;
            if (activeProjects != null && activeProjects.Length > 0)
            {
                newValue = activeProjects[0] as Project;
            }
            if (newValue != null)
            {
                return true;
            }
            return false;
        }
    }
}
