import java.util.ArrayList;
import java.util.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class NavigationMenu extends BasicMenu implements Menu 
{
	private List<MenuItem> items = new ArrayList<MenuItem>();
	
	public NavigationMenu(Menu parent)
	{
		super(parent);
		//addItem(new MenuItem("Exit", null));
		if(parent != null) {
			addItem(new MenuItem("Go to previous menu", parent));
		}
	}
	

	public void addItem(MenuItem item)
	{
		items.add(item);
	}
	
	
	public boolean executeMenu()
	{
		String output = "";
		String message = menuText;
		output += "\n\n\n";
		for(int i = 0; i < items.size(); i++) {
			output += i + ") " + items.get(i).getLabel() + "\n";
		}

		System.out.println(output);
		System.out.println(message);
		
		GUI.setOutputTextArea(output);
		GUI.setInfoMessage(message);
		
		return true;
	}
	
	public boolean processInput(String input)
	{
		try {/*
	        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	        String str = "";
	        while (str != null) {
	            str = in.readLine();*/
			
	            Menu link = items.get(Integer.parseInt(input)).getLink();
	            MenuSystem.setCurrentMenu(link);
	            link.executeMenu();

	            /*if(link == null) {
	            	return true;
	            } else {
	            	while(link.executeMenu() == false) {
	            		//Keep offering menu until valid choice is made
	            	}
	            	continue;
	            }
	        }
	    } catch (IOException e) {
	    	System.out.println("An I/O error occured. Please try again.");*/
	    } catch (IndexOutOfBoundsException e) {
	    	String message = "Please enter an integer in the range: [0-" + (items.size()-1) + "].";
	    	//System.out.println(message);
	    	GUI.setErrorMessage(message);
	    } catch (NumberFormatException e) {
	    	String message = "Please enter an integer value.";
	    	//System.out.println(message);
	    }
	    
	    return false;
	}
}
