//.################################################
//# 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.Objects
{
    /// <summary>
    /// A Product has a many-to-many relationship with Category.
    /// When a Category is added to a Product, a bi-directional relationship should be automatically setup.
    /// A Product is abstract, so it cannot be created. Only sub-classes can be created.
    /// </summary>
    [Serializable]
    public abstract class Product
    {
        public Guid ID { get; internal set; }

        public long Version { get; internal set; }

        public Product()
        {
            this.Categories = new ObjectList<Category>();
            this.Categories.Changing += new NotifyListChangesEventHandler<Category>(Categories_Changing);
            this.Categories.Changed += new NotifyListChangesEventHandler<Category>(Categories_Changed);
        }

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

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

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

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

        /// <summary>
        /// The Categories that this Product belongs to
        /// </summary>
        [ListProperty(CanModifyContents = false)]
        public ObjectList<Category> Categories { get; private set; }

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

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

        /// <summary>
        /// This is from the base class
        /// </summary>
        public string HiddenProperty { get; set; }

        /// <summary>
        /// This has a custom view
        /// </summary>
        public Money Money { get; set; }

        /// <summary>
        /// This should render itself as a button with a Green icon
        /// </summary>
        /// <returns></returns>
        public bool TestMethod1()
        {
            return true;
        }

        /// <summary>
        /// This should render itself as a button with a Grey icon, which only turns green when the argument is provided
        /// </summary>
        /// <returns></returns>
        public bool TestMethod2(Category categoryParameter)
        {
            return true;
        }
    }
}
