using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

using CategoryTheory;

namespace DataPerformer
{
    [Serializable()]
    public class IteratorConsumerLink : ISerializable, ICategoryArrow, IRemovableObject
    {
        #region Fields
        private object obj;

        IIterator iterator;

        IIteratorConsumer consumer;

        #endregion

        #region Ctor

        public IteratorConsumerLink()
        {
        }

        private IteratorConsumerLink(SerializationInfo info, StreamingContext context)
        {
        }

        #endregion

        #region ISerializable Members

        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
        }

        #endregion

        #region ICategoryArrow Members

        ICategoryObject ICategoryArrow.Source
        {
            get
            {
                return consumer as ICategoryObject;
                
            }
            set
            {
               if (!(value is IIteratorConsumer))
               {
                   CategoryException.ThrowIllegalSourceException();
               }
               consumer = value as IIteratorConsumer;
            }
        }

        ICategoryObject ICategoryArrow.Target
        {
            get
            {
                return iterator as ICategoryObject;
            }
            set
            {
                if (!(value is IIterator))
                {
                    CategoryException.ThrowIllegalTargetException();
                }
                iterator = value as IIterator;
                consumer.Add(iterator);
            }
        }

        bool ICategoryArrow.IsMonomorphism
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        bool ICategoryArrow.IsEpimorphism
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        bool ICategoryArrow.IsIsomorphism
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        ICategoryArrow ICategoryArrow.Compose(ICategory category, ICategoryArrow next)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion

        #region IAssociatedObject Members

        object IAssociatedObject.Object
        {
            get
            {
                return obj;
            }
            set
            {
               obj = value;
            }
        }

        #endregion

        #region IRemovableObject Members

        void IRemovableObject.RemoveObject()
        {
            consumer.Remove(iterator);
        }

        #endregion
    }
}
