//.################################################
//# 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;
using Evolving.TrueView.Domain.Attributes;
using System.Diagnostics;

namespace TestApplicationDomainModel.Objects
{
    /// <summary>
    /// A Category has a many-to-many relationship with Product.
    /// When a Product is added to a Category, a bi-directional relationship should be automatically setup.
    /// </summary>
    [Serializable]
    public class Category : AggregateRootBase
    {
        public Category()
        {
            this.Products = new ObjectList<Product>();
            this.Products.Changing += new NotifyListChangesEventHandler<Product>(Products_Changing);
            this.Products.Changed += new NotifyListChangesEventHandler<Product>(Products_Changed);
        }


        public IAudit Audit { get; protected set; }

        private Money _Money;
        public Money Money
        {
            get { return _Money; }
            set { _Money = value; }
        }


        /// <summary>
        /// The Products that belong to this Category
        /// </summary>
        public ObjectList<Product> Products { get; private set; }

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

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

        void Products_Changed(object sender, IListChangeEventArgs<Product> e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                case NotifyCollectionChangedAction.Replace:
                    if (!e.Item.Categories.Contains(this))
                    {
                        e.Item.Categories.Add(this);
                    }
                    break;

                case NotifyCollectionChangedAction.Remove:
                    e.Item.Categories.Remove(this);
                    break;
            }
        }

        /// <summary>
        /// The name of this category
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// The description for this category
        /// </summary>
        public string Description { get; set; }

        public override string ToString()
        {
            return this.Name;
        }

        public override bool IsValid()
        {
            return true;
        }
    }
}
