//===============================================================================
// Microsoft patterns & practices
// Web Service Software Factory
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

#region Using Statements
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.Common;
using EnvDTE;
using Microsoft.Practices.RecipeFramework.Library;
using System.ComponentModel.Design;
using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.RecipeFramework.Extensions.CommonHelpers;
using Howest.Guidance.Extensions.Common.Helpers;
#endregion

namespace Howest.Guidance.Extensions.ValueProviders.VisualStudio
{
    /// <summary>
    /// Returns a project performing a search based on its fullname
    /// </summary>
    [ServiceDependency(typeof(IDictionaryService))]
    public class ConfigurationItemProvider : ValueProvider, IAttributesConfigurable
    {
        /// <summary/>
        public const string ProjectArgumentName = "ProjectArgumentName";

        private string projectArgumentName;

        #region Overrides

        /// <summary>
        /// Uses <see cref="DteHelper.FindItemByName"/> to search for the item specified by the "Name" attributte
        /// </summary>
        /// <param name="currentValue"></param>
        /// <param name="newValue"></param>
        /// <returns></returns>
        /// <seealso cref="ValueProvider.OnBeginRecipe"/>
        /// <seealso cref="DteHelper.FindItemByName"/>
        public override bool OnBeginRecipe(object currentValue, out object newValue)
        {
            IDictionaryService dictservice = (IDictionaryService)
                ServiceHelper.GetService(this, typeof(IDictionaryService));

            Project project = dictservice.GetValue(projectArgumentName) as Project;

            if(project != null)
            {
                if(DteHelperAdv.IsWebProject(project))
                {
                    try
                    {
                        newValue = DteHelperEx.FindItemByName(project.ProjectItems, "web.config", true);
                    }
                    catch
                    {
                        //With Web projects without any subfolder this method throws an exception
                        newValue = DteHelperEx.FindItemByName(project.ProjectItems, "web.config", false);
                    }
                }
                else
                {
                    newValue = DteHelperEx.FindItemByName(project.ProjectItems, "app.config", true);
                }

                if(newValue != null)
                {
                    return true;
                }
            }

            newValue = currentValue;
            return false;
        }

        /// <param name="currentValue"></param>
        /// <param name="newValue"></param>
        /// <returns></returns>
        /// <seealso cref="ValueProvider.OnBeforeActions"/>
        /// <seealso cref="OnBeginRecipe"/>
        public override bool OnBeforeActions(object currentValue, out object newValue)
        {
            return OnBeginRecipe(currentValue, out newValue);
        }

        public override bool OnArgumentChanged(string changedArgumentName, object changedArgumentValue, object currentValue, out object newValue)
        {
            return OnBeginRecipe(currentValue, out newValue);
        }
        #endregion

        #region IAttributesConfigurable Members

        /// <summary>
        /// Configures the component using the dictionary of attributes specified
        /// in the configuration file.
        /// </summary>
        /// <param name="attributes">The attributes in the configuration element.</param>
        public void Configure(System.Collections.Specialized.StringDictionary attributes)
        {
            if (attributes == null)
                throw new ArgumentNullException("attributes");
            
            if (attributes.ContainsKey(ProjectArgumentName))
            {
                projectArgumentName = attributes[ProjectArgumentName];
            }
        }

        #endregion
    }
}
