/* Copyright (C) 2004 - 2005  db4objects Inc.  http://www.db4o.com

This file is part of the db4o open source object database.

db4o is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation and as clarified by db4objects' GPL 
interpretation policy, available at
http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
Suite 350, San Mateo, CA 94403, USA.

db4o is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */
using System;
using com.db4o.ext;

namespace com.db4o.inside.query
{
	/// <summary>
	/// List based objectSet implementation
	/// </summary>
	/// <exclude />
	public class ObjectSetFacade : ExtObjectSet, System.Collections.IList
	{
		public readonly QueryResult _delegate;

        public ObjectSetFacade(QueryResult qr)
		{
            _delegate = qr;
		}

		#region ObjectSet Members
		
		public Object get(int index) {
            return _delegate.get(index);
        }

		public void sort(com.db4o.query.QueryComparator cmp)
		{
			throw new NotImplementedException();
		}

		public long[] getIDs() 
		{
			return _delegate.getIDs();
		}

		public ExtObjectSet ext() 
		{
			return this;
		}

		public bool hasNext() 
		{
			return _delegate.hasNext();
		}

		public Object next() 
		{
			return _delegate.next();
		}

		public void reset() 
		{
			_delegate.reset();
		}

		public int size() 
		{
			return _delegate.size();
		}
    
		private Object streamLock()
		{
			return _delegate.streamLock();
		}
    
		private ObjectContainer objectContainer()
		{
			return _delegate.objectContainer();
		}

		public QueryResult delegate_()
		{
			return _delegate;
		}
		#endregion

		#region IList Members

		public bool IsReadOnly
		{
			get
			{
				return true;
			}
		}

		public object this[int index]
		{
			get
			{
				return _delegate.get(reverseIndex(index));
			}
			set
			{
				throw new NotSupportedException();
			}
		}

		public void RemoveAt(int index)
		{
			throw new NotSupportedException();
		}

		public void Insert(int index, object value)
		{
			throw new NotSupportedException();
		}

		public void Remove(object value)
		{
			throw new NotSupportedException();
		}

		public bool Contains(object value)
		{
			return IndexOf(value) >= 0;
		}

		public void Clear()
		{
			throw new NotSupportedException();
		}

		public int IndexOf(object value)
		{
			lock (streamLock())
			{
                int id = (int)objectContainer().ext().getID(value);
				if(id <= 0)
				{
					return -1;
				}
				return reverseIndex(_delegate.indexOf(id));
			}
		}

		public int Add(object value)
		{
			throw new NotSupportedException();
		}

		public bool IsFixedSize
		{
			get
			{
				return true;
			}
		}

		#endregion

		#region ICollection Members
		public bool IsSynchronized
		{
			get
			{
				return true;
			}
		}

		public int Count
		{
			get
			{
				return size();
			}
		}

        public void CopyTo(Array array, int index)
        {
            lock (streamLock())
            {
                int i = 0;
                int s = _delegate.size();
                while (i < s)
                {
                    array.SetValue(_delegate.get(reverseIndex(i)), index + i);
                    i++;
                }
            }
        }

        public object SyncRoot
		{
			get
			{
				return streamLock();
			}
		}

		#endregion

		#region IEnumerable Members

		class ObjectSetImplEnumerator : System.Collections.IEnumerator
		{
			QueryResult _result;
			int _next = 0;
			
			public ObjectSetImplEnumerator(QueryResult result)
			{
				_result = result;
			}

			public void Reset()
			{
				_next = 0;
			}

			public object Current
			{
				get
				{
					return _result.get(_next-1);
				}
			}

			public bool MoveNext()
			{
				if (_next < _result.size())
				{
					++_next;
					return true;
				}
				return false;
			}
		}

		public System.Collections.IEnumerator GetEnumerator()
		{
			return new ObjectSetImplEnumerator(_delegate);
		}
		#endregion
		
		private int reverseIndex(int idx) 
		{
        	return size()-idx-1;
    	}
	}
}


