//.################################################
//# Copyright © 2006-2010 Evolving Software Ltd   #
//# Author: Vijay Patel                           #
//################################################'

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;

using Evolving.DomainTypes;
using Evolving.TrueView.Domain.Attributes;
using System.Diagnostics;

namespace TestApplicationDomainModel.TestAssociations
{
    [Serializable]
    internal class AssociationRelationships
    {
        private ObjectList<Object> _Many = new ObjectList<Object>();

        public AssociationRelationships()
        {
            _Many.Changing += new NotifyListChangesEventHandler<Object>(_Many_Changing);
            _Many.Changed += new NotifyListChangesEventHandler<Object>(_Many_Changed);
        }

        public Guid ID { get; internal set; }

        /// <summary>
        /// This property uses a OwnsMany relationship
        /// </summary>
        [ListProperty(Relationship = ManyRelationship.OwnsMany)]
        public ObjectList<Object> Composition
        {
            get { return _Many; }
        }

        /// <summary>
        /// This property uses a HasMany relationship
        /// </summary>
        [ListProperty(Relationship = ManyRelationship.HasMany)]
        public ObjectList<Object> Aggregation
        {
            get { return _Many; }
        }

        void _Many_Changing(object sender, IListChangeEventArgs<Object> e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                case NotifyCollectionChangedAction.Replace:
                    if (_Many.Contains(e.Item))
                    {
                        Trace.TraceWarning(string.Concat(e.Item, " already belongs to ", this));
                        e.IsCancelled = true;
                    }
                    break;

                case NotifyCollectionChangedAction.Remove:
                    if (!_Many.Contains(e.Item))
                    {
                        e.IsCancelled = true;
                    }
                    break;
            }
        }

        void _Many_Changed(object sender, IListChangeEventArgs<Object> e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                case NotifyCollectionChangedAction.Replace:
                    break;

                case NotifyCollectionChangedAction.Remove:
                    break;
            }
        }

    }
}
