//===============================================================================
// 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.Reflection;
using System.Globalization;

namespace Microsoft.Practices.SoftwareFactories.ServiceFactory.Actions.CodeDom.Helpers
{
    /// <summary>
    /// Helper class for the mapping of statements.
    /// </summary>
    public class MappingStatement
    {
        PropertyInfo fromProperty;
        PropertyInfo toProperty;
        string fromObjectName;
        string toObjectName;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:MappingStatement"/> class.
        /// </summary>
        /// <param name="fromProperty">From property.</param>
        /// <param name="fromObjectName">Name of from object.</param>
        /// <param name="toProperty">To property.</param>
        /// <param name="toObjectName">Name of to object.</param>
        public MappingStatement(PropertyInfo fromProperty, string fromObjectName, PropertyInfo toProperty, string toObjectName)
        {
            this.fromProperty = fromProperty;
            this.toProperty = toProperty;
            this.fromObjectName = fromObjectName;
            this.toObjectName = toObjectName;
        }

        /// <summary>
        /// Toes the code DOM.
        /// </summary>
        /// <returns></returns>
        public CodeAssignStatement ToCodeDom()
        {
            if (IsMatching == false) return null;

            CodeVariableReferenceExpression to = new CodeVariableReferenceExpression(toObjectName);
            CodeArgumentReferenceExpression from = new CodeArgumentReferenceExpression(fromObjectName);
            CodePropertyReferenceExpression lhs = new CodePropertyReferenceExpression(to, toProperty.Name);
            CodePropertyReferenceExpression rhs = new CodePropertyReferenceExpression(from, fromProperty.Name);
            CodeAssignStatement assignment = new CodeAssignStatement(lhs, rhs);

            return assignment;
        }

        /// <summary>
        /// Gets a value indicating whether this instance is matching.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is matching; otherwise, <c>false</c>.
        /// </value>
        public bool IsMatching 
        {
            get 
            {
                if (TypesAreAssignable() == false) return false;
                if (fromProperty.CanRead == false) return false;
                if (toProperty.CanWrite == false) return false;

                return true;
            }
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </returns>
        public override string ToString()
        {
            return string.Format(CultureInfo.CurrentCulture, 
                Properties.Resources.MappingStatementToStringFormat, 
                toProperty.PropertyType.FullName, 
                toProperty.Name, 
                fromProperty.PropertyType.FullName, 
                fromProperty.Name);
        }

        /// <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)
        {
            MappingStatement other = obj as MappingStatement;
            if (other == null) return false;

            return toProperty.Equals(other.toProperty) && fromProperty.Equals(other.fromProperty);
        }

        /// <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>
        /// Typeses the are assignable.
        /// </summary>
        /// <returns></returns>
        private bool TypesAreAssignable()
        {
            return toProperty.PropertyType.IsAssignableFrom(fromProperty.PropertyType);
        }
    }
}
