/**
 * ListPanel.java
 *
 *
 * Caitlin Ross 
 * 100735219
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;

public class ListPanel extends JPanel{
	
	private JLabel recipeNamesLabel;
	private JLabel spiceListLabel;
	private JList recipeList;
	private JList spiceList;
	private EventController controller;
	private Object selectedName;
    
    public void clearSpiceListSelection(){spiceList.clearSelection();}
    public void clearNamesListSelection(){recipeList.clearSelection();}
	
    public ListPanel(EventController aController) {
    	controller = aController;
    	GridBagLayout layout = new GridBagLayout();
    	GridBagConstraints constraints = new GridBagConstraints();
    	
    	setLayout(layout);
    	
    	recipeNamesLabel = new JLabel("Recipe Names");
    	constraints.gridx = 0;
    	constraints.gridy = 0;
    	constraints.gridwidth = 1;
    	constraints.gridheight = 1;
    	constraints.fill = GridBagConstraints.NONE;
    	constraints.weightx = 60;
    	constraints.weighty = 0;
    	constraints.anchor = GridBagConstraints.SOUTHWEST;
    	constraints.insets = new Insets(5, 10, 5, 10);
    	layout.setConstraints(recipeNamesLabel, constraints);
    	add(recipeNamesLabel);
    	
    	
    	spiceListLabel = new JLabel("Spice List");
    	constraints.gridx = 1;
    	constraints.gridy = 0;
    	constraints.gridwidth = 1;
    	constraints.gridheight = 1;
    	constraints.fill = GridBagConstraints.NONE;
    	constraints.weightx = 40;
    	constraints.weighty = 0;
    	constraints.anchor = GridBagConstraints.SOUTHWEST;
    	constraints.insets = new Insets(5, 10, 5, 10);
    	layout.setConstraints(spiceListLabel, constraints);
    	add(spiceListLabel);
    	
    	recipeList = new JList();
    	recipeList.setPrototypeCellValue("xxxxxxxxxxxxxxxxxxxxxxxxxx");
    	JScrollPane scrollPane = new JScrollPane(recipeList,
              ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
              ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    	constraints.gridx = 0;
    	constraints.gridy = 1;
    	constraints.gridwidth = 1;
    	constraints.gridheight = 1;
    	constraints.fill = GridBagConstraints.BOTH;
    	constraints.weightx = 60;
    	constraints.weighty = 100;
    	constraints.insets = new Insets(5, 10, 5, 10);
    	layout.setConstraints(scrollPane, constraints);
    	add(scrollPane);
    	recipeList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
            	try{
                	controller.namesListSelected(recipeList.getModel().getElementAt(recipeList.getSelectedIndex()));
            	}
            	catch(NullPointerException ex){
            		//Don't do anything if user clicks on white space
            	}
            	catch(ArrayIndexOutOfBoundsException ex){
            		//Shouldn't affect program
            	}
            }
        });
    	
    	spiceList = new JList();
    	spiceList.setPrototypeCellValue("xxxxxxxxxxxxxxx");
    	scrollPane = new JScrollPane(spiceList,
              ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
              ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    	constraints.gridx = 1;
    	constraints.gridy = 1;
    	constraints.gridwidth = 1;
    	constraints.gridheight = 1;
    	constraints.fill = GridBagConstraints.BOTH;
    	constraints.weightx = 40;
    	constraints.weighty = 100;
    	constraints.insets = new Insets(5, 10, 5, 10);
    	layout.setConstraints(scrollPane, constraints);
    	add(scrollPane);
    	spiceList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
            	try{
                	controller.spiceListSelected(spiceList.getModel().getElementAt(spiceList.getSelectedIndex()));
            	}
            	catch(NullPointerException ex){
            		//Don't do anything if user clicks on white space
            	}
            	catch(ArrayIndexOutOfBoundsException ex){
            		//DO NOTHING! shouldn't affect program
            	}
            }
        });
    }
    
    public void setNamesList(Object names[]){
    	Object array[] = new Object[names.length + 1];
    	array[0] = "All";
    	for(int i=0; i<names.length; i++){
    		array[i+1] = names[i];
    	}
    	try{
    		recipeList.setListData(array);
    	}
    	catch(java.lang.ArrayIndexOutOfBoundsException e){
    		//Ignore exception - it doesn't affect the program
    	}
    }
    
    public void setSpiceList(String spices[]){
    	spiceList.setListData(spices);
    }
    
}