using System;
using System.Collections;
using System.Windows.Forms;
using DevComponents.UI.ContentManager;
namespace DevComponents.DotNetBar
{
	/// 
	/// Represents typed collection of BubbleButton objects.
	/// 
	public class BubbleButtonCollection:CollectionBase
	{
		#region Private Variables
		private BubbleBarTab m_Parent=null;
		private bool m_IgnoreEvents=false;
		#endregion
		#region Internal Implementation
		/// 
		/// Copies contained items to the IBlock array.
		/// 
		/// Array to copy to.
		internal void CopyTo(IBlock[] array)
		{
			List.CopyTo(array,0);
		}
		/// 
		/// Creates new instance of the collection.
		/// 
		/// Parent of the collection.
		internal BubbleButtonCollection(BubbleBarTab parent)
		{
			m_Parent=parent;
		}
		/// 
		/// Gets the parent of the collection.
		/// 
		internal BubbleBarTab Parent
		{
			get {return m_Parent;}
		}
		/// 
		/// Adds new item to the collection but it does not raise internal events.
		/// 
		/// New item to add.
		/// Index of newly added item.
		internal int _Add(BubbleButton item)
		{
			m_IgnoreEvents=true;
			int i=0;
			try
			{
				i=List.Add(item);
			}
			finally
			{
				m_IgnoreEvents=false;
			}
			return i;
		}
		/// 
		/// Adds new item to the collection at specified location but it does not raise internal events.
		/// 
		/// New item to add.
		/// Position to add item to.
		internal void _Add(BubbleButton item, int Position)
		{
			m_IgnoreEvents=true;
			try
			{
				List.Insert(Position,item);
			}
			finally
			{
				m_IgnoreEvents=false;
			}
		}
		/// 
		/// Clears the collection but it does not raise internal events.
		/// 
		internal void _Clear()
		{
			m_IgnoreEvents=true;
			try
			{
				List.Clear();
			}
			finally
			{
				m_IgnoreEvents=false;
			}
		}
		/// 
		/// Performs additional custom processes before setting a value in the CollectionBase instance.
		/// 
		/// The zero-based index at which oldValue can be found.
		/// The value to replace with newValue.
		/// The new value of the element at index.
		protected override void OnSet(int index,object oldValue,object newValue)
		{
			if(newValue==null)
				throw new InvalidOperationException("Setting of null values to BubbleButtonCollection is not allowed.");
			BubbleButton item=newValue as BubbleButton;
			if(item.Parent!=null)
			{
				item.Parent.Buttons.Remove(item);
			}
			base.OnSet(index,oldValue,newValue);
		}
		/// 
		/// Performs additional custom processes after setting a value in the CollectionBase instance.
		/// 
		/// The zero-based index at which oldValue can be found.
		/// The value to replace with newValue.
		/// The new value of the element at index. 
		protected override void OnSetComplete(int index,object oldValue,object newValue)
		{
			if(!m_IgnoreEvents)
			{
				BubbleButton item=newValue as BubbleButton;
				item.SetParentCollection(this);
				m_Parent.OnButtonInserted(item);
			}
			base.OnSetComplete(index,oldValue,newValue);
		}
		/// 
		/// Performs additional custom processes before inserting a new element into the CollectionBase instance.
		/// 
		/// The zero-based index at which to insert value. 
		/// The new value of the element at index.
		protected override void OnInsert(int index,object value)
		{
			BubbleButton item=value as BubbleButton;
			if(item.Parent!=null && item.Parent!=this.Parent)
			{
				item.Parent.Buttons.Remove(item);
			}
			base.OnInsert(index,value);
		}
		/// 
		/// Performs additional custom processes after inserting a new element into the CollectionBase instance.
		/// 
		/// The zero-based index at which to insert value.
		/// The new value of the element at index.
		protected override void OnInsertComplete(int index,object value)
		{
			if(!m_IgnoreEvents)
			{
				BubbleButton item=value as BubbleButton;
				item.SetParentCollection(this);
				m_Parent.OnButtonInserted(item);
			}
			base.OnInsertComplete(index,value);
		}
		/// 
		/// Performs additional custom processes when removing an element from the CollectionBase instance.
		/// 
		/// The zero-based index at which value can be found.
		/// The value of the element to remove from index.
		protected override void OnRemove(int index,object value)
		{
//			if(!m_IgnoreEvents)
//			{
//				BubbleButton item=value as BubbleButton;				
//			}
			base.OnRemove(index,value);
		}
		/// 
		/// Performs additional custom processes after removing an element from the CollectionBase instance.
		/// 
		/// The zero-based index at which value can be found. 
		/// The value of the element to remove from index.
		protected override void OnRemoveComplete(int index,object value)
		{
			if(!m_IgnoreEvents)
			{
				BubbleButton item=value as BubbleButton;
				item.SetParentCollection(null);
				m_Parent.OnButtonRemoved(item);
			}
			base.OnRemoveComplete(index,value);
		}
		/// 
		/// Removes an item without raising internal events.
		/// 
		/// Item to remove.
		internal void _Remove(BubbleButton item)
		{
			m_IgnoreEvents=true;
			try{List.Remove(item);}
			finally{m_IgnoreEvents=false;}
		}
		/// 
		/// Performs additional custom processes when clearing the contents of the CollectionBase instance.
		/// 
		protected override void OnClear()
		{
			if(!m_IgnoreEvents)
			{
//				if(List.Count>0)
//				{
//					foreach(BubbleButton objSub in this)
//					{
//						if(owner!=null)
//							owner.RemoveShortcutsFromItem(objSub);
//					}
//				}
				if(m_Parent!=null)
				{
					m_Parent.OnButtonsCollectionClear();
				}
			}
			base.OnClear();
		}
		/// 
		/// Copies the collection to the ArrayList object.
		/// 
		/// Target ArrayList.
		public void CopyTo(ArrayList list)
		{
			if(list==null)
				return;
			foreach(BubbleButton item in this)
				list.Add(item);
		}
		#endregion
		#region Public Interface
		/// 
		/// Adds new item to the collection.
		/// 
		/// New item to add.
		/// Index of newly added item.
		public virtual int Add(BubbleButton item)
		{
			return Add(item,-1);
		}
		/// 
		/// Adds new item to the collection at specified location.
		/// 
		/// New item to add.
		/// Position to insert item at. Position of -1 will append the item to the end of the collection.
		/// Index of the newly added item.
		public virtual int Add(BubbleButton item, int Position)
		{
			int iRet=Position;
			
			if(Position>=0)
				List.Insert(Position,item);
			else
				iRet=List.Add(item);
			return iRet;
		}
		/// 
		/// Accesses items inside of the collection based on the index.
		/// 
		public virtual BubbleButton this[int index]
		{
			get {return (BubbleButton)(List[index]);}
			set {List[index] = value;}
		}
		/// 
		/// Accesses items inside of the collection based on the name.
		/// 
		public  virtual BubbleButton this[string name]
		{
			get {return (BubbleButton)(List[this.IndexOf(name)]);}
			set {List[this.IndexOf(name)] = value;}
		}
		/// 
		/// Inserts new item at the specified position.
		/// 
		/// Position to insert item at.
		/// Item to insert.
		public virtual void Insert(int index, BubbleButton item) 
		{
			this.Add(item,index);
		}
		/// 
		/// Returns index of an item.
		/// 
		/// Item to return index for.
		/// Item at the specified position.
		public virtual int IndexOf(BubbleButton value) 
		{
			return List.IndexOf(value);
		}
		/// 
		/// Returns index of an item with given the item's name.
		/// 
		/// Name of the item.
		/// Index of the Item with the specified name or -1 if item is not found.
		public virtual int IndexOf(string name)
		{
			int i=-1;
			foreach(BubbleButton item in List)
			{
				i++;
				if(item.Name==name)
					return i;
			}
			return -1;
		}
		/// 
		/// Returns true if given item is contained by this collection.
		/// 
		/// Item to test.
		/// True if item is part of this collection otherwise false.
		public virtual bool Contains(BubbleButton value) 
		{
			return List.Contains(value);
		}
		/// 
		/// Returns true if item with given name is part of this collection.
		/// 
		/// Item name.
		/// True if item is part of this collection otherwise false.
		public virtual bool Contains(string name)
		{
			foreach(BubbleButton item in List)
			{
				if(item.Name==name)
					return true;
			}
			return false;
		}
		/// 
		/// Removes an item from the collection.
		/// 
		/// Item to remove.
		public virtual void Remove(BubbleButton item) 
		{
			List.Remove(item);
		}
		/// 
		/// Removes an item from collection at specified index.
		/// 
		/// Index of the item to remove.
		public void Remove(int index)
		{
			this.Remove((BubbleButton)List[index]);
		}
		/// 
		/// Removes item from the collection with specified name.
		/// 
		/// Name of the item to remove.
		public virtual void Remove(string name)
		{
			this.Remove(this[name]);
		}
		/// 
		/// Adds array of the items to the collection.
		/// 
		/// Array of items to add.
		public virtual void AddRange(BubbleButton[] items)
		{
			foreach(BubbleButton item in items)
			{
				this.Add(item);
			}
		}
		/// 
		/// Copy the collection to the array.
		/// 
		/// Array to copy collection to.
		/// The zero-based relative index in array at which copying begins.
		public virtual void CopyTo(BubbleButton[] array, int index) 
		{
			List.CopyTo(array, index);
		}
		#endregion
	}
}