using System;
using System.Drawing;
using System.Runtime.Serialization;


using CategoryTheory;
using DiagramUI;


namespace BitmapConsumer
{
	/// <summary>
	/// Consumer of image
	/// </summary>
	public interface IBitmapConsumer
	{
		/// <summary>
		/// Procesess image
		/// </summary>
		void Process();

		/// <summary>
		/// Provider of image
		/// </summary>
		IBitmapProvider Provider
		{
			set;
			get;
		}
	}

	/// <summary>
	/// Provider of image
	/// </summary>
	public interface IBitmapProvider
	{
		/// <summary>
		/// Bitmap
		/// </summary>
		Bitmap Bitmap
		{
			get;
		}
	}

	/// <summary>
	/// Link between bitmap consumer and bitmap provider
	/// </summary>
	[Serializable()]
	public class BitmapConsumerLink : ICategoryArrow, IRemovableObject, ISerializable
	{
		#region Fields
		private object obj;
		private int a = 0;

		private IBitmapConsumer source;

		private IBitmapProvider target;

		#endregion

		#region Constructors
		public BitmapConsumerLink()
		{
		}

		public BitmapConsumerLink(SerializationInfo info, StreamingContext context)
		{
			info.GetValue("A", typeof(int));
		}

		#endregion

		#region ICategoryArrow Members

		public ICategoryObject Source
		{
			get
			{
				return source as ICategoryObject;
			}
			set
			{
				if (!(value is IBitmapConsumer))
				{
					CategoryException.ThrowIllegalSourceException();
				}
				source = value as IBitmapConsumer;
			}
		}

		public ICategoryObject Target
		{
			get
			{
				return target as ICategoryObject;
			}
			set
			{
				if (!(value is IBitmapProvider))
				{
					CategoryException.ThrowIllegalTargetException();
				}
				target = value as IBitmapProvider;
				source.Provider = target;
			}
		}

		public bool IsMonomorphism
		{
			get
			{
				return false;
			}
		}

		public bool IsEpimorphism
		{
			get
			{
				return false;
			}
		}

		public bool IsIsomorphism
		{
			get
			{
				return false;
			}
		}

		public ICategoryArrow Compose(ICategory category, ICategoryArrow next)
		{
			return null;
		}

		#endregion

		#region IAssociatedObject Members

		public object Object
		{
			get
			{
				return obj;
			}
			set
			{
				obj = value;
			}
		}

		#endregion

		#region IRemovableObject Members

		public void RemoveObject()
		{
			source.Provider = null;
		}

		#endregion

		#region ISerializable Members

		public void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			info.AddValue("A", a);
		}

		#endregion

		#region Specific Members

		/// <summary>
		/// Updates consumer
		/// </summary>
		/// <param name="consumer">Consumer</param>
		public static void Update(IBitmapConsumer consumer)
		{
			IBitmapProvider provider = consumer.Provider;
			if (provider is IBitmapConsumer)
			{
				IBitmapConsumer c = provider as IBitmapConsumer;
				Update(c);
			}
			consumer.Process();
		}

		#endregion

	}

}