//===============================================================================
// 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.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;

using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.Common;
using Microsoft.Practices.SoftwareFactories.ServiceFactory.CommonHelpers;
using EnvDTE;
using EnvDTE80;
using Microsoft.Practices.ComponentModel;

namespace Microsoft.Practices.SoftwareFactories.ServiceFactory.ValueProviders
{
    /// <summary>
    /// A value provider that gives a list of the projects in a solution
    /// that have a particular responsibility assigned. The specific
    /// responsibility that we look for is supplied in the configuration.
    /// </summary>
    [ServiceDependency(typeof(DTE))]
    public class ProjectsWithResponsibilityProvider : ValueProvider, IAttributesConfigurable
    {
        private const string ResponsibilityArgument = "ResponsibilityArgument";
        StringDictionary configuration;

        /// <summary>
        /// Called when [begin recipe].
        /// </summary>
        /// <param name="currentValue">The current value.</param>
        /// <param name="newValue">The new value.</param>
        /// <returns></returns>
        public override bool OnBeginRecipe(object currentValue, out object newValue)
        {
            return GetValue(out newValue);
        }

        private bool GetValue(out object newValue )
        {
            ResponsibleProjectFinder finder = new ResponsibleProjectFinder((DTE)(Site.GetService(typeof(DTE))));
            List<Project> projects = finder.FindProjectsWithResponsibility(configuration[ResponsibilityArgument]);
            if (projects.Count > 0)
            {
                newValue = projects[0];
            }
            else
            {
                newValue = null;
            }
            return true;
        }

        #region IAttributesConfigurable Members

        /// <summary>
        /// Configures the specified attributes.
        /// </summary>
        /// <param name="attributes">The attributes.</param>
        public void Configure(System.Collections.Specialized.StringDictionary attributes)
        {
            if (attributes == null)
                throw new ArgumentNullException("attributes");
            
            if (!attributes.ContainsKey(ResponsibilityArgument))
            {
                throw new ArgumentException(Properties.Resources.InvalidArgument, ResponsibilityArgument);
            }
            configuration = attributes;
        }

        #endregion
    }
}
