/* CS2110 Solution Code
 * Written down to bad style by Raghav Phadke - rkp39
 */
 
/* Errors:

   Instance variables completely uncommented.
   Instance variable 'pattern' has access level modifier
   Instance variable 'myvariable' has incorrect access level modifier
   Instance variable 'myvariable' is poorly named (can demonstrate refactoring in Eclipse here).
   Instance variable 'myvariable' should not be static.
   Constructor has no comments.
   readNextLine() does not close the file.
   Unnecessarily pedantic comments in readNextLine().
   Comments for getCommand() are copied from the interface specification.
   getCommand() has an unused local variable.
   Indentation in getArgument() is poorly formatted.   
*/

package rkp39.tester;

import java.io.*;
import java.util.regex.Pattern;
import static java.util.regex.Pattern.*;

import java.util.NoSuchElementException;
import java.util.Scanner;

import cs2110.assignment1.SpeciesReader;

public class MySpeciesReader implements SpeciesReader {

	static Pattern pattern = Pattern.compile("^\\s*([a-z]+)\\s*=\\s*\"(.*)\"\\s*$", MULTILINE | CASE_INSENSITIVE);
	public static Scanner myvariable;
	
	public MySpeciesReader(String filename) throws FileNotFoundException {
		
		myvariable = new Scanner(new FileInputStream(filename)); 
		myvariable.useDelimiter("\r\n|\n"); 
		myvariable.next(pattern);
	}
	
	public boolean readNextLine() {
		try { // starts try catch block
			myvariable.next(pattern); // calls the next method in myvariable
			return true; // returns true
		} catch(NoSuchElementException e) { // catches exceptions
			return readNextLine(); // returns readNextLine()
		} catch(IllegalStateException e) { // catches exceptions
			return false; // returns false
		}
	}
	
	/* CS2110 data files contain commands.  This returns a String giving the command
	 * associated with the current line, or null if the current line is misformatted or we are at EOF
	 * The command set used in our data files is "Name", "LatinName", "Image" and "DNA"
	 * @return command at the current line, or null if EOF or malformed line
	 */
	
	public String getCommand() {
		int unusedVariable;
		try {
			// First match group is the command
			return myvariable.match().group(1);
		} catch(IllegalStateException e) {
			// myvariable is closed.
			return null;
		}
	}
	
	public String getArgument() {
		try {
		return myvariable.match().group(2);
	} catch(IllegalStateException e) {
		// myvariable is closed
				return null;
		}
	}
}
