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

    internal class MappingFragment : EFElement
    {
        internal static readonly string ElementName = "MappingFragment";
        internal static readonly string AttributeStoreEntitySet = "StoreEntitySet";

        private readonly List<ScalarProperty> _scalarProperties = new List<ScalarProperty>();
        private readonly List<ComplexProperty> _complexProperties = new List<ComplexProperty>();
        private readonly List<Condition> _conditions = new List<Condition>();
        private SingleItemBinding<EntitySet> _tableName;

        internal MappingFragment(EFElement parent, XElement element)
            : base(parent, element)
        {
            Debug.Assert(parent is EntityTypeMapping, "parent should be an EntityTypeMapping");
        }

        internal EntityTypeMapping EntityTypeMapping
        {
            get
            {
                var parent = Parent as EntityTypeMapping;
                Debug.Assert(parent != null, "this.Parent should be an EntityTypeMapping");
                return parent;
            }
        }

        /// <summary>
        ///     A bindable reference to the EntitySet in the S-Space, and thus the referenced EntityType
        /// </summary>
        internal SingleItemBinding<EntitySet> StoreEntitySet
        {
            get
            {
                if (_tableName == null)
                {
                    _tableName = new SingleItemBinding<EntitySet>(
                        this,
                        AttributeStoreEntitySet,
                        EntitySetNameNormalizer.NameNormalizer);
                }
                return _tableName;
            }
        }

        internal void AddScalarProperty(ScalarProperty prop)
        {
            _scalarProperties.Add(prop);
        }

        internal IList<ScalarProperty> ScalarProperties()
        {
            return _scalarProperties.AsReadOnly();
        }

        internal void AddComplexProperty(ComplexProperty prop)
        {
            _complexProperties.Add(prop);
        }

        internal IList<ComplexProperty> ComplexProperties()
        {
            return _complexProperties.AsReadOnly();
        }

        internal ScalarProperty FindScalarProperty(Property property, Property column)
        {
            foreach (var sp in _scalarProperties)
            {
                if (sp.Name.Target == property
                    && sp.ColumnName.Target == column)
                {
                    return sp;
                }
            }

            return null;
        }

        /// <summary>
        ///     Returns the list of ScalarProperties including those in ComplexProperty children
        /// </summary>
        /// <returns></returns>
        internal IEnumerable<ScalarProperty> AllScalarProperties()
        {
            foreach (var sp in _scalarProperties)
            {
                yield return sp;
            }

            foreach (var cp in _complexProperties)
            {
                foreach (var sp in cp.AllScalarProperties())
                {
                    yield return sp;
                }
            }
        }

        internal ComplexProperty FindComplexProperty(Property property)
        {
            foreach (var cp in _complexProperties)
            {
                if (cp.Name.Target == property)
                {
                    return cp;
                }
            }

            return null;
        }

        internal void AddCondition(Condition condition)
        {
            _conditions.Add(condition);
        }

        internal IList<Condition> Conditions()
        {
            return _conditions.AsReadOnly();
        }

        internal Condition FindConditionForColumn(Property column)
        {
            foreach (var cond in _conditions)
            {
                if (cond.ColumnName.Target == column)
                {
                    return cond;
                }
            }

            return null;
        }

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

                foreach (var child in ScalarProperties())
                {
                    yield return child;
                }

                foreach (var child2 in ComplexProperties())
                {
                    yield return child2;
                }

                foreach (var child3 in Conditions())
                {
                    yield return child3;
                }

                yield return StoreEntitySet;
            }
        }

        protected override void OnChildDeleted(EFContainer efContainer)
        {
            var sp = efContainer as ScalarProperty;
            if (sp != null)
            {
                _scalarProperties.Remove(sp);
                return;
            }

            var cp = efContainer as ComplexProperty;
            if (cp != null)
            {
                _complexProperties.Remove(cp);
                return;
            }

            var cond = efContainer as Condition;
            if (cond != null)
            {
                _conditions.Remove(cond);
                return;
            }

            base.OnChildDeleted(efContainer);
        }

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

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

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

            ClearEFObject(_tableName);
            _tableName = null;

            ClearEFObjectCollection(_scalarProperties);
            ClearEFObjectCollection(_complexProperties);
            ClearEFObjectCollection(_conditions);

            base.PreParse();
        }

        internal override bool ParseSingleElement(ICollection<XName> unprocessedElements, XElement elem)
        {
            if (elem.Name.LocalName == ScalarProperty.ElementName)
            {
                var prop = new ScalarProperty(this, elem);
                _scalarProperties.Add(prop);
                prop.Parse(unprocessedElements);
            }
            else if (elem.Name.LocalName == ComplexProperty.ElementName)
            {
                var prop = new ComplexProperty(this, elem);
                _complexProperties.Add(prop);
                prop.Parse(unprocessedElements);
            }
            else if (elem.Name.LocalName == Condition.ElementName)
            {
                var cond = new Condition(this, elem);
                _conditions.Add(cond);
                cond.Parse(unprocessedElements);
            }
            else
            {
                return base.ParseSingleElement(unprocessedElements, elem);
            }
            return true;
        }

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

        private string DisplayNameInternal(bool localize)
        {
            if (StoreEntitySet.Status == BindingStatus.Known)
            {
                var etm = Parent as EntityTypeMapping;
                if (etm != null
                    && etm.TypeName.Status == (int)BindingStatus.Known)
                {
                    return string.Format(
                        CultureInfo.CurrentCulture, "{0} <-> {1}",
                        etm.TypeName.TargetsListForDisplay,
                        StoreEntitySet.Target.LocalName.Value);
                }
            }

            string resource;
            if (localize)
            {
                resource = Resources.MappingModel_MappingFragmentDisplayName;
            }
            else
            {
                resource = "{0} (TableName)";
            }

            return string.Format(
                CultureInfo.CurrentCulture,
                resource,
                StoreEntitySet.RefName);
        }

        internal override string DisplayName
        {
            get { return DisplayNameInternal(true); }
        }

        internal override string NonLocalizedDisplayName
        {
            get { return DisplayNameInternal(false); }
        }

        internal override DeleteEFElementCommand GetDeleteCommand()
        {
            DeleteEFElementCommand cmd = new DeleteMappingFragmentCommand(this);
            if (cmd == null)
            {
                // shouldn't happen, just to be safe
                throw new InvalidOperationException();
            }
            return cmd;
        }

        internal override void GetXLinqInsertPosition(EFElement child, out XNode insertAt, out bool insertBefore)
        {
            if (child is ScalarProperty)
            {
                insertAt = FirstChildXElementOrNull();
                insertBefore = true;
            }
            else
            {
                base.GetXLinqInsertPosition(child, out insertAt, out insertBefore);
            }
        }
    }
}
