package edu.wpi.cetask.guide;
import edu.wpi.cetask.Plan;
import edu.wpi.cetask.Task;

import java.awt.*;
import java.awt.event.*;
import java.beans.Statement;
import java.lang.reflect.InvocationTargetException;

import javax.script.ScriptException;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;

import java.text.NumberFormat;
import java.util.*;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class CustomUI implements ITaskUI {
	
	 private String weight = "0";
	 private String origin = "Boston";
	 private String dest = "";
	 private String primary = "";
	 private double cost = 0.0;
	 private ArrayList<String> addons = new ArrayList<String>();
	 private JFrame win;
	 private JButton btnStart;
	 private JComboBox listTopDests;
	 private JPanel paneDest;
	 private JLabel lblEnterWeightUnits;
	 private JLabel lblEnterWeight;
	 private JButton btnServices;
	 private JButton btnDest;
	 private JButton btnWeight;
	 private JPanel paneWeight;
	 private JPanel paneMain;
	 private JTabbedPane paneTabs;
	 private JButton btnNextDone;
	 private JLabel lblCost;
	 private JLabel lblStatus;
	 private JList listAddons;
	 private JLabel lblAddOns;
	 private JLabel lblPrimary;
	 private JComboBox listPrimary;
	 private JPanel panePrimary;
	 private JSpinner spinWeight;
	 private JLabel lblBackground;
	 private JButton btnWeightNext;
	 private JButton btnDestSearch;
	 private JTextField txtDestSearch;
	 private JList listSearchResults;
	 private JLabel lblCostCaption;
	 private JLabel lblService;
	 private JLabel lblServiceCaption;
	 private JLabel lblDest;
	 private JLabel lblDestCaption;
	 private JLabel lblWeight;
	 private JLabel lblWeightCaption;
	 private JPanel currentTxnPanel;
	 private boolean withSpeech = true;
	 private boolean doneIsLive = false;
	 private final static int WINDOW_WIDTH = 600;
	 
	   public static void main (String[] args) {
		   	CustomUI ui = new CustomUI();
		   	ui.win.setVisible(true);
	   }
	   
	 protected CustomUI() {
		win = SpeechUI.getSpeechWindow();
		if (win == null) { 
			win = new JFrame(); 
			withSpeech = false;
		}
		win.setSize(WINDOW_WIDTH, 324);
		String title = "Mails Interface" + ( ( withSpeech ? " with Speech" : "") );
		win.setTitle(title);
		win.setLayout(null);
		win.setPreferredSize(new java.awt.Dimension(WINDOW_WIDTH, 324));
		{
			lblBackground = new JLabel(new ImageIcon("java/edu/wpi/cetask/guide/image/continents_map_sm.jpg"));
			ImageIcon img = new ImageIcon(getClass().getClassLoader().getResource("edu/wpi/cetask/images/continents_map_sm.jpg"));
			img.setImage(img.getImage().getScaledInstance(WINDOW_WIDTH, 324, java.awt.Image.SCALE_SMOOTH));
			lblBackground.setIcon(img);
			win.getLayeredPane().add(lblBackground, new Integer(Integer.MIN_VALUE));
			lblBackground.setBounds(0, 0, WINDOW_WIDTH, 324);
		}		
		{
			JPanel panel = new JPanel();
			panel.setOpaque(false);
			panel.setBounds(0, 4, WINDOW_WIDTH, 324);
			panel.setLayout(null);
			win.setContentPane(panel);
		}

		win.getContentPane().setBackground(new java.awt.Color(255,255,255));
		win.setResizable(false);
		win.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		{
			btnStart = new JButton();
			win.getContentPane().add(btnStart);
			btnStart.setText("Start");
			btnStart.setBounds(0, 0, 120, 30);
			btnStart.setFont(new java.awt.Font("Tahoma",0,14));
			btnStart.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent evt) {
					lblStatus.setText("Starting...");
					addInput("task SendPackage / \"" + origin + "\"");
				}				
			});
		}
		{
			currentTxnPanel = new JPanel();
			GridLayout currentTxnPanelLayout = new GridLayout(5, 3);
			currentTxnPanelLayout.setColumns(3);
			currentTxnPanelLayout.setHgap(5);
			currentTxnPanelLayout.setVgap(5);
			currentTxnPanelLayout.setRows(5);
			currentTxnPanel.setLayout(currentTxnPanelLayout);
			win.getContentPane().add(currentTxnPanel);
			currentTxnPanel.setBounds(397, 21, 197, 200);
			currentTxnPanel.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, new java.awt.Color(0,128,255), new java.awt.Color(128,128,255), new java.awt.Color(0,0,160), new java.awt.Color(0,0,128)));
			currentTxnPanel.setBackground(new java.awt.Color(255,255,255));
			{
				lblWeightCaption = new JLabel();
				currentTxnPanel.add(lblWeightCaption);
				lblWeightCaption.setText("Weight:");
			}
			{
				lblWeight = new JLabel();
				currentTxnPanel.add(lblWeight);
				lblWeight.setText(weight + " kg");
			}
			{
				lblDestCaption = new JLabel();
				currentTxnPanel.add(lblDestCaption);
				lblDestCaption.setText("Destination:");
			}
			{
				lblDest = new JLabel();
				currentTxnPanel.add(lblDest);
				lblDest.setText(dest);
			}
			{
				lblServiceCaption = new JLabel();
				currentTxnPanel.add(lblServiceCaption);
				lblServiceCaption.setText("Service:");
			}
			{
				lblService = new JLabel();
				currentTxnPanel.add(lblService);
				lblService.setText(primary);
			}
			{
				lblCostCaption = new JLabel();
				currentTxnPanel.add(lblCostCaption);
				lblCostCaption.setText("Cost:");
			}
			{
				lblCost = new JLabel();
				currentTxnPanel.add(lblCost);
				lblCost.setText("0.00");
				lblCost.setSize(33, 14);
			}
		}
		{
			btnNextDone = new JButton();
			win.getContentPane().add(btnNextDone);
			btnNextDone.setText("Next");
			btnNextDone.setBounds(467, 254, 120, 30);
			btnNextDone.setFont(new java.awt.Font("Tahoma",0,14));
			btnNextDone.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent evt) {
					btnNextDoneMouseClicked(evt);
				}
			});
		}
		{
			paneTabs = new JTabbedPane();
			win.getContentPane().add(paneTabs);
			paneTabs.setBounds(12, 36, 300, 228);
			paneTabs.setBackground(new java.awt.Color(255,255,255));
			{
				paneMain = new JPanel();
				FlowLayout paneMainLayout = new FlowLayout();
				paneMainLayout.setAlignOnBaseline(true);
				paneMainLayout.setHgap(20);
				paneMainLayout.setVgap(20);
				paneMain.setLayout(paneMainLayout);
				paneTabs.addTab("Main", null, paneMain, null);
				paneMain.setBackground(new java.awt.Color(255,255,255));
				paneMain.setPreferredSize(new java.awt.Dimension(264, 185));
				paneMain.setOpaque(false);
				{
					btnWeight = new JButton();
					paneMain.add(btnWeight);
					btnWeight.setText("Weight");
					btnWeight.setPreferredSize(new java.awt.Dimension(130, 30));
					btnWeight.setFont(new java.awt.Font("Tahoma",1,14));
					btnWeight.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							lblStatus.setText("Weight...");
							addInput("task SpecifyWeight");
						}
					});
				}
				{
					btnDest = new JButton();
					paneMain.add(btnDest);
					btnDest.setText("Destination");
					btnDest.setPreferredSize(new java.awt.Dimension(130, 30));
					btnDest.setFont(new java.awt.Font("Tahoma",1,14));
					btnDest.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							lblStatus.setText("Destination...");
							addInput("task SpecifyDestination");
							addInput("by top");
							addInput("execute LookupTopDests");
						}
					});
				}
				{
					btnServices = new JButton();
					paneMain.add(btnServices);
					btnServices.setText("Services");
					btnServices.setPreferredSize(new java.awt.Dimension(130, 30));
					btnServices.setFont(new java.awt.Font("Tahoma",1,14));
					btnServices.setEnabled(false);
					btnServices.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							lblStatus.setText("Services...");
							addInput("task SelectServices");
						}
					});
				}
			}
			{
				paneWeight = new JPanel();
				FlowLayout paneWeightLayout = new FlowLayout();
				paneWeightLayout.setHgap(15);
				paneWeightLayout.setVgap(10);
				paneWeight.setLayout(paneWeightLayout);
				paneTabs.addTab("Weight", null, paneWeight, null);
				paneWeight.setOpaque(false);
				{
					lblEnterWeight = new JLabel();
					paneWeight.add(lblEnterWeight);
					lblEnterWeight.setText("Enter Weight: ");
					lblEnterWeight.setFont(new java.awt.Font("Tahoma",0,14));
				}
				{
					SpinnerListModel jSpinner1Model = 
						new SpinnerListModel(
								new String[] { "1" , "2" , "3" , "4", "5", "6", "7", "8", "9", "10", "15", "20", "25", "30"  });
					spinWeight = new JSpinner();
					paneWeight.add(spinWeight);
					spinWeight.setModel(jSpinner1Model);
					spinWeight.getEditor().setFont(new java.awt.Font("Dialog",0,14));
				}
				{
					lblEnterWeightUnits = new JLabel();
					paneWeight.add(lblEnterWeightUnits);
					lblEnterWeightUnits.setText("kg");
					lblEnterWeightUnits.setFont(new java.awt.Font("Tahoma",0,14));
				}
				{
					btnWeightNext = new JButton();
					paneWeight.add(btnWeightNext);
					btnWeightNext.setText("Next");
					btnWeightNext.setFont(new java.awt.Font("Tahoma",0,14));
					btnWeightNext.setPreferredSize(new java.awt.Dimension(103, 26));
					btnWeightNext.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							btnWeightNextMouseClicked(evt);
						}
					});
				}
			}
			{
				paneDest = new JPanel();
				FlowLayout paneDestLayout = new FlowLayout();
				paneDestLayout.setHgap(10);
				paneDestLayout.setVgap(15);
				paneDest.setLayout(paneDestLayout);
				paneTabs.addTab("Destination", null, paneDest, null);
				paneDest.setOpaque(false);
				{
					ComboBoxModel listTopDestsModel = 
						new DefaultComboBoxModel(
								new String[] { "Most Common Destionations", "Item Two" });
					listTopDests = new JComboBox();
					paneDest.add(listTopDests);
					listTopDests.setModel(listTopDestsModel);
					listTopDests.setPreferredSize(new java.awt.Dimension(203, 24));
					listTopDests.setFont(new java.awt.Font("Tahoma",0,14));
					listTopDests.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							listTopDestsActionPerformed(evt);
						}
					});
				}
				{
					txtDestSearch = new JTextField();
					paneDest.add(txtDestSearch);
					txtDestSearch.setPreferredSize(new java.awt.Dimension(147, 25));
					txtDestSearch.setFont(new java.awt.Font("Tahoma",0,14));
				}
				{
					btnDestSearch = new JButton();
					paneDest.add(btnDestSearch);
					btnDestSearch.setText("Search");
					btnDestSearch.setFont(new java.awt.Font("Tahoma",0,14));
					btnDestSearch.setPreferredSize(new java.awt.Dimension(90, 26));
					btnDestSearch.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							lblStatus.setText("Searching for Destination...");
							addInput("done SelectDest / \"\" /  / false / false");
							//addInput("by search");
							addInput("execute SearchForDest / \"" + txtDestSearch.getText() + "\"");
						}
					});
				}
				{
					/*
					 * ListModel listSearchResultsModel = 
						new DefaultComboBoxModel(
								new String[] { "Item One", "Item Two" });
					 */
					//listSearchResults.setModel(listSearchResultsModel);
					listSearchResults = new JList();
					paneDest.add(listSearchResults);
					listSearchResults.setPreferredSize(new java.awt.Dimension(200, 100));
					listSearchResults.setFont(new java.awt.Font("Tahoma",0,14));
					listSearchResults.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							if (evt.getClickCount() == 2) {
								lblStatus.setText("Selecting Destination...");
								String dest = listSearchResults.getSelectedValue().toString();
								lblDest.setText(dest);
								addInput("execute SelectDest / \"" + dest + "\"");
							}
						}
					});
				}
			}
			{
				panePrimary = new JPanel();
				paneTabs.addTab("Services", null, panePrimary, null);
				paneTabs.setEnabledAt(paneTabs.indexOfComponent(panePrimary), false);
				panePrimary.setOpaque(false);
				panePrimary.setVisible(false);
				panePrimary.setPreferredSize(new java.awt.Dimension(295, 202));
				{
					lblPrimary = new JLabel();
					panePrimary.add(lblPrimary);
					lblPrimary.setText("Primary Service: ");
					lblPrimary.setFont(new java.awt.Font("Tahoma",0,14));
				}
				{
					ComboBoxModel listPrimaryModel = 
						new DefaultComboBoxModel(
								new String[] { "Services..." });
					listPrimary = new JComboBox();
					panePrimary.add(listPrimary);
					listPrimary.setModel(listPrimaryModel);
					listPrimary.setPreferredSize(new java.awt.Dimension(190, 26));
					listPrimary.setFont(new java.awt.Font("Tahoma",0,14));
					listPrimary.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							listPrimaryActionPerformed(evt);
						}
					});
				}
				{
					lblAddOns = new JLabel();
					panePrimary.add(lblAddOns);
					lblAddOns.setText("Additional Services: ");
					lblAddOns.setFont(new java.awt.Font("Tahoma",0,14));
				}
				{
					listAddons = new JList();
					panePrimary.add(listAddons);
					listAddons.setPreferredSize(new java.awt.Dimension(180, 100));
					listAddons.setFont(new java.awt.Font("Tahoma",0,14));
					listAddons.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							if (evt.getClickCount() == 2) {
								lblStatus.setText("Selectign AddOn...");
								String addon = listAddons.getSelectedValue().toString();
								//lblDest.setText(dest);
								addInput("execute SelectAddOn / " + weight + " / \"" + dest + "\" / \"" + primary + "\" / \"" + addon + "\"");
							}						
						}
					});
				}
			}
		}
		{
			lblStatus = new JLabel();
			win.getContentPane().add(lblStatus);
			lblStatus.setText("Status");
			lblStatus.setBounds(12, 266, 368, 14);
			lblStatus.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
			lblStatus.setSize(368, 18);
			lblStatus.setOpaque(true);
		}

		GridLayout lblWeightLayout = new GridLayout(1, 1);
		lblWeightLayout.setColumns(1);
		lblWeightLayout.setHgap(5);
		lblWeightLayout.setVgap(5);
		
		win.setVisible(true);

		addInput("load models/Mails2.xml");

	}
	
	@Override
	public void deallocate() {
		// TODO Auto-generated method stub

	}

	@Override
	public void println(Object object) {
		lblStatus.setText(object.toString());
	}

	@Override
	public void prompt() {
		setupLiveTasks();
		Plan focus = Guide.guide.engine.getFocus();
		if (focus != null) { 
			setInfoPanel();
			String focusName = focus.toString();
			String goalName = focus.getGoal().toString();
	        try {
	            // dispatch to command method
	        	Statement stmt = new Statement(this, goalName, 
	                  new Object[] { focus });
	        	stmt.execute();
	         } catch (NoSuchMethodException m) {
	            this.lblStatus.setText("Task " + focusName + " not recognized.");
	         } catch (InvocationTargetException t) {
	            this.lblStatus.setText(t.toString());
	         } catch (Exception e) { this.lblStatus.setText(e.toString()); }		
		
			focus.getParent().getGoal().getSlotValue("weight");
/*
			if (focusName.equals("SendPackage")) {
				this.paneTabs.setSelectedComponent(this.paneMain);
			} else if ( focusName.equals("SpecifyWeight") ) {
				this.paneTabs.setSelectedComponent(this.paneWeight);
			} else if ( focusName.equals("SpecifyDestination") ) {
				this.paneTabs.setSelectedComponent(this.paneDest);
			} else if ( focusName.equals("LookupTopDests") ) {
				String[] strArray = (String[])(focus.getGoal().getSlotValue("topDests"));
				if (strArray != null) {
					listTopDests.setModel(new DefaultComboBoxModel(strArray));					
				}
				this.paneTabs.setSelectedComponent(this.paneDest);
			}
			*/
		}
	}
	
	
	public void SendPackage(Plan focus) {
		this.paneTabs.setSelectedComponent(this.paneMain);
	}
	
	public void SpecifyWeight(Plan focus) {
		this.paneTabs.setSelectedComponent(this.paneWeight);
	}
	
	public void SpecifyDestination(Plan focus) {
		Plan p;
		this.paneTabs.setSelectedComponent(this.paneDest);
		if (focus.getDecomposition() != null) {
			boolean topDecomp = focus.getDecomposition().toString().equals("top");
			for (Iterator<Plan> i = focus.children(); i.hasNext();) {
				p = i.next();
				if (topDecomp) {
					if (p.getGoal().toString().equals("LookupTopDests")&& p.isDone() && !p.hasFailed() ) {
						setComboBoxModel(listTopDests, (ArrayList<String>)(p.getGoal().getSlotValue("topDests")));
					}
				} else {
					if (p.getGoal().toString().equals("SearchForDest")&& p.isDone() && !p.hasFailed() ) {
						setListBoxModel(listSearchResults, (ArrayList<String>)(p.getGoal().getSlotValue("matches")));
					}
	
				}
	
			}
		}
		//SelectDest(focus);
	}
	
	public void LookupTopDests (Plan focus) {

	}
	
	public void SelectDest (Plan focus) {
		//this.paneTabs.setSelectedComponent(this.paneDest);
		//setComboBoxModel(listTopDests, (ArrayList<String>)(focus.getGoal().getSlotValue("topdests")));

	}
	
	public void SelectServices (Plan focus) {
		this.paneTabs.setSelectedComponent(this.panePrimary);
		String goalName;
		boolean primaryIsDone = false;
		for (Iterator<Plan> i = focus.children(); i.hasNext();) {
			Plan p = i.next();
			goalName = p.getGoal().toString();
			if (goalName.equals("GetPrimarySvcs")) {
				if (p.isDone()) {
					setComboBoxModel(listPrimary, (ArrayList<String>)(p.getGoal().getSlotValue("services")));					
				} else {
					addInput("execute GetPrimarySvcs / " + weight + " / \"" + dest + "\"");
				}
			} else if (goalName.equals("SelectPrimary") && p.isDone()) {
				primaryIsDone = true;
			} else if (primaryIsDone && goalName.equals("GetAddSvcs")) {
				// hack!!!!
				//addInput("next");
				// back to reality
				if (p.isDone()) {
					setListBoxModel(listAddons, (ArrayList<String>)(p.getGoal().getSlotValue("services")));					
				} else {
					addInput("execute GetAddSvcs / " + weight + " / \"" + dest + "\" / \"" + primary + "\"");
				}			
			}
		}
	}
	
	public void PrimaryService (Plan focus) {
		this.paneTabs.setSelectedComponent(this.panePrimary);
		for (Iterator<Plan> i = focus.children(); i.hasNext();) {
			Plan p = i.next();
			if (p.getGoal().toString().equals("GetPrimarySvcs")&& p.isDone() && !p.hasFailed() ) {
				setComboBoxModel(listPrimary, (ArrayList<String>)(p.getGoal().getSlotValue("services")));
			}
		}
	}	
	
	public void GetPrimarySvcs (Plan focus) {
		lblStatus.setText("Trying to get Primary services...");
	}

	public void SelectPrimary (Plan focus) {
		lblStatus.setText("Trying to select Primary service...");
	}
	
	public void AdditionalServices (Plan focus) {
		for (Iterator<Plan> i = focus.children(); i.hasNext();) {
			Plan p = i.next();
			if (p.getGoal().toString().equals("GetAddons")&& p.isDone() && !p.hasFailed() ) {
				setListBoxModel(listAddons, (ArrayList<String>)(p.getGoal().getSlotValue("services")));
			}
		}		
	}
	
	public void GetAddSvcs (Plan focus) {
		lblStatus.setText("Trying to get AddOn services...");
		
	}
	
	public void SelectAddOn (Plan focus) {
		lblStatus.setText("Trying to select AddOn service...");		
	}
	
	public void CompleteTransaction (Plan focus) {
		lblStatus.setText("Trying to complete transaction...");
	}
	
	private void setComboBoxModel(JComboBox comboBox, ArrayList<String> arrayList) {
		String[] myArray;
		if (arrayList.size() > 0) {
			myArray = new String[arrayList.size()];
			myArray =	arrayList.toArray(myArray);	
		} else {
			myArray = new String[] { "No results found." }; 	
		}
		ComboBoxModel model = new DefaultComboBoxModel(myArray);
		comboBox.setModel(model);
	}
	
	private void setListBoxModel(JList listBox, ArrayList<String> arrayList) {
		String[] myArray;
		if (arrayList.size() > 0) {
			myArray = new String[arrayList.size()];
			myArray =	arrayList.toArray(myArray);	
		} else {
			myArray = new String[] { "No results found." }; 	
		}
		ListModel model = new DefaultComboBoxModel(myArray);
		listBox.setModel(model);
	}

	private void enterWeight(String strWeight) {
		lblStatus.setText("Setting weight to " + strWeight + "...");
		lblWeight.setText(strWeight + " kg");
		addInput("execute SpecifyWeight / " + strWeight);
	}
	
	private static void addInput(String input) {
		if (Guide.guide != null) {
			Guide.guide.addInput(input);
		}
	}

	/**
	 * Each time the UI is prompted, see if new transaction parameters are available.
	 * 
	 */
	private void setInfoPanel () {
		try {
			weight = Guide.guide.engine.evalGlobal("varWeight", "Guide").toString();
			lblWeight.setText(weight + " kg");

			dest = Guide.guide.engine.evalGlobal("varDest", "Guide").toString();
			lblDest.setText(dest);
			
			primary =  Guide.guide.engine.evalGlobal("varPrimary", "Guide").toString();
			lblService.setText(primary);
			
			Object objAddons = Guide.guide.engine.evalGlobal("varAddOns", "Guide");
			if (objAddons instanceof ArrayList) {
				addons = (ArrayList) objAddons;
			}
			if (addons.size() > 0) lblService.setText(primary + "\n" + addons.get(0));

			cost =  ((Double)Guide.guide.engine.evalGlobal("varCost", "Guide")).doubleValue();
			NumberFormat nf = NumberFormat.getCurrencyInstance();
			lblCost.setText(nf.format(cost));

		} catch (ScriptException e) {
			lblStatus.setText("Exception in setInfoPanel: " + e.toString());
		}
	}
	
	private void setupLiveTasks() {
		Plan p;
		String goalName;
		boolean isLive;
		boolean isADestLive;
		p = Guide.guide.engine.getFocus();
		if (p != null) {
			Plan top = Guide.guide.engine.getTop(p);
			if (top != null) {
				paneTabs.setVisible(true);
				isADestLive=false;
				for (Iterator<Plan> i = top.children(); i.hasNext();) {
					p = i.next();
					goalName = p.getGoal().toString();
					//Guide.guide.println("Before is live: " + new Date().getTime());
					isLive = p.isLive();
					//Guide.guide.println(" After is live: " + new Date().getTime());
					if (goalName.equals("CompleteTransaction")) {
						if (isLive) {
							btnNextDone.setText("Done");
						} else {
							btnNextDone.setText("Next");
						}
						doneIsLive = isLive;
					} else if (goalName.equals("SelectServices")) {
						btnServices.setEnabled(isLive);
						paneTabs.setEnabledAt(paneTabs.indexOfComponent(panePrimary), isLive);
					} else if (goalName.equals("SpecifyWeight")) {
						btnWeight.setEnabled(isLive);
						paneTabs.setEnabledAt(paneTabs.indexOfComponent(paneWeight), isLive);
					} else if (goalName.equals("SpecifyDestination")) {
						if (! isADestLive) {
							btnDest.setEnabled(isLive);
							paneTabs.setEnabledAt(paneTabs.indexOfComponent(paneDest), isLive);	
							isADestLive = isLive;
						}
					}
				}
			}
		} else {
			paneTabs.setVisible(false);
		}
	}
	
	private void btnWeightNextMouseClicked(MouseEvent evt) {
		enterWeight(spinWeight.getValue().toString());
	}
	
	private void btnNextDoneMouseClicked(MouseEvent evt) {
		if (doneIsLive) {
			lblStatus.setText("Completing transaction...");
			addInput("task CompleteTransaction");
			addInput("execute CompleteTransaction / varWeight / varDest / \"" + origin + "\" / varPrimary / varAddOns");
		} else {
			lblStatus.setText("What is next?");
			addInput("next");
		}
	}
	
	private void listTopDestsActionPerformed(ActionEvent evt) {
		JComboBox jcmbType = (JComboBox) evt.getSource();
		String cmbType = (String) jcmbType.getSelectedItem();
		lblStatus.setText("Select destination " + cmbType + "...");
		lblDest.setText(cmbType);
		addInput("execute SelectDest / \"" + cmbType + "\"");

	}
	
	private void listPrimaryActionPerformed(ActionEvent evt) {
		JComboBox jcmbType = (JComboBox) evt.getSource();
		String cmbType = (String) jcmbType.getSelectedItem();
		lblStatus.setText("Selecting service " + cmbType + "...");
		lblService.setText(cmbType);
		addInput("execute SelectPrimary / " + weight + " / \"" + dest + "\" / \"" + cmbType + "\"");

	}

}
