//===============================================================================
// 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.Security.Permissions;
using System.Text;
using Microsoft.Practices.RecipeFramework;
using System.Runtime.Serialization;
using EnvDTE;
using Microsoft.Practices.Common; 
#endregion

namespace Microsoft.Practices.SoftwareFactories.ServiceFactory.References
{
    /// <summary>
    /// UnBoundRecipeReference that allows to be executed only on a Vs Solution based on property
    /// </summary>
    [Serializable]
    public class SolutionPropertiesReference : UnboundRecipeReference, IAttributesConfigurable
    {
        private const string IsEnabledForPropertiesAttribute = "IsEnabledForProperties";
        private string propertiesName;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:SolutionPropertyReference"/> class.
        /// </summary>
        /// <param name="recipe">The recipe.</param>
        public SolutionPropertiesReference(string recipe)
            : base(recipe)
        {
        }

        /// <summary>
        /// See <see cref="P:Microsoft.Practices.RecipeFramework.IAssetReference.AppliesTo"/>.
        /// </summary>
        /// <value></value>
        public override string AppliesTo
        {
            get { return Properties.Resources.SolutionReference; }
        }

        /// <summary>
        /// Determines whether the reference is enabled for a particular target item,
        /// based on the condition contained in the reference.
        /// </summary>
        /// <param name="target">The <see cref="T:System.Object"/> to check for references.</param>
        /// <returns>
        /// 	<see langword="true"/> if the reference is enabled for the given <paramref name="target"/>.
        /// Otherwise, <see langword="false"/>.
        /// </returns>
        public override bool IsEnabledFor(object target)
        {
            Solution solution = target as Solution;
            bool response = false;
            string[] properties = propertiesName.Split('|');

            if(solution != null)
            {
                for(int i = 0; i < properties.Length; i++)
                {
                    if(solution.Globals.get_VariableExists(properties[i]))
                    {
                        string propertyValue = (string)solution.Globals[properties[i]];
                        bool propertyValueBoolean;

                        if(Boolean.TryParse(propertyValue, out propertyValueBoolean))
                        {
                            response = propertyValueBoolean;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }

            return response;
        }

        #region ISerializable Members

        /// <summary>
        /// Required constructor for deserialization.
        /// </summary>
        protected SolutionPropertiesReference(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            propertiesName = info.GetString(IsEnabledForPropertiesAttribute);
        }

        /// <summary>
        /// Provides a serialization member that derived classes can override to add
        /// data to be stored for serialization.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the reference.</param>
        /// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
        [PermissionSet(SecurityAction.Demand, Name="FullTrust")]
        protected override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
            info.AddValue(IsEnabledForPropertiesAttribute, propertiesName);
        }
        #endregion ISerializable Members

        #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(IsEnabledForPropertiesAttribute))
            {
                propertiesName = attributes[IsEnabledForPropertiesAttribute];
            }
        }
        #endregion
    }
}
