#if FRAMEWORK20
using System;
using System.Text;
using System.Collections;
namespace DevComponents.Editors
{
    public class VisualItemCollection : CollectionBase
    {
        #region Internal Implementation
        /// Creates new instance of the class.
        public VisualItemCollection() { }
        /// Creates new instance of the class.
        public VisualItemCollection(VisualGroup parent)
        {
            _Parent = parent;
        }
        private VisualGroup _Parent = null;
        internal VisualGroup Parent
        {
            get { return _Parent; }
            set { _Parent = value; }
        }
        /// 
        /// Adds new object to the collection.
        /// 
        /// Object to add.
        /// Index of newly added object.
        public int Add(VisualItem item)
        {
            return List.Add(item);
        }
        /// 
        /// Adds array of new objects to the collection.
        /// 
        /// Array of object to add.
        public void AddRange(VisualItem[] items)
        {
            foreach (VisualItem item in items)
                this.Add(item);
        }
        /// 
        /// Adds array of new objects to the collection.
        /// 
        /// Array of object to add.
        public void AddRange(IList items)
        {
            for (int i = 0; i < items.Count; i++)
            {
                this.Add((VisualItem)items[i]);
            }
        }
        /// 
        /// Returns reference to the object in collection based on it's index.
        /// 
        public VisualItem this[int index]
        {
            get { return (VisualItem)(List[index]); }
            set { List[index] = value; }
        }
        /// 
        /// Inserts new object into the collection.
        /// 
        /// Position of the object.
        /// Object to insert.
        public void Insert(int index, VisualItem value)
        {
            List.Insert(index, value);
        }
        /// 
        /// Returns index of the object inside of the collection.
        /// 
        /// Reference to the object.
        /// Index of the object.
        public int IndexOf(VisualItem value)
        {
            return List.IndexOf(value);
        }
        /// 
        /// Returns whether collection contains specified object.
        /// 
        /// Object to look for.
        /// true if object is part of the collection, otherwise false.
        public bool Contains(VisualItem value)
        {
            return List.Contains(value);
        }
        /// 
        /// Removes specified object from the collection.
        /// 
        /// 
        public void Remove(VisualItem value)
        {
            List.Remove(value);
        }
        protected override void OnRemove(int index, object value)
        {
            if (_Parent != null)
            {
                _Parent.ProcessItemsCollectionChanged(new CollectionChangedInfo(null, new VisualItem[] { (VisualItem)value }, eCollectionChangeType.Removing));
            }
            base.OnRemove(index, value);
        }
        protected override void OnRemoveComplete(int index, object value)
        {
            if (_Parent != null)
            {
                _Parent.ProcessItemsCollectionChanged(new CollectionChangedInfo(null, new VisualItem[] { (VisualItem)value }, eCollectionChangeType.Removed));
            }
            base.OnRemoveComplete(index, value);
        }
        protected override void OnInsert(int index, object value)
        {
            if (_Parent != null)
            {
                _Parent.ProcessItemsCollectionChanged(new CollectionChangedInfo(new VisualItem[] { (VisualItem)value }, null, eCollectionChangeType.Adding));
            }
            base.OnInsert(index, value);
        }
        protected override void OnInsertComplete(int index, object value)
        {
            if (_Parent != null)
            {
                _Parent.ProcessItemsCollectionChanged(new CollectionChangedInfo(new VisualItem[] { (VisualItem)value }, null, eCollectionChangeType.Added));
            }
            base.OnInsertComplete(index, value);
        }
        protected override void OnClear()
        {
            if (_Parent != null)
            {
                _Parent.ProcessItemsCollectionChanged(new CollectionChangedInfo(null, null, eCollectionChangeType.Clearing));
            }
            base.OnClear();
        }
        protected override void OnClearComplete()
        {
            if (_Parent != null)
            {
                _Parent.ProcessItemsCollectionChanged(new CollectionChangedInfo(null, null, eCollectionChangeType.Cleared));
            }
            base.OnClearComplete();
        }
        protected override void OnSet(int index, object oldValue, object newValue)
        {
            if (_Parent != null)
            {
                _Parent.ProcessItemsCollectionChanged(new CollectionChangedInfo(new VisualItem[] { (VisualItem)oldValue },
                    new VisualItem[] { (VisualItem)newValue }, eCollectionChangeType.Removing));
            }
            base.OnSet(index, oldValue, newValue);
        }
        protected override void OnSetComplete(int index, object oldValue, object newValue)
        {
            if (_Parent != null)
            {
                _Parent.ProcessItemsCollectionChanged(new CollectionChangedInfo(new VisualItem[] { (VisualItem)oldValue },
                    new VisualItem[] { (VisualItem)newValue }, eCollectionChangeType.Removed));
            }
            base.OnSetComplete(index, oldValue, newValue);
        }
        /// 
        /// Copies collection into the specified array.
        /// 
        /// Array to copy collection to.
        /// Starting index.
        public void CopyTo(VisualItem[] array, int index)
        {
            List.CopyTo(array, index);
        }
        /// 
        /// Copies contained items to the VisualItem array.
        /// 
        /// Array to copy to.
        internal void CopyTo(VisualItem[] array)
        {
            List.CopyTo(array, 0);
        }
        #endregion
    }
    public class CollectionChangedInfo
    {
        public CollectionChangedInfo(VisualItem[] added, VisualItem[] removed, eCollectionChangeType changeType)
        {
            _Added = added;
            _Removed = removed;
            _ChangeType = changeType;
        }
        private VisualItem[] _Added;
        public VisualItem[] Added
        {
            get { return _Added; }
        }
        private VisualItem[] _Removed;
        public VisualItem[] Removed
        {
            get { return _Removed; }
        }
        private eCollectionChangeType _ChangeType;
        public eCollectionChangeType ChangeType
        {
            get { return _ChangeType; }
        }
    }
    internal class VisualCollectionEnumerator : IEnumerable, IEnumerator
    {
        private IList _ParentCollection = null;
        private int _CurrentIndex = -1;
        private int _Direction = 1, _Start = -1, _End = -1;
        public VisualCollectionEnumerator(VisualItemCollection parentCollection, bool rightToLeft)
        {
            _ParentCollection = parentCollection;
            if (rightToLeft)
            {
                _Direction = -1;
                _Start = parentCollection.Count - 1;
                _End = -1;
            }
            else
            {
                _Start = 0;
                _End = parentCollection.Count;
            }
        }
        #region IEnumerable Members
        public IEnumerator GetEnumerator()
        {
            return this;
        }
        #endregion
        #region IEnumerator Members
        public object Current
        {
            get
            {
                if (_CurrentIndex < 0)
                    throw new InvalidOperationException("IEnumerator Pointer is invalid. Use MoveNext to advance enumerator.");
                return _ParentCollection[_CurrentIndex];
            }
        }
        internal int CurrentIndex
        {
            get
            {
                return _CurrentIndex;
            }
            set
            {
                _CurrentIndex = value;
            }
        }
        public bool MoveNext()
        {
            if (_CurrentIndex == _End && _CurrentIndex >= 0)
                throw new InvalidOperationException("Enumerator cannot advance beyond the boundaries of array. Use Reset to reset enumerator");
            if (_CurrentIndex == -1) _CurrentIndex = _Start - _Direction;
            _CurrentIndex += _Direction;
            if (_CurrentIndex == _End) return false;
            return true;
        }
        public void Reset()
        {
            _CurrentIndex = -1;
        }
        #endregion
    }
}
#endif