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

namespace Microsoft.Practices.SoftwareFactories.ServiceFactory.Actions.CodeDom.Helpers
{
    /// <summary>
    /// Helper class for mapping the method declaration.
    /// </summary>
    public class MappingMethodDeclaration
    {
        Type fromType;
        Type toType;
        string fromName;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:MappingMethodDeclaration"/> class.
        /// </summary>
        /// <param name="fromType">From type.</param>
        /// <param name="fromName">From name.</param>
        /// <param name="toType">To type.</param>
        public MappingMethodDeclaration(Type fromType, string fromName, Type toType)
        {
            this.fromType = fromType;
            this.fromName = fromName;
            this.toType = toType;
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"></see> is equal to the current <see cref="T:System.Object"></see>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"></see> to compare with the current <see cref="T:System.Object"></see>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"></see> is equal to the current <see cref="T:System.Object"></see>; otherwise, false.
        /// </returns>
        public override bool Equals(object obj)
        {
            MappingMethodDeclaration other = obj as MappingMethodDeclaration;
            if (other == null) return false;

            return fromType.Equals(other.fromType) && toType.Equals(other.toType);
        }

        /// <summary>
        /// Serves as a hash function for a particular type. <see cref="M:System.Object.GetHashCode"></see> is suitable for use in hashing algorithms and data structures like a hash table.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"></see>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Toes the code DOM.
        /// </summary>
        /// <returns></returns>
        public CodeMemberMethod ToCodeDom()
        {
            CodeMemberMethod methodDom = new CodeMemberMethod();
            methodDom.Attributes = MemberAttributes.Public | MemberAttributes.Static; 
            methodDom.Name = GenerateMethodName(fromType, toType);
            methodDom.Parameters.Add(new MethodParameter(fromType, fromName).ToCodeDom());
            methodDom.ReturnType = new CodeTypeReference(toType);

            return methodDom;
        }

        /// <summary>
        /// Generates the name of the method.
        /// </summary>
        /// <param name="fromType">From type.</param>
        /// <param name="toType">To type.</param>
        /// <returns></returns>
        private string GenerateMethodName(Type fromType, Type toType)
        {
            string firstMethodName = fromType.Name;
            string secondMethodName = toType.Name;

            return "Translate" + firstMethodName + "To" + secondMethodName;
        }
    }
}
