//===============================================================================
// 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 System.ComponentModel.Design;
using EnvDTE;
using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.Common;
using System.Globalization; 
#endregion

namespace Microsoft.Practices.SoftwareFactories.ServiceFactory.ValueProviders
{
    /// <summary>
    /// Returns a XML Namespace of the format 'http://projectNameArgument/YYYY/MM'.
    /// </summary>
    [ServiceDependency(typeof(IDictionaryService))]
    public class XmlNamespaceProvider : ValueProvider, IAttributesConfigurable
    {
        /// <summary>
        /// The attribute name that specify the project name.
        /// See <see cref="XmlNamespaceProvider"/>.
        /// </summary>
        public const string ProjectNameArgumentAttribute = "ProjectNameArgument";

        string projectNameArgument;

        #region ValueProvider Implementation

        /// <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);
        }

        /// <summary>
        /// Called when [argument changed].
        /// </summary>
        /// <param name="changedArgumentName">Name of the changed argument.</param>
        /// <param name="changedArgumentValue">The changed argument value.</param>
        /// <param name="currentValue">The current value.</param>
        /// <param name="newValue">The new value.</param>
        /// <returns></returns>
        public override bool OnArgumentChanged(string changedArgumentName, object changedArgumentValue, object currentValue, out object newValue)
        {
            return GetValue(out newValue);
        }

        private bool GetValue(out object newValue)
        {
            try
            {
                IDictionaryService dictservice = (IDictionaryService)
                    ServiceHelper.GetService(this, typeof(IDictionaryService));

                string projectName = dictservice.GetValue(this.projectNameArgument) as string;

                if (string.IsNullOrEmpty(projectName))
                {
                    projectName = ProjectNameArgumentAttribute; 
                }

                newValue = string.Format(CultureInfo.InvariantCulture, Properties.Resources.XmlNamespaceFormat,
                    projectName,
                    DateTime.Now.Year,
                    DateTime.Now.ToString("MM", DateTimeFormatInfo.CurrentInfo));

                if (!Uri.IsWellFormedUriString(newValue.ToString(), UriKind.Absolute))
                {
                    throw new UriFormatException(newValue.ToString());
                }

                return true;
            }
            catch
            {
                newValue = string.Empty;
                return false;
            }
        }

        #endregion

        #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(ProjectNameArgumentAttribute))
            {
                projectNameArgument = attributes[ProjectNameArgumentAttribute];
            }    
        }

        #endregion
    }
}
