// 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.Mapping
{
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Xml.Linq;

    internal class QueryView : EFElement
    {
        internal static readonly string ElementName = "QueryView";
        internal static readonly string AttributeTypeName = "TypeName";

        private EntityTypeMappingTypeNameBinding _entityTypes;

        internal QueryView(EFElement parent, XElement element)
            : base(parent, element)
        {
            Debug.Assert(
                parent is EntitySetMapping || parent is AssociationSetMapping,
                "parent should be an EntitySetMapping or an AssociationSetMapping");
        }

        internal EntityTypeMappingTypeNameBinding TypeName
        {
            get
            {
                if (_entityTypes == null)
                {
                    _entityTypes = new EntityTypeMappingTypeNameBinding(
                        this,
                        EntityTypeMappingTypeNameNormalizer.NameNormalizer);
                }
                return _entityTypes;
            }
        }

        internal string QueryViewCommand
        {
            get { return XElement.Value; }
        }

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

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

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

            ClearEFObject(_entityTypes);
            _entityTypes = null;

            base.PreParse();
        }

        protected override void DoResolve(EFArtifactSet artifactSet)
        {
            TypeName.Rebind();
            // TypeName attribute is optional so its status might be undefined
            if (TypeName.Status == (int)BindingStatus.Known
                || TypeName.Status == (int)BindingStatus.Undefined)
            {
                State = EFElementState.Resolved;
            }
        }
    }
}
