using System;
using System.Collections.Generic;
namespace FairyGUI.Utils
{
    /// 
    /// 
    /// 
    public class XMLList
    {
        public List rawList;
        public XMLList()
        {
            rawList = new List();
        }
        public XMLList(List list)
        {
            rawList = list;
        }
        public void Add(XML xml)
        {
            rawList.Add(xml);
        }
        public void Clear()
        {
            rawList.Clear();
        }
        public int Count
        {
            get { return rawList.Count; }
        }
        public XML this[int index]
        {
            get { return rawList[index]; }
        }
        public Enumerator GetEnumerator()
        {
            return new Enumerator(rawList, null);
        }
        public Enumerator GetEnumerator(string selector)
        {
            return new Enumerator(rawList, selector);
        }
        static List _tmpList = new List();
        public XMLList Filter(string selector)
        {
            bool allFit = true;
            _tmpList.Clear();
            int cnt = rawList.Count;
            for (int i = 0; i < cnt; i++)
            {
                XML xml = rawList[i];
                if (xml.name == selector)
                    _tmpList.Add(xml);
                else
                    allFit = false;
            }
            if (allFit)
                return this;
            else
            {
                XMLList ret = new XMLList(_tmpList);
                _tmpList = new List();
                return ret;
            }
        }
        public XML Find(string selector)
        {
            int cnt = rawList.Count;
            for (int i = 0; i < cnt; i++)
            {
                XML xml = rawList[i];
                if (xml.name == selector)
                    return xml;
            }
            return null;
        }
        public void RemoveAll(string selector)
        {
            rawList.RemoveAll(xml => xml.name == selector);
        }
        public struct Enumerator
        {
            List _source;
            string _selector;
            int _index;
            int _total;
            XML _current;
            public Enumerator(List source, string selector)
            {
                _source = source;
                _selector = selector;
                _index = -1;
                if (_source != null)
                    _total = _source.Count;
                else
                    _total = 0;
                _current = null;
            }
            public XML Current
            {
                get { return _current; }
            }
            public bool MoveNext()
            {
                while (++_index < _total)
                {
                    _current = _source[_index];
                    if (_selector == null || _current.name == _selector)
                        return true;
                }
                return false;
            }
            public void Erase()
            {
                _source.RemoveAt(_index);
                _total--;
            }
            public void Reset()
            {
                _index = -1;
            }
        }
    }
}