//===============================================================================
// 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.Text;
using System.CodeDom;
using System.ComponentModel;
using Microsoft.Practices.SoftwareFactories.ServiceFactory.Editors.Grid.Data;

namespace Microsoft.Practices.SoftwareFactories.ServiceFactory.Actions.CodeDom.Helpers
{
    /// <summary/>
    public class ContractAttributeDeclaration
    {
        private Type contractInfoType;
        private object infoObject;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:ContractAttributeDeclaration"/> class.
        /// </summary>
        /// <param name="contractInfoType">Type of the contract info.</param>
        /// <param name="info">The info object.</param>
        public ContractAttributeDeclaration(Type contractInfoType, object info)
        {
            this.contractInfoType = contractInfoType;
            this.infoObject = info;
        }

        /// <summary>
        /// Toes the code DOM.
        /// </summary>
        /// <returns></returns>
        public CodeAttributeDeclaration ToCodeDom()
        {
            CodeAttributeDeclaration attributeDeclaration = new CodeAttributeDeclaration(new CodeTypeReference(contractInfoType.FullName));

            foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(infoObject))
            {
                if (IsPropertyIgnored(property)) continue;

                object propertyValue = property.GetValue(infoObject);
                object defaultValue = GetDefaultPropertyValue(infoObject.GetType(), property);

                if (IsPropertyMandatory(property) || HasPropertyValueChanged(propertyValue, defaultValue))
                {
                    AddParameterToDeclaration(attributeDeclaration, property, propertyValue);
                }
            }

            return attributeDeclaration;
        }

        private static bool IsPropertyIgnored(PropertyDescriptor property)
        {
            return property.Attributes[typeof(IgnoreContractAttributeMemberAttribute)] != null;
        }

        private static bool IsPropertyMandatory(PropertyDescriptor property)
        {
            return property.Attributes[typeof(MandatoryAttributeParameterAttribute)] != null;
        }

        private static void AddParameterToDeclaration(CodeAttributeDeclaration attributeDeclaration, PropertyDescriptor property, object propertyValue)
        {
            CodeAttributeArgument parameter = new CodeAttributeArgument();
            parameter.Name = property.Name;
            if (property.PropertyType.Equals(typeof(Type)))
            {
                parameter.Value = new CodeTypeOfExpression((Type)propertyValue);
            }
			else if (property.PropertyType.IsEnum)
			{
				parameter.Value = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(property.PropertyType), propertyValue.ToString());
			}
			else
			{
				parameter.Value = new CodePrimitiveExpression(propertyValue);
			}

            attributeDeclaration.Arguments.Add(parameter);
        }

        private static object GetDefaultPropertyValue(Type objectType, PropertyDescriptor property)
        {
            object defaultObject = Activator.CreateInstance(objectType);
            object defaultValue = property.GetValue(defaultObject);
            return defaultValue;
        }

        private static bool HasPropertyValueChanged(object propertyValue, object defaultValue)
        {
            return propertyValue.Equals(defaultValue) == false;
        }
    }
}
