//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CommunityServer.Controls {

	/// <summary>
	/// Represents a collection of <see cref="CollapseBarItem"/> controls in a <see cref="CollapseBar"/> control.
	/// </summary>
	public sealed class CollapseBarItemCollection : System.Collections.IList {
		
		internal CollapseBarItemCollection( CollapseBar owner ) : base() {
			this.owner = owner;
		}

		private CollapseBar owner;

		#region Strongly Typed IList

		/// <summary>
		/// Adds the given <see cref="CollapseBarItem"/> to the collection.
		/// </summary>
		/// <returns>Returns the index of the item in the collection.</returns>
		public Int32 Add( CollapseBarItem item ) {
			this.AddAt( -1, item );
			return ( this.owner.Controls.Count - 1 ); 
		}

		/// <summary>
		/// Adds the given <see cref="CollapseBarItem"/> to the collection at the given index.
		/// </summary>
		public void AddAt( int index, CollapseBarItem item ) {
			this.owner.Controls.AddAt( index, item );
		}

		/// <summary>
		/// Adds the given array of <see cref="CollapseBarItem"/> controls to the collection.
		/// </summary>
		public void AddRange( CollapseBarItem[] items ) {
			if ( items == null ) {
				throw new ArgumentNullException("items");
			}

			for ( Int32 i = 0; i < items.Length; i++ ) {
				this.Add( items[i] );
			}
 
		}

		/// <summary>
		/// Gets the index of the given item.
		/// </summary>
		public int GetItemIndex( CollapseBarItem item ) {
			if( this.owner.HasControls() ) {
				return this.owner.Controls.IndexOf( item ); 
			}
			return -1;
		}

		/// <summary>
		/// Removes the given <see cref="CollapseBarItem"/> from the collection.
		/// </summary>
		public void Remove( CollapseBarItem item ) {
			this.owner.Controls.Remove( item );
		}

		/// <summary>
		/// Gets the <see cref="CollapseBarItem" /> in the collection at the given index.
		/// </summary>
		public CollapseBarItem this[ int index ] {
			get {
				return this.owner.Controls[ index ] as CollapseBarItem;
			}
		}


		#endregion

		#region IList Members

		/// <summary>
		/// Returns false to indicate that this collection is not read only.
		/// </summary>
		public bool IsReadOnly {
			get {
				return false;
			}
		}

		object IList.this[ int index ] {
			get {
				return this.owner.Controls[ index ];
			}
			set {
				this.RemoveAt( index );
				this.AddAt( index, (CollapseBarItem)value );
			}
		}

		/// <summary>
		/// Removes the <see cref="CollapseBarItem"/> at the given index.
		/// </summary>
		public void RemoveAt( int index ) {
			this.owner.Controls.RemoveAt( index );
		}

		void IList.Insert( int index, object value ) {
			this.owner.Controls.AddAt( index, (CollapseBarItem)value );
		}

		void IList.Remove( object value ) {
			this.owner.Controls.Remove( (CollapseBarItem)value );
		}

		bool IList.Contains( object value ) {
			return this.owner.Controls.Contains( (CollapseBarItem)value );
		}

		/// <summary>
		/// Clears the collection.
		/// </summary>
		public void Clear() {
			if ( this.owner.HasControls() ) {
				foreach( Control control in this.owner.Controls ) {
					this.owner.Controls.Remove( control );
				}
			}
		}

		/// <summary>
		/// Gets the index in the collection of the given <see cref="CollapseBarItem"/>
		/// </summary>
		public int IndexOf( object value ) {
			return this.owner.Controls.IndexOf( (CollapseBarItem)value );
		}

		int IList.Add( object value ) {
			return this.Add( (CollapseBarItem)value );
		}

		bool IList.IsFixedSize {
			get {
				return false;
			}
		}

		#endregion

		#region ICollection Members

		/// <summary>
		/// Returns false to indicate that this collection is not syncronized.
		/// </summary>
		/// <exclude />
		public bool IsSynchronized {
			get {
				return false;
			}
		}

		/// <summary>
		/// Gets the number of <see cref="CollapseBarItem"/> controls contained in the collection.
		/// </summary>
		public int Count {
			get {
				if( this.owner.HasControls() ) {
					return this.owner.Controls.Count;
				}
				return 0;
			}
		}

		/// <summary>
		/// Copies the collection to the given array.
		/// </summary>
		public void CopyTo( Array array, int index ) {
			foreach( Object item in this ) {
				index++;
				array.SetValue( item, index );
			}
		}

		/// <summary>
		/// Returns the current collection object.
		/// </summary>
		/// <exclude />
		public object SyncRoot {
			get {
				return this;
			}
		}

		#endregion

		#region IEnumerable Members

		IEnumerator IEnumerable.GetEnumerator() {
			return this.owner.Controls.GetEnumerator();
		}

		#endregion

	}
}
