// 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.Entity
{
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Xml.Linq;
    using Microsoft.Data.Entity.Design.Model.Commands;

    internal class PropertyRef : EFElement
    {
        internal static readonly string ElementName = "PropertyRef";
        internal static readonly string AttributeName = "Name";
        internal static List<EFObject> EmptyList = new List<EFObject>(0);

        private SingleItemBinding<Property> _property;
        private readonly SingleItemBinding<Property>.NameNormalizer _nameNormalizerForPropertyRef;

        internal PropertyRef(EFElement parent, XElement element, SingleItemBinding<Property>.NameNormalizer nameNormalizerForPropertyRef)
            : base(parent, element)
        {
            _nameNormalizerForPropertyRef = nameNormalizerForPropertyRef;
        }

        /// <summary>
        ///     This will return NULL if this is not in an Key
        /// </summary>
        internal Key Key
        {
            get
            {
                var parent = Parent as Key;
                return parent;
            }
        }

        /// <summary>
        ///     This will return NULL if this is not in an ReferentialConstraintRole
        /// </summary>
        internal ReferentialConstraintRole ReferentialConstraintRole
        {
            get
            {
                var parent = Parent as ReferentialConstraintRole;
                return parent;
            }
        }

        internal SingleItemBinding<Property> Name
        {
            get
            {
                if (_property == null)
                {
                    _property = new SingleItemBinding<Property>(
                        this,
                        AttributeName,
                        _nameNormalizerForPropertyRef);
                }
                return _property;
            }
        }

        // 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 Name;
            }
        }

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

        protected override void PreParse()
        {
            ClearEFObject(_property);
            _property = null;

            base.PreParse();
        }

        internal override bool ParseSingleElement(ICollection<XName> unprocessedElements, XElement elem)
        {
            return false;
        }

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

        internal override DeleteEFElementCommand GetDeleteCommand()
        {
            var prc = Parent as PropertyRefContainer;
            if (prc != null)
            {
                return prc.GetDeleteCommandForChild(this);
            }
            Debug.Fail("why is a property ref not a child of a propertyRefContainer?");
            return base.GetDeleteCommand();
        }
    }
}
