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

    internal class EnumTypeMember : EFNameableItem
    {
        internal static readonly string ElementName = "Member";

        private DefaultableValue<string> _valueAttr;

        internal EnumTypeMember(EnumType enumType, XElement element)
            : base(enumType, element)
        {
        }

        internal override string EFTypeName
        {
            get { return ElementName; }
        }

        /// <summary>
        ///     Manages the content of the Value attribute
        /// </summary>
        internal DefaultableValue<string> Value
        {
            get
            {
                if (_valueAttr == null)
                {
                    _valueAttr = new DefaultableEnumTypeMemberValue(this);
                }
                return _valueAttr;
            }
        }

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

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

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

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

            if (State != EFElementState.Parsed)
            {
                ClearEFObject(_valueAttr);
                _valueAttr = null;
                base.PreParse();
            }
        }
    }
}
