/*******************************************************************************
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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 Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 * 
 * Contact Info:
 * 	Bruce Donald
 * 	Duke University
 * 	Department of Computer Science
 * 	Levine Science Research Center (LSRC)
 * 	Durham
 * 	NC 27708-0129 
 * 	USA
 * 	brd@cs.duke.edu
 * 
 * Copyright (C) 2011 Jeffrey W. Martin and Bruce R. Donald
 * 
 * <signature of Bruce Donald>, April 2011
 * Bruce Donald, Professor of Computer Science
 ******************************************************************************/


package edu.duke.donaldLab.jdshot.grid;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

public class PointIterator implements Iterator<GridPoint>
{
	/**************************
	 *   Definitions
	 **************************/
	
	private enum Mode
	{
		Array,
		List,
		Reader,
		CellIterator
	}
	
	
	/**************************
	 *   Data Members
	 **************************/
	
	private Mode m_mode;
	private PointReader m_reader;
	private GridPoint[] m_array;
	private List<? extends GridPoint> m_list;
	private CellIterator m_cellIterator;
	private GridPointAdapter m_adapter;
	private int m_index;
	private int m_numServed;
	
	
	/**************************
	 *   Constructors
	 **************************/
	
	public PointIterator( GridPoint[] points )
	{
		reset();
		m_mode = Mode.Array;
		m_array = points;
	}
	
	public PointIterator( List<? extends GridPoint> points )
	{
		reset();
		m_mode = Mode.List;
		m_list = points;
	}
	
	public PointIterator( PointReader reader )
	{
		reset();
		m_mode = Mode.Reader;
		m_reader = reader;
	}
	
	public PointIterator( CellIterator cellIterator, GridPointAdapter adapter )
	{
		reset();
		m_mode = Mode.CellIterator;
		m_cellIterator = cellIterator;
		m_adapter = adapter;
	}
	
	
	/**************************
	 *   Static Methods
	 **************************/
	
	public static PointIterator newFromPath( String inPath, Symmetry symmetry )
	throws Exception
	{
		return newFromPath( new File( inPath ), symmetry );
	}
	
	public static PointIterator newFromPath( File inFile, Symmetry symmetry )
	throws Exception
	{
		String inPath = inFile.getAbsolutePath();
		String extension = inPath.substring( inPath.lastIndexOf( '.' ) + 1 );
		if( extension.equals( "cells" ) )
		{
			return new PointIterator( new CellIterator( new CellReader( inFile, symmetry ) ), new GridPointAdapterCenter() );
		}
		else if( extension.equals( "points" ) )
		{
			return new PointIterator( new PointReader( inFile, symmetry ) );
		}
		else
		{
			throw new Exception( "Invalid input file!" );
		}
	}
	
	
	/**************************
	 *   Methods
	 **************************/
	
	public boolean hasNext( )
	{
		switch( m_mode )
		{
			case Array: return m_index < m_array.length;
			case List: return m_index < m_list.size();
			case Reader: return m_reader.hasNext();
			case CellIterator : return m_cellIterator.hasNext();
		}
		
		assert( false ) : "PointIterator: Invalid mode!";

		// just to make the compiler happy
		return false;
	}
	
	public GridPoint next( )
	{
		try
		{
			m_numServed++;
			
			switch( m_mode )
			{
				case Array: return m_array[m_index++];
				case List: return m_list.get( m_index++ );
				case Reader: return m_reader.readPoint();
				case CellIterator: return m_adapter.getPoint( m_cellIterator.next() );
			}
		}
		catch( IOException ex )
		{
			m_numServed--;
			System.err.println( "WARNING: PointIterator.next() caught IOException!" );
			return null;
		}
		
		assert( false ) : "PointIterator: Invalid mode!";
		
		// just to make the compiler happy
		return null;
	}
	
	public void remove( )
	{
		throw new UnsupportedOperationException();
	}
	
	public int getNumServed( )
	{
		return m_numServed;
	}
	
	public long getNumPoints( )
	{
		switch( m_mode )
		{
			case Array: return m_array.length;
			case List: return m_list.size();
			case Reader: return m_reader.getNumPoints();
			case CellIterator: return m_cellIterator.getNumCells();
		}
		
		assert( false ) : "PointIterator: Invalid mode!";
		
		// just to make the compiler happy
		return -1;
	}
	
	
	/**************************
	 *   Functions
	 **************************/
	
	private void reset( )
	{
		m_array = null;
		m_list = null;
		m_reader = null;
		m_cellIterator = null;
		m_adapter = null;
		m_index = 0;
		m_numServed = 0;
	}	
}
