/* Written by willie
 * Add in your own customer user interface here.
 * 
 */
package edu.wpi.cetask.guide;

import java.util.Iterator;

import edu.wpi.cetask.Plan;
import edu.wpi.cetask.TaskEngine;


public class CustomGuide extends Guide {
	
	final protected CustomUI myUI;

	   public static void main (String[] args) {
		   VERSION += " - MyUI";
		   CustomGuide guide;
		   if ( args.length > 0 && args[0].equals("-NoSpeech") ) guide = new CustomGuide(args.length > 1 ? args[1] : null, false);
		   else guide = new CustomGuide(args.length > 0 ? args[0] : null, true);
		   guide.loop();
	   }

	   public CustomGuide(String log, boolean hasSpeech) {
		   super(log);

		   if (hasSpeech) {
			   SpeechUI speech = new SpeechUI(new Sphinx4(), new FreeTTS());
			   uiList.add(speech);
		   }

		   // instantiate your UI, it must be of type ITaskUI
		   myUI = new CustomUI();
		   
		   // add simulator as an interface
		   uiList.add(1, myUI);

		   // other setup
		   doProposal = false;

	   }

	   public void focus (String args) throws Throwable {
		   Plan focus = Guide.guide.engine.getFocus();
		   Plan newFocus = null;
			if (focus != null) {
				if ( focus.toString().equals(args) ) {
					println("Already in focus");
				}
				else {
					boolean foundPlan = false;
			      for (Iterator<Plan> i = Guide.guide.engine.tops(); i.hasNext();) {
			          newFocus = i.next();
			          if ( newFocus.toString().equals(args) ) {
			        	  foundPlan = true;
			        	  break;
			          }
			          for (Iterator<Plan> j = newFocus.children(); j.hasNext();) {
			        	  newFocus = j.next();
			        	  if ( newFocus.toString().equals(args) ) {
			        		  foundPlan = true;
			        		  break;
			        	  }
			          }
			          if (! foundPlan) {
				          newFocus=null;			        	  
			          } else {
			        	  break;
			          }
			       }
			      if (newFocus != null) {
					
					Guide.guide.engine.setFocus(newFocus);
					respond("@shift");
			      }
			      else {
			    	  println("New focus not found");
			      }
				}
			}
		   }
		   
		   @Override
		   protected void help () {
		      super.help();
		      System.out.println("    ptt                 - push to talk [control key]");
		      System.out.println("    say <utterance>     - natural language text input");
		   }

}
