/* vim: ts=2 sw=2 expandtab
 * BEGIN FLOCK GPL
 *
 * Copyright Flock Inc. 2005-2009
 * http://flock.com
 *
 * This file may be used under the terms of the
 * GNU General Public License Version 2 or later (the "GPL"),
 * http://www.gnu.org/licenses/gpl.html
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * END FLOCK GPL
 */

#include "flockAggregator.h"
#include "nsArray.h"
#include "flockPriorityQueue.h"

NS_IMPL_ISUPPORTS1(flockAggregator, flockIAggregator)

flockAggregator::flockAggregator()
{
	nsresult rv;
	rv = FLOCK_NewPriorityQueue(getter_AddRefs(mPriorityQueue));
}

flockAggregator::~flockAggregator()
{
}

NS_IMETHODIMP flockAggregator::AggregateFromNode(nsIRDFDataSource *aDataSource, 
                                              nsIRDFResource *aContainer, 
                                              const nsAString & aFilter, 
                                              PRBool groupByStatus, 
                                              PRBool newFirst, 
                                              PRInt64 offset, 
                                              flockIPQEnumerator **_retval)
{ 
	return NS_OK;
}

NS_IMETHODIMP flockAggregator::AggregateStreams(nsIArray *streams, flockIPQEnumerator **_retval)
{
	nsresult rv;
	PRUint32 arrayCount;
	PRUint32 indexCounter;
	PRBool doesHaveMoreElements=PR_FALSE;
	
	streams->GetLength(&arrayCount);
	for (indexCounter = 0; indexCounter < arrayCount; indexCounter++)
	{
		nsCOMPtr<nsISimpleEnumerator> streamEnumerator = do_QueryElementAt(streams, indexCounter);
		rv = streamEnumerator->HasMoreElements(&doesHaveMoreElements);
		if (NS_SUCCEEDED(rv) && doesHaveMoreElements)
		{
			nsCOMPtr<nsISupports> element;
			rv = streamEnumerator->GetNext(getter_AddRefs(element));
			
			mPriorityQueue->Push(element);
		}
	}
	
  flockPriorityQueue* tmpPriorityQueue = NS_STATIC_CAST(flockPriorityQueue*, mPriorityQueue);
	rv = FLOCK_NewPQEnumerator(tmpPriorityQueue, _retval);
	return rv;
}
