// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

namespace Microsoft.Data.Entity.Design.Model.Designer
{
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Xml.Linq;
    using Microsoft.Data.Entity.Design.Model.Entity;

    internal class InheritanceConnector : Connector
    {
        internal static readonly string ElementName = "InheritanceConnector";
        internal static readonly string AttributeEntityType = "EntityType";

        private SingleItemBinding<EntityType> _entityTypeBinding;

        internal InheritanceConnector(EFElement parent, XElement element)
            : base(parent, element)
        {
        }

        /// <summary>
        ///     A bindable reference to the EntityType for this connector
        /// </summary>
        internal SingleItemBinding<EntityType> EntityType
        {
            get
            {
                if (_entityTypeBinding == null)
                {
                    _entityTypeBinding = new SingleItemBinding<EntityType>(
                        this,
                        AttributeEntityType,
                        EFNormalizableItemDefaults.DefaultNameNormalizerForDesigner);
                }

                return _entityTypeBinding;
            }
        }

        #region overrides

        // we unfortunately get a warning from the compiler when we use the "base" keyword in "iterator" types generated by using the
        // "yield return" keyword.  By adding this method, I was able to get around this.  Unfortunately, I wasn't able to figure out
        // a way to implement this once and have derived classes share the implementation (since the "base" keyword is resolved at 
        // compile-time and not at runtime.
        private IEnumerable<EFObject> BaseChildren
        {
            get { return base.Children; }
        }

        internal override IEnumerable<EFObject> Children
        {
            get
            {
                foreach (var efobj in BaseChildren)
                {
                    yield return efobj;
                }

                yield return _entityTypeBinding;
            }
        }

#if DEBUG
        internal override ICollection<string> MyAttributeNames()
        {
            var s = base.MyAttributeNames();
            s.Add(AttributeEntityType);
            return s;
        }
#endif

        protected override void PreParse()
        {
            Debug.Assert(State != EFElementState.Parsed, "this object should not already be in the parsed state");

            ClearEFObject(_entityTypeBinding);
            _entityTypeBinding = null;

            base.PreParse();
        }

        protected override void DoResolve(EFArtifactSet artifactSet)
        {
            EntityType.Rebind();
            if (EntityType.Status == BindingStatus.Known)
            {
                State = EFElementState.Resolved;
            }
        }

        /// <summary>
        ///     The model item should return the entity type that the connector points to.
        ///     This should be the derived entity type.
        /// </summary>
        internal override EFObject ModelItem
        {
            get
            {
                if (EntityType.Status == BindingStatus.Known)
                {
                    return EntityType.SafeTarget as ConceptualEntityType;
                }
                return null;
            }
        }

        #endregion
    }
}
