/**
 * Class that generates a path for auto
 * 
 * @author squirlemaster42
 */
package org.usfirst.frc.team1699.utils.autonomous;

import org.usfirst.frc.team1699.utils.command.Command;

public class AutoPath {
	
	private String path;
	private String[] fileAsString;
	private Object[] cmds;
	
	/**
	 * Constructor
	 * 
	 * @param path
	 * @param numLines
	 * @param cmds
	 */
	public AutoPath(String path, int numLines, Object[] cmds){
		this.path = path;
		this.cmds = cmds;
		fileAsString = AutoUtils.loadFileAsArray(path, numLines);
	}
	
	/**
	 * Runs all lines of file (main)
	 */
	public void runScript(){
		for(int i = 0; i <= fileAsString.length; i++){
			try{
				callCommandFromString(fileAsString[i]);
			}catch(CommandNotFoundException e){
				System.out.println("Your autonomous script has failed because a command does not exist.");
				e.printStackTrace();
				break;
			}
		}
	}
	
	/**
	 * Runs each line
	 * Called from runScript()
	 * 
	 * @param inp
	 * @throws CommandNotFoundException
	 */
	//We may want to split this up into more than one method
	public void callCommandFromString(String inp) throws CommandNotFoundException{
		String[] cmdLine = inp.split(" ");
		String cmdStr;
		double speed = 0;
		int distance = 0;
		Object cmd = null;
		if(Command.getCmdNames().contains(cmdLine[0])){
			cmdStr = cmdLine[0];
			
			for(int i = 0; i <= cmds.length; i++){
				if(cmds[i].equals(cmds)){
					cmd = cmds[i];
				}
			}
		}else{
			throw new CommandNotFoundException();
		}
		
		for(int i = 0; i <= cmdLine.length; i++){
			if(((cmdLine[i].equals("until")) || cmdLine[i].equals("for")) && (i + 1 < cmdLine.length)){
				distance = AutoUtils.parseInt(cmdLine[i]);
				break;
			}
		}
		
		for(int j = 0; j <= cmdLine.length; j++){
			if((cmdLine[j].equals("at")) && (j + 1 < cmdLine.length)){
				speed = AutoUtils.parseDouble(cmdLine[j + 1]);
				break;
			}
		}
		
		((Command) cmd).runAuto(distance, speed);
		
	}
	
	/**
	 * Returns path to file that contains autonomous scripts
	 * 
	 * @return
	 */
	public String getPath() {
		return path;
	}
 
	/**
	 * Returns file as string array
	 * 
	 * @return
	 */
	public String[] getFileAsString() {
		return fileAsString;
	}

}
@@ -1057,67 +1057,8 @@
 */%0D%0A
-%09//We may want to split this up into more than one method%0D%0A
 %09pub
@@ -1178,63 +1178,364 @@
 %0D%0A%09%09
-String cmdStr;%0D%0A%09%09double speed = 0;%0D%0A%09%09int distance = 0
+double speed = getSpeed(cmdLine);%0D%0A%09%09int distance = getDistance(cmdLine);%0D%0A%09%09Object cmd = getCommandObject(cmdLine);%0D%0A%09%09%0D%0A%09%09//Needs to be tested and may not work%0D%0A%09%09((Command) cmd).runAuto(distance, speed);%0D%0A%09%09%0D%0A%09%7D%0D%0A%09%0D%0A%09public Object getCommandObject(String%5B%5D cmdLine) throws CommandNotFoundException%7B%0D%0A%09%09@SuppressWarnings(%22unused%22)%0D%0A%09%09String cmdStr = null
 ;%0D%0A%09
@@ -1555,16 +1555,20 @@
  null;%0D%0A
+%09%09%0D%0A
 %09%09if(Com
@@ -1735,16 +1735,34 @@
 ds%5Bi%5D;%0D%0A
+%09%09%09%09%09return cmd;%0D%0A
 %09%09%09%09%7D%0D%0A%09
@@ -1819,24 +1819,112 @@
 on();%0D%0A%09%09%7D%0D%0A
+%09%09return null;%0D%0A%09%7D%0D%0A%09%0D%0A%09public int getDistance(String%5B%5D cmdLine)%7B%0D%0A%09%09int distance = 0;%0D%0A
 %09%09%0D%0A%09%09for(in
@@ -2108,33 +2108,134 @@
 );%0D%0A%09%09%09%09
-b
 re
-ak;%0D%0A%09%09%09%7D%0D%0A%09%09%7D
+turn distance;%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%09return 0;%0D%0A%09%7D%0D%0A%09%0D%0A%09public double getSpeed(String%5B%5D cmdLine)%7B%0D%0A%09%09double speed = 0.0;
 %0D%0A%09%09%0D%0A%09%09
@@ -2388,37 +2388,44 @@
 e%5Bj + 1%5D);%0D%0A%09%09%09%09
-b
 re
-ak
+turn speed
 ;%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%09
@@ -2432,53 +2432,19 @@
 %0D%0A%09%09
-((Command) cmd).runAuto(distance, speed);%0D%0A%09%09
+return 0.0;
 %0D%0A%09%7D

Split code into multiple methods