//===============================================================================
// 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.RecipeFramework;
using Microsoft.Practices.Common;
using Microsoft.Practices.SoftwareFactories.ServiceFactory.ValueProviders.Helpers;
using System.ComponentModel.Design;
using Microsoft.Practices.ComponentModel;
using EnvDTE;
using Microsoft.Practices.SoftwareFactories.ServiceFactory.Actions.Configuration; 
#endregion

namespace Microsoft.Practices.SoftwareFactories.ServiceFactory.ValueProviders
{
    /// <summary>
    /// Returns a class name
    /// </summary>
    [ServiceDependency(typeof(IDictionaryService))]
    public class ConfigurationProvider : ValueProvider, IAttributesConfigurable
    {
        const string SelectedProjectAttributeName = "SelectedProject";
        string selectedProjectName;

        /// <summary>
        /// Contains code that will be called when recipe execution begins. This is the first method in the lifecycle.
        /// </summary>
        /// <param name="currentValue">An <see cref="T:System.Object"/> that contains the current value of the argument.</param>
        /// <param name="newValue">When this method returns, contains an <see cref="T:System.Object"/> that contains
        /// the new value of the argument, if the returned value
        /// of the method is <see langword="true"/>. Otherwise, it is ignored.</param>
        /// <returns>
        /// 	<see langword="true"/> if the argument value should be replaced with
        /// the value in <paramref name="newValue"/>; otherwise, <see langword="false"/>.
        /// </returns>
        /// <remarks>By default, always returns <see langword="false"/>, unless overriden by a derived class.</remarks>
        public override bool OnBeginRecipe(object currentValue, out object newValue)
        {
            newValue = null;
            if (selectedProjectName == null)
            {
                return false;
            }

            IDictionaryService dictservice = this.GetService<IDictionaryService>(true);
            Project project = dictservice.GetValue(selectedProjectName) as Project;

            if (project == null)
            {
                return false;
            }

            // Only create an App.config. 
            using (GetConfigurationAction configAction = new GetConfigurationAction())
            {
                base.Container.Add(configAction);
                configAction.Project = project;
                configAction.Execute();
                newValue = configAction.Configuration;
            }

            return true;
        }

        /// <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(SelectedProjectAttributeName))
            {
                this.selectedProjectName = attributes[SelectedProjectAttributeName];
            }
        }
    }
}
