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

    internal abstract class DesignerInfo : EFElement
    {
        protected DesignerInfoPropertySet _propertySet;

        protected DesignerInfo(EFElement parent, XElement element)
            : base(parent, element)
        {
        }

        protected void AddDesignerProperty(string propertyName, DesignerProperty prop)
        {
            PropertySet.AddDesignerProperty(propertyName, prop);
        }

        internal DesignerInfoPropertySet PropertySet
        {
            get { return _propertySet; }
            set { _propertySet = value; }
        }

        #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 _propertySet;
            }
        }

        protected override void OnChildDeleted(EFContainer efContainer)
        {
            if (efContainer is DesignerInfoPropertySet)
            {
                _propertySet = null;
            }

            base.OnChildDeleted(efContainer);
        }

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

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

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

            ClearEFObject(_propertySet);

            base.PreParse();
        }

        internal override bool ParseSingleElement(ICollection<XName> unprocessedElements, XElement elem)
        {
            if (elem.Name.LocalName == "DesignerInfoPropertySet")
            {
                _propertySet = new DesignerInfoPropertySet(this, elem);
                _propertySet.Parse(unprocessedElements);
            }
            else
            {
                return base.ParseSingleElement(unprocessedElements, elem);
            }
            return true;
        }

        #endregion

        protected DesignerProperty SafeGetDesignerProperty(DesignerProperty designerProperty, string propertyName)
        {
            // Check if the object is already disposed, we should try to get a new value if it is true.
            if (designerProperty != null
                && designerProperty.IsDisposed)
            {
                designerProperty = null;
            }

            if (designerProperty == null
                && PropertySet != null)
            {
                PropertySet.TryGetDesignerProperty(propertyName, out designerProperty);
            }
            return designerProperty;
        }
    }
}
