// 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.Diagnostics.CodeAnalysis;
    using System.Xml.Linq;

    internal class StorageProperty : Property
    {
        private DefaultableValue<string> _typeAttr;

        [SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        internal StorageProperty(StorageEntityType parent, XElement element)
            : base(parent, element)
        {
        }

        internal override string TypeName
        {
            get { return Type.Value; }
        }

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

        protected override DefaultableValue<string> GetStoreGeneratedPatternDefaultableValue()
        {
            return new StoreGeneratedPatternForSsdlDefaultableValue(this);
        }

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

            ClearEFObject(_typeAttr);
            _typeAttr = null;

            base.PreParse();
        }

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