using System.ComponentModel.Design;
using System.IO;
using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RecipeFramework;

namespace Microsoft.Practices.RecipeFramework.Extensions.ValueProviders.VisualStudio
{
    /// <summary>
    /// Provides the name of an item been unfolded
    /// </summary>
    [ServiceDependency(typeof(IDictionaryService))]
    public class ItemNameProvider : ValueProvider
    {
        #region Overrides

        /// <summary>Provides the name of an item been unfolded</summary>
        /// <param name="currentValue"></param>
        /// <param name="newValue"></param>
        /// <returns></returns>
        /// <seealso cref="ValueProvider.OnBeginRecipe"/>
        public override bool OnBeginRecipe(object currentValue, out object newValue)
        {
            if (currentValue == null)
            {
                IDictionaryService dictionaryService = (IDictionaryService)GetService(typeof(IDictionaryService));
                string name = dictionaryService.GetValue("rootname") as string;
                if (name != null)
                {
                    name = Path.GetFileNameWithoutExtension(name);
                }
                else
                {
                    name = dictionaryService.GetValue("projectname") as string;
                }
                if (name != null)
                {
                    newValue = name;
                    return true;
                }
                else
                {
                    newValue = currentValue;
                    return false;
                }
            }
            newValue = currentValue;
            return false;
        }

        #endregion
    }
}
