import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * GDDI2 B-Tree data reader
 */

public class GDI2BTreeDataReader 
{
	private BufferedReader reader;
	
	/**
	 * Sets the reader for the GDI2 B-Tree data file
	 * 
	 * @param dataFile the file where the B-Tree data is given
	 * @throws GDI2BTreeFileFormatException
	 */
	public GDI2BTreeDataReader(String dataFile)
	{
		try
		{
			setReader(new BufferedReader(new FileReader(dataFile)));
		}
		catch (IOException e)
		{
			throw new GDI2BTreeFileFormatException(
					GDI2BTreeFileFormatException.GDI2TypeOfException.FILE, 
					"IOException raised while reading the file.");
		}
	}
	/**
	 * This method reads the next line from the file
	 * 
	 * @return a string or null if the end of the stream is reached
	 * @throws GDI2BTreeFileFormatException
	 */
	public String[] getNextLine()
	{
		// TODO: extend this method with further checks
		String line = null;
		try 
		{
			line = reader.readLine();
			if (line == null)
				return null;
		} 
		catch (IOException e) 
		{
			throw new GDI2BTreeFileFormatException(
					GDI2BTreeFileFormatException.GDI2TypeOfException.FILE, 
					"IOException raised while reading the file.");
		}
		line = line.trim();
		String[] commandLine = line.split(" ");
		// checking if the line has the correct format (<command> <key>)
		if (commandLine.length != 2)
			throw new GDI2BTreeFileFormatException(
					GDI2BTreeFileFormatException.GDI2TypeOfException.FILE, 
					"Wrong command/key line!");
		// checking the commands
		if (!(commandLine[0].equals("add") || commandLine[0].equals("del")))
			throw new GDI2BTreeFileFormatException(
					GDI2BTreeFileFormatException.GDI2TypeOfException.FILE, 
					"Unknown command!");
		return commandLine;
	}
	/**
	 * Reads the order from the file
	 * @return the order for the B-Tree
	 * @throws GDI2BTreeFileFormatException
	 */
	public int readOrder()
	{
		String line = "";
		int order = -1;
		try 
		{
			line = reader.readLine();
			line = line.trim();
			// get the order of the B-Tree
			order = Integer.parseInt(line);
		} 
		catch (IOException ioe) 
		{
			throw new GDI2BTreeFileFormatException(
					GDI2BTreeFileFormatException.GDI2TypeOfException.FILE, 
					"IOException raised while reading file.");
		}
		catch (NumberFormatException nfe)
		{
			throw new GDI2BTreeFileFormatException(
					GDI2BTreeFileFormatException.GDI2TypeOfException.FILE, 
					"NumberFormatException raised while reading order.");
		}
		return order;
	}
	/**
	 * This method closes the data reader
	 * 
	 * @throws GDI2BTreeFileFormatException
	 */
	public void closeGDI2BTreeDataReader()
	{
		try
		{
			getReader().close();
		}
		catch (IOException ioe) 
		{
			throw new GDI2BTreeFileFormatException(
					GDI2BTreeFileFormatException.GDI2TypeOfException.FILE, 
					"IOException raised while closing the file.");
		}
	}
	/**
	 * @param reader the reader to set
	 */
	public void setReader(BufferedReader reader) {
		this.reader = reader;
	}
	/**
	 * @return the reader
	 */
	public BufferedReader getReader() {
		return reader;
	}
}
