using System;
using System.Collections;
using System.Runtime.Serialization;
using System.Xml;
namespace DevComponents.DotNetBar
{
	/// 
	/// Collection of Bar objects.
	/// 
	public class Bars:System.Collections.CollectionBase,IDisposable
	{
		private DotNetBarManager m_Owner;
		internal Bars(DotNetBarManager Owner)
		{
			m_Owner=Owner;
		}
		internal DotNetBarManager Owner
		{
			get
			{
				return m_Owner;
			}
		}
		/// 
		/// Releases the resources used by the Component.
		/// 
		public void Dispose()
		{
			m_Owner=null;
			List.Clear();
		}
		/// 
		/// Adds an Bar to the end of Bars collection.
		/// 
		/// The Bar to be added to the end of the Bars collection.
		public void Add(Bar bar)
		{
			if(List.IndexOf(bar)<0)
				List.Add(bar);
			else
				throw new InvalidOperationException("Bar is already in collection.");
		}
		protected override void OnInsertComplete(int index,object value)
		{
			base.OnInsertComplete(index,value);
			Bar bar=(Bar)value;
			bar.Owner=m_Owner;
			bar.ThemeAware=m_Owner.ThemeAware;
			bar.Style=m_Owner.Style;
			IOwnerBarSupport ownersupport=m_Owner as IOwnerBarSupport;
			if(ownersupport!=null)
				ownersupport.AddShortcutsFromBar(bar);
			bar.OnAddedToBars();
		}
		/// 
		/// Gets the Bar at the specified index.
		/// 
		public Bar this[int Index]
		{
			get
			{
				return List[Index] as Bar;
			}
		}
		/// 
		/// Gets the Bar with the specified name.
		/// 
		public Bar this[string Name]
		{
			get
			{
				foreach(Bar bar in List)
					if(bar.Name==Name)
						return bar;
				return null;
			}
		}
		/// 
		/// Removes specified bar from collection.
		/// 
		/// Bar to remove
		public virtual void Remove(Bar bar) 
		{
			List.Remove(bar);
		}
		protected override void OnRemoveComplete(int index,object value)
		{
			Bar bar=(Bar)value;
			IOwnerBarSupport ownersupport=m_Owner as IOwnerBarSupport;
			if(ownersupport!=null)
				ownersupport.RemoveShortcutsFromBar(bar);
			if(bar.Visible)
				bar.HideBar();
			if(bar.Parent!=null)
			{
				if(bar.Parent is DockSite && ((DockSite)bar.Parent).IsDocumentDock)
					((DockSite)bar.Parent).GetDocumentUIManager().UnDock(bar);
				if(bar.Parent!=null)
					bar.Parent.Controls.Remove(bar);
			}
			base.OnRemoveComplete(index,value);
		}
		protected override void OnClear()
		{
            if (m_Owner != null && m_Owner.GetDesignMode())
                return;
			foreach(Bar bar in List)
			{
                if (bar.GetDesignMode())
                    continue;
				if(bar.AutoHide)
					bar.AutoHide=false;
				if(bar.Visible)
					bar.HideBar();
                if (bar.DockSide == eDockSide.Document && bar.Parent is DockSite && ((DockSite)bar.Parent).GetDocumentUIManager()!=null)
                    ((DockSite)bar.Parent).GetDocumentUIManager().UnDock(bar);
				if(bar!=null && !bar.IsDisposed)
					((IOwnerBarSupport)m_Owner).RemoveShortcutsFromBar(bar);
				if(bar.Parent!=null)
					bar.Parent.Controls.Remove(bar);
                if (!bar.GetDesignMode())
				    bar.Dispose();
			}
		}
		/// 
		/// Determines whether an Bar is in the collection.
		/// 
		/// The Bar to locate in the collection.
		/// true if item is found in the collection; otherwise, false.
		public bool Contains(Bar bar)
		{
			return List.Contains(bar);
		}
		/// 
		/// Determines whether bar with given name is in collection.
		/// 
		/// Name of the bar
		/// True if bar is part of this collection, otherwise false.
		public bool Contains(string name)
		{
			foreach(Bar bar in this.List)
				if(bar.Name==name)
					return true;
			return false;
		}
		/// 
		/// Returns the zero-based index of the Bar in the collection.
		/// 
		/// Bar to locate.
		/// 
		public int IndexOf(Bar bar)
		{
			return List.IndexOf(bar);
		}
		public void CopyTo(System.Array array)
		{
			List.CopyTo(array,0);
		}
		internal void ClearNonDocumentBars()
		{
			ArrayList removeList=new ArrayList(this.Count);
			foreach(Bar bar in this.List)
			{
				if(bar.DockSide!=eDockSide.Document)
                    removeList.Add(bar);
			}
			foreach(Bar bar in removeList)
				this.Remove(bar);
		}
	}
}