using System;
using System.Collections;
using System.ComponentModel;
namespace DevComponents.AdvTree
{
	/// 
	/// Represents collection for Node objects.
	/// 
	public class NodeCollection:CollectionBase 
	{
		#region Private Variables
		private Node m_ParentNode=null;
		private AdvTree m_TreeControl=null;
		private eTreeAction m_SourceAction=eTreeAction.Code;
        private bool _PassiveCollection = false;
		#endregion
		#region Internal Implementation
		public NodeCollection()
		{
		}
        internal eTreeAction SourceAction
        {
            get
            {
                return m_SourceAction;
            }
            set
            {
                m_SourceAction = value;
            }
        }
        internal bool PassiveCollection
        {
            get { return _PassiveCollection; }
            set
            {
                _PassiveCollection = value;
            }
        }
		/// 
		/// Gets or sets the node this collection is associated with.
		/// 
		[Browsable(false),DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public Node ParentNode
		{
			get {return m_ParentNode;}
		}
		/// 
		/// Sets the node collection belongs to.
		/// 
		/// Node that is parent of this collection.
		internal void SetParentNode(Node parent)
		{
			m_ParentNode=parent;
		}
		internal AdvTree TreeControl
		{
			get {return m_TreeControl;}
			set {m_TreeControl=value;}
		}
		/// 
		/// Adds new object to the collection.
		/// 
		/// Object to add.
		/// Index of newly added object.
		public virtual int Add(Node node)
		{
			return Add(node, eTreeAction.Code);
		}
		
		/// 
		/// Adds new object to the collection and provides information about the source of the command
		/// 
		/// Node to add
		/// Source action
		/// 
		public virtual int Add(Node node, eTreeAction action)
		{
			m_SourceAction = action;
			return List.Add(node);
		}
		
		/// 
		/// Adds an array of objects to the collection. 
		/// 
		/// Array of Node objects.
		public virtual void AddRange(Node[] nodes)
		{
			foreach(Node node in nodes)
				this.Add(node);
		}
		
		/// 
		/// Returns reference to the object in collection based on it's index.
		/// 
		public Node this[int index]
		{
			get {return (Node)(List[index]);}
			set {List[index] = value;}
		}
		/// 
		/// Inserts new object into the collection.
		/// 
		/// Position of the object.
		/// Object to insert.
		public virtual void Insert(int index, Node value) 
		{
			List.Insert(index, value);
		}
		
		/// 
		/// Inserts new object into the collection.
		/// 
		/// Position of the object.
		/// Object to insert.
		/// Action that is causing the event
        public virtual void Insert(int index, Node value, eTreeAction action) 
		{
			m_SourceAction = action;
			List.Insert(index, value);
		}
		/// 
		/// Returns index of the object inside of the collection.
		/// 
		/// Reference to the object.
		/// Index of the object.
		public int IndexOf(Node 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(Node value) 
		{
			return List.Contains(value);
		}
		/// 
		/// Removes specified object from the collection.
		/// 
		/// 
		public virtual void Remove(Node value) 
		{
			List.Remove(value);
		}
		
		/// 
		/// Removes specified object from the collection and provides information about source of the command
		/// 
		/// Node to remove
		/// Source action
		public virtual void Remove(Node node, eTreeAction action)
		{
			m_SourceAction = action;
			List.Remove(node);
		}
		
		protected override void OnRemove(int index, object value)
		{
            if (!_PassiveCollection)
            {
                AdvTree tree = GetTreeControl();
                if (tree != null)
                    tree.InvokeBeforeNodeRemove(m_SourceAction, value as Node, m_ParentNode);
            }
			base.OnRemove (index, value);
		}
		protected override void OnRemoveComplete(int index,object value)
		{
			base.OnRemoveComplete(index,value);
            NodeRemoveComplete(index, value);
		}
        private void NodeRemoveComplete(int index, object value)
        {
            if (!_PassiveCollection)
            {
                Node node = value as Node;
                node.SetParent(null);
                node.internalTreeControl = null;
                if (m_ParentNode != null) m_ParentNode.OnChildNodeRemoved(node);
                AdvTree tree = GetTreeControl();
                if (tree != null)
                {
                    //tree.InvokeAfterNodeRemove(m_SourceAction, value as Node, m_ParentNode);
                    tree.NodeRemoved(m_SourceAction, value as Node, m_ParentNode, index);
                }
            }
        }
        protected override void OnSetComplete(int index, object oldValue, object newValue)
        {
            base.OnSetComplete(index, oldValue, newValue);
            NodeRemoveComplete(index, oldValue);
            NodeInsertComplete(newValue);
        }
        protected override void OnSet(int index, object oldValue, object newValue)
        {
            if (!_PassiveCollection)
            {
                AdvTree tree = GetTreeControl();
                if (tree != null)
                    tree.InvokeBeforeNodeRemove(m_SourceAction, oldValue as Node, m_ParentNode);
                if (tree != null)
                    tree.InvokeBeforeNodeInsert(m_SourceAction, newValue as Node, m_ParentNode);
            }
            base.OnSet(index, oldValue, newValue);
        }
		
		protected override void OnInsert(int index, object value)
		{
            if (!_PassiveCollection)
            {
                AdvTree tree = GetTreeControl();
                if (tree != null)
                    tree.InvokeBeforeNodeInsert(m_SourceAction, value as Node, m_ParentNode);
            }
			base.OnInsert (index, value);
		}
		protected override void OnInsertComplete(int index,object value)
		{
			base.OnInsertComplete(index,value);
            NodeInsertComplete(value);
		}
        private void NodeInsertComplete(object value)
        {
            if (!_PassiveCollection)
            {
                Node node = value as Node;
                if (m_ParentNode != null)
                {
                    if (node.Parent != null && node.Parent != m_ParentNode)
                        node.Remove();
                    node.SetParent(m_ParentNode);
                    if (m_ParentNode.NodesColumns.IsSorted)
                    {
                        AdvTree parentTree = m_TreeControl;
                        if (parentTree == null) parentTree = m_ParentNode.TreeControl;
                        if (parentTree != null)
                            parentTree.PushSortRequest(m_ParentNode);
                    }
                }
                else
                {
                    if (node.Parent != null)
                        node.Remove();
                    else
                        node.InvokeOnParentChanged();
                    if (m_TreeControl != null && m_TreeControl.Columns.IsSorted)
                    {
                        m_TreeControl.PushSortRequest();
                    }
                }
                node.internalTreeControl = m_TreeControl;
                if (m_ParentNode != null)
                    m_ParentNode.OnChildNodeInserted(node);
                else
                    node.SizeChanged = true;
                AdvTree tree = GetTreeControl();
                if (tree != null)
                    tree.InvokeAfterNodeInsert(m_SourceAction, value as Node, m_ParentNode);
            }
            m_SourceAction = eTreeAction.Code;
        }
        /// 
		/// Copies collection into the specified array.
		/// 
		/// Array to copy collection to.
		/// Starting index.
		public void CopyTo(Node[] array, int index) 
		{
			List.CopyTo(array, index);
		}
		/// 
		/// Copies contained items to the Node array.
		/// 
		/// Array to copy to.
		[EditorBrowsable(EditorBrowsableState.Never)]
        public void CopyTo(Node[] array)
		{
			List.CopyTo(array,0);
		}
		protected override void OnClear()
		{
            if (!_PassiveCollection)
            {
                foreach (Node node in this.List)
                {
                    node.SetParent(null);
                    node.internalTreeControl = null;
                }
            }
           
			base.OnClear();
		}
        protected override void OnClearComplete()
        {
            if (m_TreeControl != null && !PassiveCollection)
            {
                m_TreeControl.OnNodesCleared();
            }
            else if (m_ParentNode != null && !PassiveCollection)
                m_ParentNode.OnNodesCleared();
            AdvTree tree = GetTreeControl();
            if (tree != null)
            {
                tree.ValidateSelectedNode();
            }
            base.OnClearComplete();
        }
		
		private AdvTree GetTreeControl()
		{
			if(m_TreeControl!=null)
				return m_TreeControl;
			if(m_ParentNode!=null)
				return m_ParentNode.TreeControl;
			return null;
		}
        /// 
        /// Sorts the elements in the entire collection using the IComparable implementation of each element.
        /// 
        public virtual void Sort()
        {
            this.Sort(0, this.Count, Comparer.Default);
        }
        /// 
        /// Sorts the elements in the entire collection using the specified comparer.
        /// 
        /// The IComparer implementation to use when comparing elements.-or- null to use the IComparable implementation of each element.
        public virtual void Sort(IComparer comparer)
        {
            this.Sort(0, this.Count, comparer);
        }
        /// 
        /// Sorts the elements in a range of elements in collection using the specified comparer.
        /// 
        /// 
        /// 
        /// 
        public virtual void Sort(int index, int count, IComparer comparer)
        {
            AdvTree tree = GetTreeControl();
            if (!_PassiveCollection && tree != null)
                tree.BeginUpdate();
            this.InnerList.Sort(index, count, comparer);
            if (tree != null && tree.DeepSort)
            {
                foreach (Node node in this.InnerList)
                {
                    node.Nodes.Sort(0, node.Nodes.Count, comparer);
                }
            }
            if (!_PassiveCollection && tree != null)
                tree.EndUpdate(); 
        }
        
        /// 
        /// Finds the tree nodes with specified key, optionally searching sub-nodes.
        /// 
        /// The name of the tree node to search for.
        /// true to search child nodes of tree nodes; otherwise, false. 
        /// An array of Node objects whose Name property matches the specified key.
        public Node[] Find(string name,	bool searchAllChildren)
        {
            ArrayList list = new ArrayList();
            NodeOperations.FindNodesByName(this, name, searchAllChildren, list);
            Node[] nodes = new Node[list.Count];
            if (list.Count > 0) list.CopyTo(nodes);
            return nodes;
        }
		#endregion
	}
    #region Node Comparer
    public class NodeComparer : IComparer
    {
        private int _ColumnIndex = 0;
        /// 
        /// Creates new instance of NodeComparer class. You can use NodeComparer to sort the nodes by specific column/cell by calling
        /// NodeCollection.Sort method and pass new instance of NodeComparer class. 
        /// 
        public NodeComparer()
        {
        }
        /// 
        /// Creates new instance of NodeComparer class. You can use NodeComparer to sort the nodes by specific column/cell by calling
        /// NodeCollection.Sort method and pass new instance of NodeComparer class. 
        /// 
        /// Column/Cell index to use for sorting.
        public NodeComparer(int columnIndex)
        {
            _ColumnIndex = columnIndex;
        }
        /// 
        /// Gets or sets the Column/Cell index that is used for sorting.
        /// 
        public int ColumnIndex
        {
            get { return _ColumnIndex; }
            set { _ColumnIndex = value; }
        }
        #region IComparer Members
        public virtual int Compare(object x, object y)
        {
            Node nx = (Node)x;
            Node ny = (Node)y;
            if (_ColumnIndex < nx.Cells.Count && _ColumnIndex < ny.Cells.Count)
            {
                if(AdvTreeSettings.UseSortAlphaComparer)
                    return Utilities.CompareAlpha(nx.Cells[_ColumnIndex].Text, ny.Cells[_ColumnIndex].Text);
                else
                    return Utilities.CompareAlphaNumeric(nx.Cells[_ColumnIndex].Text, ny.Cells[_ColumnIndex].Text);
            }
            return 0;
        }
        #endregion
    }
    /// 
    /// Reverse sort nodes.
    /// 
    public class NodeComparerReverse : NodeComparer
    {
        /// 
        /// Creates new instance of NodeComparer class. You can use NodeComparer to sort the nodes by specific column/cell by calling
        /// NodeCollection.Sort method and pass new instance of NodeComparer class. 
        /// 
        /// Column/Cell index to use for sorting.
        public NodeComparerReverse(int columnIndex) : base(columnIndex)
        {
        }
        public override int Compare(object x, object y)
        {
            return base.Compare(y, x);
        }
    }
    /// 
    /// Sort by flat node index.
    /// 
    public class NodeFlatIndexComparer : IComparer
    {
        private AdvTree _TreeControl = null;
        /// 
        /// Creates new instance of NodeComparer class. You can use NodeComparer to sort the nodes by specific column/cell by calling
        /// NodeCollection.Sort method and pass new instance of NodeComparer class. 
        /// 
        public NodeFlatIndexComparer(AdvTree treeControl)
        {
            _TreeControl = treeControl;
        }
        #region IComparer Members
        public virtual int Compare(object x, object y)
        {
            Node nx = (Node)x;
            Node ny = (Node)y;
            if (_TreeControl.GetNodeFlatIndex(nx) < _TreeControl.GetNodeFlatIndex(ny))
                return -1;
            else
                return 1;
        }
        #endregion
    }
    #endregion
}